Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Infinite scrolling with IntersectionObserver API

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 600
    Comment on it

    JavaScript is the programming language of HTML and the Web.It plays a very important role in web. Its client side scripting language.JavaScript is also defined as a lightweight, interpreted programming language.
    It is complimentary to and integrated with Java.


    JavaScript is used for many purposes in web as its having many inbuilt functions that is used according to the requirements by the developers.

    Infinite scroll is also a feature of JavaScript that also used in most of the websites that are having many pages data .Infinite scroll helps to load the website fast if the website have very much data in that particular page because infinite scroll loads some amount of data that should be visible and loads the another data whenever user scrolls at the end of the page.

    1. <ul class="listview"></ul>
    2. <span class="polyfill-notice">polyfill</span>
    3. <script>
    4. if (!('IntersectionObserver' in window)) //IntersectionObserver api function to load content
    5. document.body.classList.add('polyfill');
    6. </script>
    7.  
    8.  
    9. <script src="https://cdn.rawgit.com/surma-dump/IntersectionObserver/polyfill/polyfill/intersectionobserver-polyfill.js"></script>
    10.  
    11. //include polyfill javascript by which could load content in scroll if necessary
    12.  
    1. var
    2. pageSize = 10,
    3. sentinel = {
    4. el: null,
    5. set: function(el) {
    6. this.el = el;
    7. this.el.classList.add('sentinel');
    8. sentinelObserver.observe(this.el);
    9. },
    10. unset: function() {
    11. if (!this.el)
    12. return;
    13. sentinelObserver.unobserve(this.el);
    14. this.el.classList.remove('sentinel');
    15. this.el = null;
    16. }
    17. },
    18. sentinelListener = function(entries) {
    19. console.log(entries);
    20. sentinel.unset();
    21. listView.classList.add('loading');
    22. nextPage().then(() => {
    23. updateSentinel();
    24. listView.classList.remove('loading');
    25. });
    26. },
    27. updateSentinel = function() {
    28. sentinel.set(listView.children[listView.children.length - pageSize]);
    29. },
    30. nextPage = function() {
    31. return loadNextPage().then((items) => {
    32. listView.insertAdjacentHTML(
    33. 'beforeend',
    34. items.map(item => `
    35. <li class="listview__item">
    36. <span class="listview__item__id">${item.id}</span>
    37. <div class="listview__item__content">
    38. <p>${item.text}</p>
    39. </div>
    40. </li>
    41. `).join('')
    42. );
    43. });
    44. },
    45. loadNextPage = (function() { //content function that will load when scroll and used in html
    46. var
    47. pageNumber = 0,
    48. placeholders = [
    49. 'Lorem Ipsum. Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum',
    50. 'Lorem Ipsum. Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLoLorem Ipsum. Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum
    51.  
    52. ]
    53. ;
    54. return function () {
    55. console.log(`Loading page #${pageNumber}`);
    56. return new Promise(function(resolve, reject) {
    57. var items = [];
    58. for (var id = pageNumber*pageSize, lastId = id + pageSize - 1; id <= lastId; ++id) {
    59. items.push({
    60. id: id,
    61. text: placeholders[getRandomInRange(0, placeholders.length - 1)]
    62. })
    63. }
    64. pageNumber++;
    65. setTimeout(function() { resolve(items); }, getRandomInRange(500, 4000));
    66. });
    67. }
    68. })(),
    69. getRandomInRange = function(min, max) {
    70. return Math.floor(Math.random() * (max - min + 1)) + min;
    71. },
    72. listView = document.querySelector('.listview'),
    73. sentinelObserver = new IntersectionObserver(sentinelListener, {threshold: 1}) // threshold represents the elements area that needs to become visible to trigger the callback
    74. ;
    75. nextPage().then(() => {
    76. nextPage().then(updateSentinel);
    77. });
    78.  

    Intersection Observer API is used for  trying to figure out the best way to detect when any element enters the viewport.Intersection Observer API is useful in  infinite scrolling, lazy loading images etc.

    The script we have included polyfill in html and javascript by which  we could load the data if only ii is necessary.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: