jquery - 如何判断滚动 Pane 在 JavaScript 中滚动的方式?

标签 jquery event-handling scroll

我有以下 jquery 来处理特定 div 上的滚动事件并写入一些内容:

$('#myDiv').scroll(function(eventData) {
  if(eventData.isGoingUp)
    $('#myDiv').prepend('<p>Going up.</p>');
  else
    $('#myDiv').append('<p>Going down.</p>');
});

显然,evt.isGoingUp 实际上并不存在。有没有什么东西可以实现这个逻辑?

最佳答案

希望这个解决方案对您有用...它将适用于类名为“scroll-track”的所有元素。您还必须为可滚动元素提供一个新属性: data-scroll='{"x":"0", "y":"0"}' 您可以在这里测试:http://jsfiddle.net/CgZDD/

-js-

$(document).ready(function(){
    // make sure overflow is set to 'scroll'
    $('.scroll-track').css({
        overflow: 'scroll'
    });

    $('.scroll-track').scroll(function() {
        var scrollData = $(this).data('scroll');

        if(scrollData.y > $(this).scrollTop()){
            $('#scrollDir').append($(this).attr('id') + ' up');
        }else if(scrollData.y != $(this).scrollTop()){
            $('#scrollDir').append($(this).attr('id') + ' down');
        }

        if(scrollData.x > $(this).scrollLeft()){
            $('#scrollDir').append($(this).attr('id') + ' left');
        }else if(scrollData.x != $(this).scrollLeft()){
            $('#scrollDir').append($(this).attr('id') + ' right');
        }

        $('#scrollDir').append('<br />');

        scrollData.x = $(this).scrollLeft();
        scrollData.y = $(this).scrollTop();
        $(this).data('scroll', scrollData);
    });
});

关于jquery - 如何判断滚动 Pane 在 JavaScript 中滚动的方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4307775/

相关文章:

javascript - 使用 jquery ajax 在 window.location 重定向后在下一页的 div 中写入 html 文本

ajax - IOS6 和 Safari 照片上传 - 文件 API + Canvas + jQuery Ajax 异步上传和调整文件大小

javascript - Selectable (jQuery UI) 的浏览器历史记录

javascript - 将当前元素作为参数传递给循环内的回调函数

javascript - 一次为一个段落制作动画

c - 为什么如果我禁用一个窗口,鼠标捕获会被释放,但如果我禁用其父窗口则不会释放鼠标捕获?

javascript - 设置状态 PubSub ReactJS 和 Rails

键盘可见时的 Swift iOS ScrollView

android - TextView 中值的垂直平滑滚动

android - 移动 chrome 在滚动时触发调整大小事件