贴心猫(Imcat)
贴心代码
详情
js鼠标滚动方向
发布:2011-11-29 17:09
点击1042 次
评论:0 条
JavaScript获取鼠标滚动方向
http://hi.baidu.com/linderman/blog/item/b48bc33f1469b3cc9e3d6252.html
<html>
<head>
<title>JavaScript判断鼠标滚动方向</title>
<script type="text/javascript">
function handle(delta) {
var s = delta + ": ";
if (delta <0)
s += "您在向下滚……";
else
s += "您在向上滚……";
document.getElementById(''delta'').innerHTML = s;
}
function wheel(event){
var delta = 0;
if (!event) event = window.event;
if (event.wheelDelta) {
delta = event.wheelDelta/120;
if (window.opera) delta = -delta;
} else if (event.detail) {
delta = -event.detail/3;
}
if (delta)
handle(delta);
}
if (window.addEventListener) {
window.addEventListener(''DOMMouseScroll'', wheel, false);
}
window.Onmousewheel = document.Onmousewheel = wheel;
</script>
</head>
<body>
<div id="delta">滚动中轮试试~请选按着中轮滚动,激活后可以不按,直接滚动。</div>
</body>
</html>
var docu = document; //alert(window.opener);
var docp = window.parent.document.body;
// 隐藏父窗口滚动条
if(docp){
//window.parent.document.body.style.cssText = "overflow-Y:hidden;";
//window.parent.document.body.style.overflow = ''hidden'';
var dp = window.parent.document;
var dE = dp.documentElement;
var dB = dp.body;
var a = dE.scrollTop==0? dB.clientHeight : dE.clientHeight;
var b = dE.scrollTop==0? dB.scrollTop : dE.scrollTop;
var c = dE.scrollTop==0? dB.scrollHeight : dE.scrollHeight;
//a = 62;
//window.parent.document.body.style.cssText = ''height:''+(c)+''px;position:relative;left:0px;top:''+(b)+''px;overflow:hidden'';
window.parent.document.body.style.overflow = ''hidden'';
//alert(b);
//window.parent.document.body.style.position = ''relative;'';
}
function mouseWStop(e){
try{ e.returnValue = false; } //IE/Opera/Chrome
catch(e2){ e.preventDefault(); } //Firefox
return;
}
function mouseWhell(e){
e = e || window.event; d = 0;
//mouseWStop(e); //测试:屏蔽所有
var dE = docu.documentElement;
var dB = docu.body;
var a = dE.scrollTop==0? dB.clientHeight : dE.clientHeight;
var b = dE.scrollTop==0? dB.scrollTop : dE.scrollTop;
var c = dE.scrollTop==0? dB.scrollHeight : dE.scrollHeight;
if (e.wheelDelta) {
d = e.wheelDelta/120;
if (e.opera) d = -d;
} else if (e.detail) {
d = -e.detail/3;
}
if(docp){ // 有父窗口
if(Math.abs(d)>1){ //快速滑动,会滑动父窗口
mouseWStop(e);
return;
}
if(d>0){ //up
if(b<20) mouseWStop(e);
}
if(d<0){ //down
if(c-(a+b)<20) mouseWStop(e);
}
//if(((c-(a+b)<110&&d<0))||((b<110)&&d>0)) mouseWStop(e);
}
if(a+b==c){
if((b==0&&d>0)||(b>0&&d<0)) mouseWStop(e);
}else{
//docu.title = a+"+"+b+"="+c+"("+d+"); "; //测试
}
}//*/
if(docu.addEventListener){
//docu.addEventListener("DOMMouseScroll",mouseWhell,false);
}else{
//docu.Onmousewheel = mouseWhell;
}
/*
if(docu.addEventListener){
docu.addEventListener("DOMMouseScroll",function(e){
//e.preventDefault();
},false);
}
else{
docu.Onmousewheel = function(){
//event.returnValue = false;
};
}*/