-
Notifications
You must be signed in to change notification settings - Fork 0
/
鼠标滚轮
65 lines (62 loc) · 1.7 KB
/
鼠标滚轮
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
62
63
64
65
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#div1{width:200px;height:200px;background:red;margin:0 auto;}
</style>
<script>
//普通事件:onclick、onmousedown
//DOM事件:DOMMouseScroll
//DOM事件只能通过绑定来实现
function myAddEvent(obj,sEvent,fn){
if(obj.attachEvent){
obj.attachEvent('on'+sEvent,fn);
}
else{
obj.addEventListener(sEvent,fn,false);
}
}
window.onload=function(){
var oDiv=document.getElementById('div1');
function onMouseWheel(ev){
var oEvent=ev||event;
//IE
//wheelDelta: 下 负
// 上 正
//FF
//detail: 下 正
// 上 负
var bDown=true;
/* if(oEvent.wheelDelta){
if(oEvent.wheelDelta<0){
bDown=true;
}else{
bDown=false;
}
}
else{
if(oEvent.detail>0){
bDown=true;
}else{
bDown=false;
}
}*/
bDown=oEvent.wheelDelta?oEvent.wheelDelta<0:oEvent.detail>0;
if(bDown){
oDiv.style.height=oDiv.offsetHeight+10+'px';
}else{
oDiv.style.height=oDiv.offsetHeight-10+'px';
}
return false;
}
myAddEvent(oDiv,'mousewheel',onMouseWheel);
myAddEvent(oDiv,'DOMMouseScroll',onMouseWheel);
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>