Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. class Layzr {
  2.  
  3. constructor(config){
  4. this._lastScroll = 0;
  5. this._ticking = false;
  6. config = config || {};
  7. this._optionsContainer = document.querySelector(config.container) || window;
  8. this._optionsSelector = config.selector || '[data-layzr]';
  9. this._optionsAttr = config.attr || 'data-lazyr';
  10. this._optionsAttrRetina = config.retinaAttr || 'data-layzr-retina';
  11. this._optionsAttrBg = config.bgAttr || 'data-layzr-bg';
  12. this._optionsAttrHidden = config.hiddenAttr || 'data-layzr-hidden';
  13. this._optionsThreshold = config.threshold || 0;
  14. this._optionsCallback = config.callback || null;
  15. this._retina = window.devicePixelRatio > 1;
  16. this._srcAttr = this._retina ? this._optionsAttrRetina : this._optionsAttr;
  17. this._nodes = document.querySelectorAll(this._optionsSelector);
  18. this._create();
  19. }
  20.  
  21. _requestScroll(){
  22. if ( this._optionsContainer === window ) {
  23. this._lastScroll = window.scrollY || window.pageYOffset;
  24. } else {
  25. this._lastScroll = this._optionsContainer.scrollTop + this._getOffset(this._optionsContainer);
  26. }
  27. this._requestTick();
  28. }
  29.  
  30. _requestTick() {
  31. this._ticking || (requestAnimationFrame(this.update.bind(this)), this._ticking = !0);
  32. }
  33.  
  34. _getOffset(a) {
  35. var b = 0;
  36. do isNaN(a.offsetTop) || (b += a.offsetTop);
  37. while (a = a.offsetParent);
  38. return b
  39. }
  40.  
  41. _getContainerHeight() {
  42. return this._optionsContainer.innerHeight || this._optionsContainer.offsetHeight;
  43. }
  44.  
  45. _create() {
  46. this._requestScroll();
  47. this._optionsContainer.addEventListener("scroll", this._requestScroll.bind(this), !1);
  48. this._optionsContainer.addEventListener("resize", this._requestScroll.bind(this), !1);
  49. }
  50.  
  51. _destroy() {
  52. this._optionsContainer.removeEventListener("scroll", this._requestScroll.bind(this), !1);
  53. this._optionsContainer.removeEventListener("resize", this._requestScroll.bind(this), !1);
  54. }
  55.  
  56. _inViewport(a) {
  57. var b = this._lastScroll,
  58. c = b + this._getContainerHeight(),
  59. d = this._getOffset(a),
  60. e = d + this._getContainerHeight(),
  61. f = this._optionsThreshold / 100 * window.innerHeight;
  62. return e >= b - f && c + f >= d && !a.hasAttribute(this._optionsAttrHidden);
  63. }
  64.  
  65. _reveal(a) {
  66. var b = a.getAttribute(this._srcAttr) || a.getAttribute(this._optionsAttr);
  67. a.hasAttribute(this._optionsAttrBg) ? a.style.backgroundImage = "url(" + b + ")" : a.setAttribute("src", b), "function" == typeof this._optionsCallback && this._optionsCallback.call(a), a.removeAttribute(this._optionsAttr), a.removeAttribute(this._optionsAttrRetina), a.removeAttribute(this._optionsAttrBg), a.removeAttribute(this._optionsAttrHidden);
  68. }
  69.  
  70. updateSelector() {
  71. this._nodes = document.querySelectorAll(this._optionsSelector);
  72. }
  73.  
  74. update() {
  75. for (var a = this._nodes.length, b = 0; a > b; b++) {
  76. var c = this._nodes[b];
  77. c.hasAttribute(this._optionsAttr) && this._inViewport(c) && this._reveal(c)
  78. }
  79. this._ticking = !1;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement