diff --git a/dist/sticky.compile.js b/dist/sticky.compile.js index 62bf02f..3c7ea92 100644 --- a/dist/sticky.compile.js +++ b/dist/sticky.compile.js @@ -31,6 +31,7 @@ var Sticky = function () { this.vp = this.getViewportSize(); this.body = document.querySelector('body'); + this.firstRender = false; this.options = { wrap: options.wrap || false, @@ -60,13 +61,19 @@ var Sticky = function () { // wait for page to be fully loaded var pageLoaded = setInterval(function () { - if (document.readyState === 'complete') { - clearInterval(pageLoaded); + if (document.readyState === 'interactive' && _this.firstRender === false) { + _this.firstRender = true; var elements = document.querySelectorAll(_this.selector); _this.forEach(elements, function (element) { return _this.renderElement(element); }); + return; + } + + if (document.readyState === 'complete') { + clearInterval(pageLoaded); + _this.update(); } }, 10); }; @@ -137,6 +144,7 @@ var Sticky = function () { Sticky.prototype.activate = function activate(element) { if (element.sticky.rect.top + element.sticky.rect.height < element.sticky.container.rect.top + element.sticky.container.rect.height && element.sticky.stickyFor < this.vp.width && !element.sticky.active) { element.sticky.active = true; + element.setAttribute('data-sticky-rendered', ''); } if (this.elements.indexOf(element) < 0) { @@ -322,12 +330,20 @@ var Sticky = function () { Sticky.prototype.update = function update() { var _this5 = this; - this.forEach(this.elements, function (element) { - element.sticky.rect = _this5.getRectangle(element); - element.sticky.container.rect = _this5.getRectangle(element.sticky.container); + var elements = document.querySelectorAll(this.selector); + this.forEach(elements, function (element) { + // if this element has not already been rendered + if (element.getAttribute('data-sticky-rendered') === null) { + element.setAttribute('data-sticky-rendered', ''); + _this5.elements.push(element); + _this5.renderElement(element); + } else { + element.sticky.rect = _this5.getRectangle(element); + element.sticky.container.rect = _this5.getRectangle(element.sticky.container); - _this5.activate(element); - _this5.setPosition(element); + _this5.activate(element); + _this5.setPosition(element); + } }); }; @@ -458,7 +474,9 @@ var Sticky = function () { if (typeof exports !== 'undefined') { module.exports = factory; } else if (typeof define === 'function' && define.amd) { - define([], factory); + define([], function () { + return factory; + }); } else { root.Sticky = factory; } diff --git a/dist/sticky.min.js b/dist/sticky.min.js index fb319c1..92baab3 100644 --- a/dist/sticky.min.js +++ b/dist/sticky.min.js @@ -1 +1 @@ -function _classCallCheck(t,i){if(!(t instanceof i))throw new TypeError("Cannot call a class as a function")}var Sticky=function(){function t(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,t),this.selector=i,this.elements=[],this.version="1.2.0",this.vp=this.getViewportSize(),this.body=document.querySelector("body"),this.options={wrap:e.wrap||!1,marginTop:e.marginTop||0,stickyFor:e.stickyFor||0,stickyClass:e.stickyClass||null,stickyContainer:e.stickyContainer||"body"},this.updateScrollTopPosition=this.updateScrollTopPosition.bind(this),this.updateScrollTopPosition(),window.addEventListener("load",this.updateScrollTopPosition),window.addEventListener("scroll",this.updateScrollTopPosition),this.run()}return t.prototype.run=function(){var t=this,i=setInterval(function(){if("complete"===document.readyState){clearInterval(i);var e=document.querySelectorAll(t.selector);t.forEach(e,function(i){return t.renderElement(i)})}},10)},t.prototype.renderElement=function(t){var i=this;t.sticky={},t.sticky.active=!1,t.sticky.marginTop=parseInt(t.getAttribute("data-margin-top"))||this.options.marginTop,t.sticky.stickyFor=parseInt(t.getAttribute("data-sticky-for"))||this.options.stickyFor,t.sticky.stickyClass=t.getAttribute("data-sticky-class")||this.options.stickyClass,t.sticky.wrap=!!t.hasAttribute("data-sticky-wrap")||this.options.wrap,t.sticky.stickyContainer=this.options.stickyContainer,t.sticky.container=this.getStickyContainer(t),t.sticky.container.rect=this.getRectangle(t.sticky.container),t.sticky.rect=this.getRectangle(t),"img"===t.tagName.toLowerCase()&&(t.onload=function(){return t.sticky.rect=i.getRectangle(t)}),t.sticky.wrap&&this.wrapElement(t),this.activate(t)},t.prototype.wrapElement=function(t){t.insertAdjacentHTML("beforebegin",""),t.previousSibling.appendChild(t)},t.prototype.activate=function(t){t.sticky.rect.top+t.sticky.rect.height=t.sticky.container.rect.top+t.sticky.container.rect.height||t.sticky.stickyFor>=this.vp.width&&t.sticky.active)&&(t.sticky.active=!1),this.setPosition(t)},t.prototype.initScrollEvents=function(t){var i=this;t.sticky.scrollListener=function(){return i.onScrollEvents(t)},window.addEventListener("scroll",t.sticky.scrollListener)},t.prototype.destroyScrollEvents=function(t){window.removeEventListener("scroll",t.sticky.scrollListener)},t.prototype.onScrollEvents=function(t){t.sticky.active&&this.setPosition(t)},t.prototype.setPosition=function(t){this.css(t,{position:"",width:"",top:"",left:""}),this.vp.heightt.sticky.rect.top-t.sticky.marginTop?(this.css(t,{position:"fixed",width:t.sticky.rect.width+"px",left:t.sticky.rect.left+"px"}),this.scrollTop+t.sticky.rect.height+t.sticky.marginTop>t.sticky.container.rect.top+t.sticky.container.offsetHeight?(t.sticky.stickyClass&&t.classList.remove(t.sticky.stickyClass),this.css(t,{top:t.sticky.container.rect.top+t.sticky.container.offsetHeight-(this.scrollTop+t.sticky.rect.height)+"px"})):(t.sticky.stickyClass&&t.classList.add(t.sticky.stickyClass),this.css(t,{top:t.sticky.marginTop+"px"}))):(t.sticky.stickyClass&&t.classList.remove(t.sticky.stickyClass),this.css(t,{position:"",width:"",top:"",left:""}),t.sticky.wrap&&this.css(t.parentNode,{display:"",width:"",height:""})))},t.prototype.update=function(){var t=this;this.forEach(this.elements,function(i){i.sticky.rect=t.getRectangle(i),i.sticky.container.rect=t.getRectangle(i.sticky.container),t.activate(i),t.setPosition(i)})},t.prototype.destroy=function(){var t=this;this.forEach(this.elements,function(i){t.destroyResizeEvents(i),t.destroyScrollEvents(i),delete i.sticky})},t.prototype.getStickyContainer=function(t){for(var i=t.parentNode;!i.hasAttribute("data-sticky-container")&&!i.parentNode.querySelector(t.sticky.stickyContainer)&&i!==this.body;)i=i.parentNode;return i},t.prototype.getRectangle=function(t){this.css(t,{position:"",width:"",top:"",left:""});var i=Math.max(t.offsetWidth,t.clientWidth,t.scrollWidth),e=Math.max(t.offsetHeight,t.clientHeight,t.scrollHeight),s=0,o=0;do s+=t.offsetTop||0,o+=t.offsetLeft||0,t=t.offsetParent;while(t);return{top:s,left:o,width:i,height:e}},t.prototype.getViewportSize=function(){return{width:Math.max(document.documentElement.clientWidth,window.innerWidth||0),height:Math.max(document.documentElement.clientHeight,window.innerHeight||0)}},t.prototype.updateScrollTopPosition=function(){this.scrollTop=(window.pageYOffset||document.scrollTop)-(document.clientTop||0)||0},t.prototype.forEach=function(t,i){for(var e=0,s=t.length;e0&&void 0!==arguments[0]?arguments[0]:"",i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,t),this.selector=e,this.elements=[],this.version="1.2.0",this.vp=this.getViewportSize(),this.body=document.querySelector("body"),this.firstRender=!1,this.options={wrap:i.wrap||!1,marginTop:i.marginTop||0,stickyFor:i.stickyFor||0,stickyClass:i.stickyClass||null,stickyContainer:i.stickyContainer||"body"},this.updateScrollTopPosition=this.updateScrollTopPosition.bind(this),this.updateScrollTopPosition(),window.addEventListener("load",this.updateScrollTopPosition),window.addEventListener("scroll",this.updateScrollTopPosition),this.run()}return t.prototype.run=function(){var t=this,e=setInterval(function(){if("interactive"===document.readyState&&!1===t.firstRender){t.firstRender=!0;var i=document.querySelectorAll(t.selector);return void t.forEach(i,function(e){return t.renderElement(e)})}"complete"===document.readyState&&(clearInterval(e),t.update())},10)},t.prototype.renderElement=function(t){var e=this;t.sticky={},t.sticky.active=!1,t.sticky.marginTop=parseInt(t.getAttribute("data-margin-top"))||this.options.marginTop,t.sticky.stickyFor=parseInt(t.getAttribute("data-sticky-for"))||this.options.stickyFor,t.sticky.stickyClass=t.getAttribute("data-sticky-class")||this.options.stickyClass,t.sticky.wrap=!!t.hasAttribute("data-sticky-wrap")||this.options.wrap,t.sticky.stickyContainer=this.options.stickyContainer,t.sticky.container=this.getStickyContainer(t),t.sticky.container.rect=this.getRectangle(t.sticky.container),t.sticky.rect=this.getRectangle(t),"img"===t.tagName.toLowerCase()&&(t.onload=function(){return t.sticky.rect=e.getRectangle(t)}),t.sticky.wrap&&this.wrapElement(t),this.activate(t)},t.prototype.wrapElement=function(t){t.insertAdjacentHTML("beforebegin",""),t.previousSibling.appendChild(t)},t.prototype.activate=function(t){t.sticky.rect.top+t.sticky.rect.height=t.sticky.container.rect.top+t.sticky.container.rect.height||t.sticky.stickyFor>=this.vp.width&&t.sticky.active)&&(t.sticky.active=!1),this.setPosition(t)},t.prototype.initScrollEvents=function(t){var e=this;t.sticky.scrollListener=function(){return e.onScrollEvents(t)},window.addEventListener("scroll",t.sticky.scrollListener)},t.prototype.destroyScrollEvents=function(t){window.removeEventListener("scroll",t.sticky.scrollListener)},t.prototype.onScrollEvents=function(t){t.sticky.active&&this.setPosition(t)},t.prototype.setPosition=function(t){this.css(t,{position:"",width:"",top:"",left:""}),this.vp.heightt.sticky.rect.top-t.sticky.marginTop?(this.css(t,{position:"fixed",width:t.sticky.rect.width+"px",left:t.sticky.rect.left+"px"}),this.scrollTop+t.sticky.rect.height+t.sticky.marginTop>t.sticky.container.rect.top+t.sticky.container.offsetHeight?(t.sticky.stickyClass&&t.classList.remove(t.sticky.stickyClass),this.css(t,{top:t.sticky.container.rect.top+t.sticky.container.offsetHeight-(this.scrollTop+t.sticky.rect.height)+"px"})):(t.sticky.stickyClass&&t.classList.add(t.sticky.stickyClass),this.css(t,{top:t.sticky.marginTop+"px"}))):(t.sticky.stickyClass&&t.classList.remove(t.sticky.stickyClass),this.css(t,{position:"",width:"",top:"",left:""}),t.sticky.wrap&&this.css(t.parentNode,{display:"",width:"",height:""})))},t.prototype.update=function(){var t=this,e=document.querySelectorAll(this.selector);this.forEach(e,function(e){null===e.getAttribute("data-sticky-rendered")?(e.setAttribute("data-sticky-rendered",""),t.elements.push(e),t.renderElement(e)):(e.sticky.rect=t.getRectangle(e),e.sticky.container.rect=t.getRectangle(e.sticky.container),t.activate(e),t.setPosition(e))})},t.prototype.destroy=function(){var t=this;this.forEach(this.elements,function(e){t.destroyResizeEvents(e),t.destroyScrollEvents(e),delete e.sticky})},t.prototype.getStickyContainer=function(t){for(var e=t.parentNode;!e.hasAttribute("data-sticky-container")&&!e.parentNode.querySelector(t.sticky.stickyContainer)&&e!==this.body;)e=e.parentNode;return e},t.prototype.getRectangle=function(t){this.css(t,{position:"",width:"",top:"",left:""});var e=Math.max(t.offsetWidth,t.clientWidth,t.scrollWidth),i=Math.max(t.offsetHeight,t.clientHeight,t.scrollHeight),s=0,c=0;do{s+=t.offsetTop||0,c+=t.offsetLeft||0,t=t.offsetParent}while(t);return{top:s,left:c,width:e,height:i}},t.prototype.getViewportSize=function(){return{width:Math.max(document.documentElement.clientWidth,window.innerWidth||0),height:Math.max(document.documentElement.clientHeight,window.innerHeight||0)}},t.prototype.updateScrollTopPosition=function(){this.scrollTop=(window.pageYOffset||document.scrollTop)-(document.clientTop||0)||0},t.prototype.forEach=function(t,e){for(var i=0,s=t.length;i { - if (document.readyState === 'complete') { - clearInterval(pageLoaded); + if (document.readyState === 'interactive' && this.firstRender === false) { + this.firstRender = true; const elements = document.querySelectorAll(this.selector); this.forEach(elements, (element) => this.renderElement(element)); + return; + } + + if (document.readyState === 'complete') { + clearInterval(pageLoaded); + this.update(); } }, 10); } @@ -123,6 +130,7 @@ class Sticky { && !element.sticky.active ) { element.sticky.active = true; + element.setAttribute( 'data-sticky-rendered', '' ); } if (this.elements.indexOf(element) < 0) { @@ -305,12 +313,20 @@ class Sticky { * @function */ update() { - this.forEach(this.elements, (element) => { - element.sticky.rect = this.getRectangle(element); - element.sticky.container.rect = this.getRectangle(element.sticky.container); + const elements = document.querySelectorAll(this.selector); + this.forEach(elements, (element) => { + // if this element has not already been rendered + if ( element.getAttribute( 'data-sticky-rendered' ) === null ) { + element.setAttribute( 'data-sticky-rendered', '' ); + this.elements.push( element ); + this.renderElement(element); + } else { + element.sticky.rect = this.getRectangle(element); + element.sticky.container.rect = this.getRectangle(element.sticky.container); - this.activate(element); - this.setPosition(element); + this.activate(element); + this.setPosition(element); + } }); }