javascript 尝试使用 jquery.mousewheel 触发滚动事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13015305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:38:57  来源:igfitidea点击:

Trying to trigger scroll event by using jquery.mousewheel

javascripttriggersscrollmousewheel

提问by Dygestor

I'm having some trouble with triggering a scroll event by using jquery.mousewheel. I want to "expand" the scroll event for #bio-content-container to trigger when scrolling over #bio-slider-container. I'm using following code:

我在使用 jquery.mousewheel 触发滚动事件时遇到了一些麻烦。我想“扩展”#bio-content-container 的滚动事件以在滚动#bio-slider-container 时触发。我正在使用以下代码:

var lastScrollTop = 0;      
$("#bio-content-container").scroll(function () {
    var st = $(this).scrollTop();
    if (st > lastScrollTop){
        scroll('Down');
    } else {
        scroll('Up');
    }
    lastScrollTop = st;
});

jQuery(function($) {
    $('#bio-slider-container')
        .bind('mousewheel', function(event, delta) {
            $("#bio-content-container").trigger('scroll');
            return false;
    });
});

I don't want to trigger scroll on #bio-slider-container, so that's why I'm using mousewheel. Any help would be much appreciated :)

我不想在#bio-slider-container 上触发滚动,所以这就是我使用鼠标滚轮的原因。任何帮助将非常感激 :)

采纳答案by Slippery Pete

If I understand correctly, you want to scroll the contents of #bio-content-container when you use the mousewheel over #bio-slider-container. You might want to check out the jquery.scrollToplugin. This code works for me (without seeing your HTML):

如果我理解正确,当您在#bio-slider-container 上使用鼠标滚轮时,您想滚动#bio-content-container 的内容。您可能想查看jquery.scrollTo插件。此代码适用于我(没有看到您的 HTML):

$(document).ready(function () {

    $('#bio-slider-container').bind('mousewheel', function (event, delta) {
        var content = $("#bio-content-container");
        if (delta < 0) {
            content.scrollTo("+=10");
        } else {
            content.scrollTo("-=10");
        }
    });

});