diff --git a/README.md b/README.md index da055e2..8d859ba 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Sticky-js is a library for sticky elements written in vanilla javascript. With t ## Features - Written in vanilla javascript, no dependencies needed -- Lightweight (minified: ~4.82kb, gzipped: ~1.46kb) +- Lightweight (minified: ~4.68kb, gzipped: ~1.40kb) - It can be sticky to the entire page or to selected parent container - No additional CSS needed diff --git a/bower.json b/bower.json index b45f838..0390518 100644 --- a/bower.json +++ b/bower.json @@ -18,5 +18,5 @@ "test", "tests" ], - "version": "1.1.2" + "version": "1.1.3" } diff --git a/dist/sticky.compile.js b/dist/sticky.compile.js index 184535a..cf5bd89 100644 --- a/dist/sticky.compile.js +++ b/dist/sticky.compile.js @@ -4,7 +4,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons * Sticky.js * Library for sticky elements written in vanilla javascript. With this library you can easily set sticky elements on your website. It's also responsive. * - * @version 1.1.2 + * @version 1.1.3 * @author Rafal Galus * @website https://rgalus.github.io/sticky-js/ * @repo https://github.com/rgalus/sticky-js @@ -27,7 +27,7 @@ var Sticky = function () { this.selector = selector; this.elements = []; - this.version = '1.1.2'; + this.version = '1.1.3'; this.vp = this.getViewportSize(); this.scrollTop = this.getScrollTopPosition(); diff --git a/dist/sticky.min.js b/dist/sticky.min.js index 9c10ab8..6a6d0fe 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.1.2",this.vp=this.getViewportSize(),this.scrollTop=this.getScrollTopPosition(),this.options={marginTop:e.marginTop||0,stickyFor:e.stickFor||0,stickyClass:e.stickyClass||null},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.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)}),this.activate(t)},t.prototype.activate=function(t){var i=void 0,e=void 0;i=t.sticky.container.offsetHeight,this.addStyle(t,{position:"fixed"}),e=t.sticky.container.offsetHeight,this.removeStyle(t,["position"]),e>=i&&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.onScrollEvents=function(t){this.scrollTop=this.getScrollTopPosition(),t.sticky.active&&this.setPosition(t)},t.prototype.setPosition=function(t){this.removeStyle(t,["position","width","top","left"]),this.vp.heightt.sticky.rect.top-t.sticky.marginTop?(this.addStyle(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.addStyle(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.addStyle(t,{top:t.sticky.marginTop+"px"}))):(t.sticky.stickyClass&&t.classList.remove(t.sticky.stickyClass),this.removeStyle(t,["position","width","top","left"])))},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.setPosition(i)})},t.prototype.getStickyContainer=function(t){for(var i=t;!i.hasAttribute("data-sticky-container")&&i!==document.querySelector("body");)i=i.parentNode;return i},t.prototype.getRectangle=function(t){this.removeStyle(t,["position","width","top","left"]);var i=0,e=0,s=0,o=0;s=t.offsetWidth,o=t.offsetHeight;do i+=t.offsetTop||0,e+=t.offsetLeft||0,t=t.offsetParent;while(t);return{top:i,left:e,width:s,height:o}},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.getScrollTopPosition=function(){return(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]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,t),this.selector=i,this.elements=[],this.version="1.1.3",this.vp=this.getViewportSize(),this.scrollTop=this.getScrollTopPosition(),this.options={marginTop:e.marginTop||0,stickyFor:e.stickFor||0,stickyClass:e.stickyClass||null},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.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)}),this.activate(t)},t.prototype.activate=function(t){var i=void 0,e=void 0;i=t.sticky.container.offsetHeight,this.addStyle(t,{position:"fixed"}),e=t.sticky.container.offsetHeight,this.removeStyle(t,["position"]),e>=i&&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.onScrollEvents=function(t){this.scrollTop=this.getScrollTopPosition(),t.sticky.active&&this.setPosition(t)},t.prototype.setPosition=function(t){this.removeStyle(t,["position","width","top","left"]),this.vp.heightt.sticky.rect.top-t.sticky.marginTop?(this.addStyle(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.addStyle(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.addStyle(t,{top:t.sticky.marginTop+"px"}))):(t.sticky.stickyClass&&t.classList.remove(t.sticky.stickyClass),this.removeStyle(t,["position","width","top","left"])))},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.setPosition(i)})},t.prototype.getStickyContainer=function(t){for(var i=t;!i.hasAttribute("data-sticky-container")&&i!==document.querySelector("body");)i=i.parentNode;return i},t.prototype.getRectangle=function(t){this.removeStyle(t,["position","width","top","left"]);var i=0,e=0,s=0,o=0;s=t.offsetWidth,o=t.offsetHeight;do i+=t.offsetTop||0,e+=t.offsetLeft||0,t=t.offsetParent;while(t);return{top:i,left:e,width:s,height:o}},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.getScrollTopPosition=function(){return(window.pageYOffset||document.scrollTop)-(document.clientTop||0)||0},t.prototype.forEach=function(t,i){for(var e=0,s=t.length;e * @website https://rgalus.github.io/sticky-js/ * @repo https://github.com/rgalus/sticky-js @@ -21,7 +21,7 @@ class Sticky { this.selector = selector; this.elements = []; - this.version = '1.1.2'; + this.version = '1.1.3'; this.vp = this.getViewportSize(); this.scrollTop = this.getScrollTopPosition();