javascript 如何停止滚动事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25811653/
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-28 05:03:59  来源:igfitidea点击:

how to stop scroll event

javascriptjqueryscroll

提问by feesar

Hello i have 2 functions , first detect sroll direction up/down , and launch other function (autoscrolling). How to force stop .scroll() function after call ? now it's look's like infinity loop...

您好,我有 2 个功能,首先检测向上/向下滚动方向,然后启动其他功能(自动滚动)。调用后如何强制停止 .scroll() 函数?现在它看起来像无限循环......

var pageScroll = function(){

    var iScrollPos = 0;

    $(window).scroll(function(e){

        var iCurScrollPos = $(this).scrollTop();

            if (iCurScrollPos > iScrollPos) {

                App.scrollto((currentPage+1));

            } else {

                App.scrollto((currentPage-1));

            }

        iScrollPos = iCurScrollPos;

    });

}

and called functions

和调用函数

    scrollto: function(page){

        var section_offset = $('section[data-page="'+page+'"]').offset().top;

        $('html, body').animate({ scrollTop: section_offset }, 'slow',function(){

            $('html, body').stop();
            currentPage = page;
            console.log(currentPage);

        });

    }

and console log screen http://prntscr.com/4m493q(infinity loop)

和控制台日志屏幕http://prntscr.com/4m493q(无限循环)

回答by Code-EZ

Use $(window).unbind('scroll');: http://api.jquery.com/unbind/

使用$(window).unbind('scroll');http: //api.jquery.com/unbind/

回答by rafsb

try this:

试试这个:

HTMLElement.prototype.stopScroll = function(){
    this.scroll({top:this.scrollTop+1});
}

by done, call whatever element you want, assuming you're scrolling < body >:

完成后,调用您想要的任何元素,假设您正在滚动 <body>:

document.getElementByTagName('body')[0].stopScroll();

or with JQuery:

或使用 JQuery:

$('body')[0].stopScroll();

回答by Hyman

You can try to return falseat the end of your custom scroll function, or call preventDefault()before calling your custom scroll function.

您可以尝试return false在自定义滚动函数的末尾,或preventDefault()在调用自定义滚动函数之前调用。

回答by LcSalazar

The parameter you defined as eon your scroll callback handler is the eventobject.

e在滚动回调处理程序中定义的参数是event对象。

$(window).scroll(function(e){

Every event on Jquery has a method called preventDefault(), that according to the Jquery Docs:

Jquery 上的每个事件都有一个名为 的方法preventDefault(),根据Jquery Docs

Description:If this method is called, the default action of the event will not be triggered.

说明:如果调用该方法,则不会触发事件的默认动作。

So, calling this method will keep your window of scrolling:

因此,调用此方法将使您的窗口保持滚动:

$(window).scroll(function(e){
    e.preventDefault();