3 回答
TA贡献1828条经验 获得超4个赞
mousewheel
wheelDelta
px
>0
up
wheelDelta
<0
down
.
火狐DOMMouseScroll
originalEvent.detail
+/-
3
120
-40
@EASISTY的回答<div>
delta
200px
<div>
500px
scrollTop
400
mousewheel
120px
<div>
<body>
400
+ 120
> 500
.
jQuery
$(document).on('DOMMouseScroll mousewheel', '.Scrollable', function(ev) { var $this = $(this), scrollTop = this.scrollTop, scrollHeight = this.scrollHeight, height = $this.innerHeight(), delta = (ev.type == 'DOMMouseScroll' ? ev.originalEvent.detail * -40 : ev.originalEvent.wheelDelta), up = delta > 0; var prevent = function() { ev.stopPropagation(); ev.preventDefault(); ev.returnValue = false; return false; } if (!up && -delta > scrollHeight - height - scrollTop) { // Scrolling down, but this will take us past the bottom. $this.scrollTop(scrollHeight); return prevent(); } else if (up && delta > scrollTop) { // Scrolling up, but this will take us past the top. $this.scrollTop(0); return prevent(); }});
scrollTop
<div>
mousewheel
body
TA贡献1863条经验 获得超2个赞
function preventDefault(e) { e = e || window.event; if (e.preventDefault) e.preventDefault(); e.returnValue = false; }document.getElementById('a').onmousewheel = function(e) { document.getElementById('a').scrollTop -= e. wheelDeltaY; preventDefault(e);}
添加回答
举报