1、鼠标事件
鼠标事件 | |
---|
鼠标移入 | onmouseenter |
鼠标移出 | onmouseleave |
鼠标点击 | onclick |
鼠标移动 | onmousemove |
2、键盘事件
键盘事件 | onkeydown |
---|
键码 | keyCode |
向左偏移量 | offsetLeft |
向上偏移量 | offsetTop |
document.onkeydown = function (e) {
console.log(e.keyCode);
}
document.onkeydown = function (e) {
let code = e.keyCode;
switch (code) {
case 37: console.log('左'); break;
case 38: console.log('上'); break;
case 39: console.log('右'); break;
case 40: console.log('下'); break;
}
}
var box = document.querySelector('.box');
console.log(box.offsetLeft);
console.log(box.offsetTop);
document.onkeydown = function (e) {
let code = e.keyCode;
switch (code) {
case 37: box.style.left = box.offsetLeft - 5 + 'px'; break;
case 38: box.style.top = box.offsetTop - 5 + 'px'; break;
case 39: box.style.left = box.offsetLeft + 5 + 'px'; break;
case 40: box.style.top = box.offsetTop + 5 + 'px'; break;
}
}
3、触屏事件
var box = document.querySelector('.box');
box.ontouchstart = function () {
console.log('ontouchstart');
}
box.ontouchend = function () {
console.log('ontouchend');
}
box.ontouchmove = function () {
console.log('ontouchmove');
}