2

using the following will execute action even if the user crolls from the end to the start. I want just when the user scrolls down vertically

if( $(window).scrollTop() + $(window).height() == $(document).height() ) {

     //do stuff
}

this is my code so far,

   function addEvent( obj, type, fn ) {


     if ( obj.attachEvent ) {
        obj['e'+type+fn] = fn;
       obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
       obj.attachEvent( 'on'+type, obj[type+fn] );
    } else
      obj.addEventListener( type, fn, false );

   }
  addEvent(window, 'scroll', function(event) {

    if( $(window).scrollTop() + $(window).height() == $(document).height() )  {
    load();
    }

});
N.G
  • 134
  • 1
  • 9
  • Works for me in chrome http://jsfiddle.net/j08691/pcbxmqzu/ – j08691 Sep 12 '14 at 16:40
  • http://stackoverflow.com/questions/4326845/how-can-i-determine-the-direction-of-a-jquery-scroll-event –  Sep 12 '14 at 16:49

3 Answers3

0
window.onscroll = function(e) {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
        // do stuff
    }
};
0

This works:

$(window).scroll(function() {   
   if($(window).scrollTop() + $(window).height() == $(document).height()) {
       alert("bottom!");
   }
});

or this:

$(window).scroll(function() {   
       if($(window).scrollTop() + $(window).height() > $(document).height()) {
           alert("bottom!");
       }
    });

See How to detect user reached bottom of page while scrolling

Also you may be interested in Infinite Scroll Paging using Jquery and Ajax

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • I tried this, but some how my action is executed whether I scroll down or up – N.G Sep 12 '14 at 16:40
  • 1
    well , that was my initial try...s mentionned in the post – N.G Sep 12 '14 at 16:44
  • @N.G see the second link i posted in the edit, i implemented it in my application and it works fine – Ehsan Sajjad Sep 12 '14 at 16:45
  • I'm also confused, it should have worked properly but it works fine with IE but on firefox and chrome it works when I scroll up and not down, I may be making something wrong, please see edit for the whole code. – N.G Sep 13 '14 at 13:27
0

try this

 $(window).scroll(function() {
    if ($(window).scrollTop() + $(window).height() > $(document).height()) {
        //your stuffs here
    }
});
Shreeraj Karki
  • 234
  • 2
  • 11