event的属性

扫码查看

t获取鼠标相对于浏览器左上角的坐标

<div id="dv" style=" width:300px; height:200px; background-color: Blue;">

<script type="text/javascript">
window.onload = function () {
document.getElementById('dv').onmousedown = function () {
//相对于页面的左上角0的位置的坐标,不是层的0,0开始
if (arguments.length > 0) {
//火狐浏览器
document.title = arguments[0].clientX + '====' + arguments[0].clientY;
} else {
//IE浏览器
document.title = window.event.clientX + '====' + window.event.clientY;
}
};
};
</script>

获取用户按下的键

<div id="dv" style=" width:300px; height:200px; background-color: Orange;">

<script type="text/javascript">

        window.onload = function () {

            document.getElementById('dv').onclick = function () {

                //用户是否按下了ctrl键
if (window.event.ctrlKey) {
alert('按下了ctrl键');
} else if (window.event.shiftKey) {
alert('按下了shift键');
} else if (window.event.altKey) {
alert('按下了alt键');
} else {
alert('什么都没按下');
}
//用户是否按下了shift键 //用户是否按下了alt键
}; };
</script>

获取鼠标每个键的值

window.event.button; //左键是1,右键是2,左右同时是3,中滑轮是4

//获取的横坐标是相对屏幕左上角的 // document.title = window.event.screenX+'==='+window.event.screenY;

//当前这个元素的左上角--事件源 document.title = window.event.offsetX;

05-11 05:12
查看更多