最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to stop scroll event - Stack Overflow

programmeradmin3浏览0评论

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 (infinity loop)

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./4m493q (infinity loop)

Share Improve this question asked Sep 12, 2014 at 15:22 feesarfeesar 5002 gold badges5 silver badges21 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

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./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();

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

发布评论

评论列表(0)

  1. 暂无评论