Guest User

Untitled

a guest
Dec 12th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. /**
  2. * Hides all elements that are outside the scrolling viewport to improve
  3. * performance.
  4. *
  5. * You'll need to call this function whenever dom nodes are inserted or removed
  6. * in the list.
  7. *
  8. * You'll need to declare the "unload" class in your css file as following:
  9. * .unload { visibility: hidden; }
  10. *
  11. * @param Node scrollEl the dom node that has the scrollbars
  12. * @param NodeList scrollItems a list of the scrolling items
  13. * @param Number itemHeight the height of each item in the list
  14. */
  15. function hideElementsWhenScrolling(scrollEl, scrollItems, itemHeight) {
  16. var i,
  17. len = scrollItems.length,
  18. item,
  19. containerTop = scrollEl.offsetTop,
  20. containerHeight = scrollEl.offsetHeight,
  21. itemsPerPage = Math.ceil(containerHeight / itemHeight),
  22. scrollTop = scrollEl.scrollTop,
  23. lastFirstEl = 0;
  24.  
  25. for (i = 0; i < len; i++) {
  26. item = scrollItems[i];
  27. item._offsetTop = item.offsetTop - containerTop;
  28. item._offsetBottom = item._offsetTop - containerTop + itemHeight;
  29.  
  30. if (item._offsetBottom < scrollTop || item._offsetTop > scrollTop + containerHeight) {
  31. item.classList.add('unload');
  32. } else {
  33. item.classList.remove('unload');
  34. }
  35. }
  36.  
  37. function doScrollShow(scrollTop) {
  38. var firstEl = Math.floor(scrollTop / itemHeight),
  39. isGoingUp = firstEl >= lastFirstEl,
  40. i;
  41.  
  42. if (isGoingUp) {
  43. for (i = lastFirstEl; i < firstEl; i++) {
  44. scrollItems[i].classList.add('unload');
  45. }
  46.  
  47. for (i = Math.min(lastFirstEl + itemsPerPage, len - 1); i < Math.min(firstEl + itemsPerPage + 1, len); i++) {
  48. scrollItems[i].classList.remove('unload');
  49. }
  50. } else {
  51. for (i = lastFirstEl; i >= firstEl; i--) {
  52. scrollItems[i].classList.remove('unload');
  53. }
  54.  
  55. for (i = Math.min(lastFirstEl + itemsPerPage, len - 1); i > Math.min(firstEl + itemsPerPage, len); i--) {
  56. scrollItems[i].classList.add('unload');
  57. }
  58. }
  59.  
  60. lastFirstEl = firstEl;
  61. }
  62.  
  63. function showTo(scrollTop, newScrollTop) {
  64. var firstEl = Math.floor(scrollTop / itemHeight),
  65. isGoingUp = newScrollTop > scrollTop,
  66. lastEl,
  67. i;
  68.  
  69. if (isGoingUp) {
  70. lastEl = Math.min(Math.floor(newScrollTop / itemHeight) + itemsPerPage * 2, len);
  71. for (i = firstEl; i < lastEl; i++) {
  72. scrollItems[i].classList.remove('unload');
  73. }
  74. } else {
  75. lastEl = Math.max(Math.floor(newScrollTop / itemHeight) - itemsPerPage * 2, 0);
  76. for (i = lastEl; i < firstEl; i++) {
  77. scrollItems[i].classList.remove('unload');
  78. }
  79. }
  80.  
  81. lastFirstEl = firstEl;
  82. }
  83.  
  84. function onscroll() {
  85. doScrollShow(scrollEl.scrollTop);
  86. }
  87.  
  88. scrollEl.onscroll = onscroll;
  89.  
  90. scrollEl.ontouchmove = function (e) {
  91. scrollEl._lastY = scrollEl._y;
  92. scrollEl._lastT = scrollEl._t;
  93. scrollEl._y = e.pageY;
  94. scrollEl._t = e.timeStamp;
  95. };
  96.  
  97. scrollEl.ontouchend = function (e) {
  98. var deltaT = scrollEl._t - scrollEl._lastT,
  99. deltaS = scrollEl._y - scrollEl._lastY,
  100. V = deltaS / deltaT,
  101. K = 278.4434180138568,
  102. endPos = K * -V + scrollEl.scrollTop;
  103.  
  104. if (!isNaN(K * -V + scrollEl.scrollTop)) {
  105. showTo(scrollEl.scrollTop, endPos);
  106. }
  107. };
  108. }
Add Comment
Please, Sign In to add comment