4

I have my code, and I cannot understand how can I detect end of scroll (when it gets to the bottom of the element).

document.getElementById('list').addEventListener('wheel', function () {
    function load() {
       var fragment = document.createDocumentFragment();

        for (var i = 0; i < 10; i++) {
            var p = document.createElement('p');
            p.appendChild(document.createTextNode(i));
            fragment.appendChild(p);
       }
       return fragment;
     }

      // if ( scroll end ) { load new content 
         this.appendChild(load());
      //} 
  }, false);

Is there any best practice/algorithm for this task?

Anders Abel
  • 67,989
  • 17
  • 150
  • 217

1 Answers1

1

Ok, i get what you wanted to achieve now...

so basically, there are those two properties .scrollHeight which is the height of the content in a scrollable element, and .scrollTop which returns the current scroll position of the scrollable content.

when you scroll to the end of the scrollable element, its scrolltop + height will be equal to its scrollheight, so your condition should look like this:

var myList = document.getElementById('list');
if (myList.scrollTop + myList.offsetHeight == myList.scrollHeight) {
    this.appendChild(load());
}

here is your Fixed Fiddle

Banana
  • 7,424
  • 3
  • 22
  • 43