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 badges4 Answers
Reset to default 2try 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.