Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. function scroll_item() {
  2. var tracked_item = $(".list_item").last(); // grabs the last "item" with the class list_item
  3.  
  4. // $(window).outerHeight() is the height of the browser window
  5. // tracked_item.offset().top is the vertical amount of pixels from(below) the top of the web page document
  6.  
  7. var scrollPosition = tracked_item.offset().top - $(window).outerHeight(); // figures out how many pixels below the
  8. // initial browser window is the tracked item. So if the window is 700px vertically and the item that is being tracked
  9. // is 1200px from the very top of the document.. then the user needs to scroll 500 more pixels to reach the
  10. // tracked item.
  11.  
  12. $(window).scroll(function(event) {
  13. // applies an event listener to the document window
  14. // $(window).scrollTop() is how many pixels that are hidden(above) the element ( window in this case )
  15. if (scrollPosition > $(window).scrollTop()) {
  16. // checks if there are more pixels left to scroll(scrollPosition)
  17. // then what has been scrolled already($(window).scrollTop())
  18. return; // since the amount left is greater then what has been scrolled... do nothing, tracked_item isn't visible yet.
  19. }
  20. // finally the amount of pixels scrolled ($(window).scrollTop()) is larger then the amount needed to scroll
  21. // to the tracked_item.
  22. $(this).off(event); // temporarily remove this scroll event from the window.
  23. add_to_items(); // lazy load and append new item. If desired, at the end of the add_to_items make
  24. // sure to re-call scroll_item() to reattached the event listener to the window.
  25. });
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement