热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

除非页面移动,否则不会触发滚动事件-Scrolleventdoesn'tfireunlesspagemoves

Imlookingtogetaneventtofirewhenonescrollsupfrom$(window).scrollTop0.我正在寻找一个事件,

I'm looking to get an event to fire when one scrolls "up" from $(window).scrollTop == 0.

我正在寻找一个事件,当一个滚动“向上”从$(窗口)。scrollTop = = 0。

If you have the following code:

如果您有以下代码:

$(window).scroll(function(){
    console.log("scrolling")
});

On a page where the document $(window).scrollTop isn't changing, but this doesn't mean that there's no mouse scroll input. I want an event to fire on mouse scroll regardless if the page is moving or not.

在文档 <窗口高度的页面上,因为$(窗口)而不会触发事件。scrolltop没有变化,但这并不意味着没有鼠标滚动输入。无论页面是否移动,我都希望在鼠标滚屏上触发一个事件。

2 个解决方案

#1


10  

Seems like what you are looking for:

看起来你想要的是:

http://jsfiddle.net/n8eVQ/

http://jsfiddle.net/n8eVQ/

$(document).on('mousewheel DOMMouseScroll MozMousePixelScroll', function(event, delta) {
    console.log('mousewheel');
    //you could trigger window scroll handler
    $(window).triggerHandler('scroll');
});

Other way is to capture scroll event on modern browsers which support event capturing phase (IE>8). This can be used for any dynamic element. As jQuery doesn't implement capturing phase, you have to use Javascript addEventListener() method. Here an example implementing logic to get scrolling direction for a textarea:

另一种方法是在支持事件捕获阶段(即>8)的现代浏览器上捕获滚动事件。这可以用于任何动态元素。由于jQuery没有实现捕获阶段,所以必须使用Javascript addEventListener()方法。这里有一个实现逻辑以获取文本区域滚动方向的示例:

document.addEventListener('scroll', function (event) {
    var $elm = $(event.target);
    if ($elm.is('textarea')) { // or any other filtering condition
        // do some stuff
        var direction = $elm.scrollTop() > ($elm.data('scrollTop') || 0) ? "down" : "up";
        $elm.data('scrollTop', $elm.scrollTop());
        console.log('scrolling', direction);
    }
}, true);

-DEMO-

#2


0  

document.addEventListener('DOMMouseScroll', callbackFunction, false);

Solution for firefox; for other browsers see @roasted solution

firefox的解决方案;对于其他浏览器,请参见@焙烧。


推荐阅读
author-avatar
我是小钞票
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有