javascript - execute jquery code when user scrolls down a certain amount (pixels) -
i execute below code when user scrolls position on page; instance 600px; down (from top). detect 600px down in pixels.
// $(document).ready(function() { $(".slidedown").one('load', function () { $(this).slidedown(200); }).each(function() { if(this.complete) $(this).trigger('load'); }); // }); this did not work:
$(document).scroll(function(){ if (document.scrollheight > 600) { $(".slidedown").one('load', function () { $(this).slidedown(200); }).each(function() { if(this.complete) $(this).trigger('load'); }); } });
simply set threshold of # of pixels before fire event , function triggered when user scrolls or down n pixels
https://jsfiddle.net/7daffjh8/7/
var scrollamount = 0; var pixelstonotify = 200; $(document).scroll(function() { var distance = $(window).scrolltop(); console.log(distance, scrollamount); if(distance - scrollamount > pixelstonotify){ alert('user scrolled down event'); scrollamount = distance; } if(distance < scrollamount - pixelstonotify){ alert('user scrolled event'); scrollamount = distance; } });
Comments
Post a Comment