当有人在一个元素上滚动时,我想要运行一个函数。就像这样:
$('div').scrollDown(function(){ alert('down') });
$('div').scrollUp(function(){ alert('up') });
但是这些函数并不存在。有没有解决这个问题的方法?Awwwards似乎能够做到(导航栏中的标志会根据滚动方向改变)。不幸的是,源代码已被压缩,所以没什么运气。
当有人在一个元素上滚动时,我想要运行一个函数。就像这样:
$('div').scrollDown(function(){ alert('down') });
$('div').scrollUp(function(){ alert('up') });
但是这些函数并不存在。有没有解决这个问题的方法?Awwwards似乎能够做到(导航栏中的标志会根据滚动方向改变)。不幸的是,源代码已被压缩,所以没什么运气。
最终我成功地弄清楚了,如果有人正在寻找答案:
//Firefox
$('#elem').bind('DOMMouseScroll', function(e){
if(e.originalEvent.detail > 0) {
//scroll down
console.log('Down');
}else {
//scroll up
console.log('Up');
}
//prevent page fom scrolling
return false;
});
//IE, Opera, Safari
$('#elem').bind('mousewheel', function(e){
if(e.originalEvent.wheelDelta < 0) {
//scroll down
console.log('Down');
}else {
//scroll up
console.log('Up');
}
//prevent page fom scrolling
return false;
});
e.wheelDelta
也不是 e.delta
,而是 e.originalEvent.wheelDelta
。 - sadfuzzydelta
:$('#el').on('mousewheel', function(e, delta) { if(delta > 0) { /*up*/ } else { /*down*/ }})
。请参见此答案。 - Mojo下面的例子将只监听鼠标滚轮事件,不包括触摸屏和触控板的滚动。
它使用jQuery.on()方法(从jQuery 1.7版本开始,该方法是附加事件处理程序到文档的首选方法)。
$('#elem').on( 'DOMMouseScroll mousewheel', function ( event ) {
if( event.originalEvent.detail > 0 || event.originalEvent.wheelDelta < 0 ) { //alternative options for wheelData: wheelDeltaX & wheelDeltaY
//scroll down
console.log('Down');
} else {
//scroll up
console.log('Up');
}
//prevent page fom scrolling
return false;
});
适用于所有浏览器。
这个需要更新一下 - 现在我们有了wheel
事件:
$(function() {
$(window).on('wheel', function(e) {
var delta = e.originalEvent.deltaY;
if (delta > 0) $('body').text('down');
else $('body').text('up');
return false; // this line is only added so the whole page won't scroll in the demo
});
});
body {
font-size: 22px;
text-align: center;
color: white;
background: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
现代浏览器的支持已经相当不错了:
https://developer.mozilla.org/en-US/docs/Web/Events/wheel
如果需要更深层次的浏览器支持,最好使用mousewheel.js而不是自己搞:
https://plugins.jquery.com/mousewheel/
$(function() {
$(window).mousewheel(function(turn, delta) {
if (delta > 0) $('body').text('up');
else $('body').text('down');
return false; // this line is only added so the whole page won't scroll in the demo
});
});
body {
font-size: 22px;
text-align: center;
color: white;
background: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.13/jquery.mousewheel.min.js"></script>
这篇文章和其他stackoverflow文章提供了3种解决方案。
解决方案1
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('up 1');
}
else {
console.log('down 1');
}
lastScrollTop = st;
});
$('body').on('DOMMouseScroll', function(e){
if(e.originalEvent.detail < 0) {
console.log('up 2');
}
else {
console.log('down 2');
}
});
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('up 3');
}
else {
console.log('down 3');
}
});
我无法在Safari上进行测试。
chrome 42(Win 7)
Firefox 37(Win 7)
IE 11(Win 8)
IE 10 (Win 7)
IE 9 (Win 7)
IE 8 (Win 7)
我检查了IE 11和IE 8的副作用,发现是由于if else语句引起的。因此,我将其替换为以下的if else if语句。
通过多浏览器测试,我决定对于常见的浏览器使用解决方案3,对于Firefox和IE 11使用解决方案1。
我参考了this answer来检测IE 11。
// Detect IE version
var iev=0;
var ieold = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
var trident = !!navigator.userAgent.match(/Trident\/7.0/);
var rv=navigator.userAgent.indexOf("rv:11.0");
if (ieold) iev=new Number(RegExp.$1);
if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
if (trident&&rv!=-1) iev=11;
// Firefox or IE 11
if(typeof InstallTrigger !== 'undefined' || iev == 11) {
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('Up');
}
else if(st > lastScrollTop) {
console.log('Down');
}
lastScrollTop = st;
});
}
// Other browsers
else {
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('Up');
}
else if(e.originalEvent.wheelDelta < 0) {
console.log('Down');
}
});
}
$(function(){
var _top = $(window).scrollTop();
var _direction;
$(window).scroll(function(){
var _cur_top = $(window).scrollTop();
if(_top < _cur_top)
{
_direction = 'down';
}
else
{
_direction = 'up';
}
_top = _cur_top;
console.log(_direction);
});
});
这里是一个示例,展示了一种简单的方法来实现它。脚本如下:
$(function() {
var _t = $("#container").scrollTop();
$("#container").scroll(function() {
var _n = $("#container").scrollTop();
if (_n > _t) {
$("#target").text("Down");
} else {
$("#target").text("Up");
}
_t = _n;
});
});
#container
是你的div标签 id
。 #target
仅用于查看其是否工作。在上下时更改为所需内容。
编辑
OP之前没有说,但由于他正在使用带有overflow: hidden
的div标签,因此不会发生滚动,那么检测滚动的脚本就是问题的最小部分。那么,如何检测不发生的事情?!
因此,OP本人发布了带有所需内容的链接,那么为什么不使用该库? http://cdn.jquerytools.org/1.2.5/full/jquery.tools.min.js。
调用只需:
$(function() {
$(".scrollable").scrollable({ vertical: true, mousewheel: true });
});
scrollUp
和 scrollDown
添加到您的 jQuery 中。
https://github.com/phpust/JQueryScrollDetector
var lastScrollTop = 0;
var action = "stopped";
var timeout = 100;
// Scroll end detector:
$.fn.scrollEnd = function(callback, timeout) {
$(this).scroll(function(){
// get current scroll top
var st = $(this).scrollTop();
var $this = $(this);
// fix for page loads
if (lastScrollTop !=0 )
{
// if it's scroll up
if (st < lastScrollTop){
action = "scrollUp";
}
// else if it's scroll down
else if (st > lastScrollTop){
action = "scrollDown";
}
}
// set the current scroll as last scroll top
lastScrollTop = st;
// check if scrollTimeout is set then clear it
if ($this.data('scrollTimeout')) {
clearTimeout($this.data('scrollTimeout'));
}
// wait until timeout done to overwrite scrolls output
$this.data('scrollTimeout', setTimeout(callback,timeout));
});
};
$(window).scrollEnd(function(){
if(action!="stopped"){
//call the event listener attached to obj.
$(document).trigger(action);
}
}, timeout);
var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
$(document).bind(mousewheelevt,
function(e)
{
var evt = window.event || e //equalize event object
evt = evt.originalEvent ? evt.originalEvent : evt; //convert to originalEvent if possible
var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta //check for detail first, because it is used by Opera and FF
if(delta > 0)
{
scrollup();
}
else
{
scrolldown();
}
}
);