JSDOM事件续

一.事件的传播

事件的传播分为三个阶段:
------1.捕获阶段
-------- 在捕获阶段时, 从最外层的祖先元素, 向目标元素进行事件捕获, 但此时默认不会触发事件
------2.目标阶段
-------- 事件捕获到目标元素, 捕获结束, 并开始在目标元素上触发事件
------3.冒泡阶段
-------- 事件从目标元素向他的祖先元素传递, 依次触发祖先元素上的相应事件

如果希望在捕获阶段就触发事件, 可以将addEventListener()方法中的第三个参数设置为true, 但一般情况下, 我们不会这么做, 所以一般这个参数都是false

IE8及以下浏览器没有捕获阶段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<head>
<meta charset="UTF-8">
<script type="text/javascript">
function bind(obj,event,callback){
if(obj.addEventListener){
obj.addEventListener(event,callback,true);
}else{
obj.attachEvent("on"+event,function(){
callback.call(obj);
});
}
}
window.onload = function(){
var box1 = document.getElementById("box1");
var box2 = document.getElementById("box2");
var box3 = document.getElementById("box3");
bind(box1,"click",function(){alert(this.id);});
bind(box2,"click",function(){alert(this.id);});
bind(box3,"click",function(){alert(this.id);});
};
</script>
<style type="text/css">
#box1{width: 300px;height: 300px;background-color: #bfc;}
#box2{width: 200px;height: 200px;background-color: red;}
#box3{width: 100px;height: 100px;background-color: yellow;}
</style>
</head>
<body>
<div id="box1">box1
<div id="box2">box2
<div id="box3">box3</div>
</div>
</div>
</body>

二.拖拽

  • 流程:
  • ------1.鼠标在被拖拽元素上按下时, 开始拖拽 onmousedown
  • ------2.鼠标移动时, 被拖拽元素跟随鼠标移动 onmousemove
  • ------3.鼠标松开时, 被拖拽元素固定在当前位置 onmouseup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<head>
<meta charset="UTF-8">
<script type="text/javascript">
function draw(obj){
//1.鼠标在obj上按下时,开始拖拽
obj.onmousedown = function(event){
//设置obj捕获第一次onmousedown事件
obj.setCapture && obj.setCapture();
event = event||window.event;
var ol = event.clientX - obj.offsetLeft;
var ot = event.clientY - obj.offsetTop;
//2.obj随鼠标移动
document.onmousemove = function(event){
event = event||window.event;
var top = event.clientY - ol;
var left = event.clientX - ot;
obj.style.left = left + "px";
obj.style.top = top + "px";
};
//3.鼠标松开,固定obj
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
//鼠标松开时,取消obj对鼠标按下的捕获
obj.releaseCapture && obj.releaseCapture();
};
//当我们拖拽网页中的内容时,浏览器会默认到搜索引擎中搜索该内容
//此时会导致拖拽异常,可以通过return false;取消搜索
//IE8及以下不支持,可以用setCapture()实现
return false;
};
}
window.onload = function(){
var box1 = document.getElementById("box1");
var box2 = document.getElementById("box2");
draw(box1);
draw(box2);
};
</script>
<style type="text/css">
#box1{
width: 100px;
height: 100px;
background-color: #FF0000;
position: absolute;
}
#box2{
width: 100px;
height: 100px;
background-color: #bfa;
position: absolute;
left: 200px;
top: 200px;
}
</style>
</head>
<body>
<span>文本内容</span>
<div id="box1"></div>
<div id="box2"></div>
</body>

三.滚轮事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<head>
<meta charset="UTF-8">
<script type="text/javascript">
function bind(obj,event,callback){
if(obj.addEventListener){
obj.addEventListener(event,callback,false);
}else{
obj.attachEvent("on"+event,function(){
callback.call(obj);
});
}
}
window.onload = function(){
var box1 = document.getElementById("box1");
//一般浏览器添加滚轮事件
box1.onmousewheel = function(event){
if(event.wheelDelta>0||event.detail<0){
box1.style.height = box1.clientHeight - 10 + "px";
}else{
box1.style.height = box1.clientHeight + 10 + "px";
}
//取消Firefox附带滚动条的默认行为
event.preventDefault && event.preventDefault();
//取消附带滚动条的默认行为
return false;
};
//Firefox添加滚轮事件
bind(box1,"DomMouseScroll",box1.onmousewheel);
};
</script>
<style type="text/css">
#box1{width: 100px;height: 100px;background-color: red;}
</style>
</head>
<body>
<div id="box1"></div>
</body>

四.键盘事件

事件 描述
onkeydown 某个键盘的键被按下
onkeyup 某个键盘的键被松开
onkeypress 某个键盘的键被按下或按住

键盘事件一般绑定给一些可以获取焦点的对象(一般为表单项)或者document

对于onkeydown事件来说, 若一直按着某个键不松手, 则事件一直在被触发;当它被连续触发时, 第一次和第二次触发之间会间隔稍微长一些, 之后触发很快; 这种设计是为了防止误操作

键盘事件属性 描述
altKey 返回当事件被触发时,“ALT” 是否被按下
ctrlKey 返回当事件被触发时,“CTRL” 键是否被按下
shiftKey 返回当事件被触发时,“SHIFT” 键是否被按下
keyCode 对于keypress事件, 该属性声明了被敲击的键生成的Unicode字符码. 对于keydown和keyup事件, 它指定了被敲击的键的虚拟键盘码. 虚拟键盘码可能和使用的键盘的布局相关.

1.键盘事件示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<head>
<meta charset="UTF-8">
<script type="text/javascript">
window.onload = function(){
var text = document.getElementById("text");
text.onkeydown = function(event){
event = event||window.event;
//主键区数字:48-57,数字键区:96-105
//禁止在文本框中输入数字
if(event.keyCode>=48&&event.keyCode<=57){
//取消在文本框中输出文本的默认行为
return false;
}
if(event.keyCode>=96&&event.keyCode<=105){
return false;
}
if(event.ctrlKey&&event.keyCode==65){
console.log("ctrl + A 被按下");
}
};
};
</script>
</head>
<body>
<input type="text" id="text" value="" />
</body>

2.使用键盘移动div

这里用键盘移动div有个小问题, 假如一直按着→键, div在第一次移动之后会有一点卡顿(这时为了防止按键的误操作), 但这不是我们希望的, 之后会用定时器解决这个问题.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<head>
<meta charset="UTF-8">
<script type="text/javascript">
window.onload = function(){
var box = document.getElementById("box");
document.onkeydown = function(event){
event = event||window.event;
var speed = 10;
//按住alt键加速
if(event.altKey){
speed = 50;
}//左:37 上:38 右:40 下:41
switch(event.keyCode){
case 37:box.style.left = box.offsetLeft - speed + "px";break;
case 38:box.style.top = box.offsetTop - speed + "px";break;
case 39:box.style.left = box.offsetLeft + speed + "px";break;
case 40:box.style.top = box.offsetTop + speed + "px";break;
}
};
};
</script>
</head>
<body>
<div id="box" style="width: 100px;height: 100px;background-color: red;position: absolute;"></div>
</body>

评论