简体   繁体   中英

how to stop scroll event

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...

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)

This bit of code worked for me on a number of browsers.

 $(function() { var scrollTop = $(window).scrollTop(); $(window).scroll(function() { // this bit stops the page from being scrolled if ( * * * insert check * * * ) $(window).scrollTop(scrollTop); // update the new y value scrollTop = $(window).scrollTop(); ) }; }); 

try this:

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

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

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

or with JQuery:

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

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

The parameter you defined as e on your scroll callback handler is the event object.

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

Every event on Jquery has a method called preventDefault() , that according to the 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();

您可以尝试在自定义滚动函数结束时return false ,或者在调用自定义滚动函数之前调用preventDefault()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM