function _classcallcheck(t, i) { if (!(t instanceof i)) throw new typeerror("cannot call a class as a function") } function _defineproperties(t, i) { for (var e = 0; e < i.length; e++) { var s = i[e]; s.enumerable = s.enumerable || !1, s.configurable = !0, "value"in s && (s.writable = !0), object.defineproperty(t, s.key, s) } } function _createclass(t, i, e) { return i && _defineproperties(t.prototype, i), e && _defineproperties(t, e), t } var sticky = function() { function e() { var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : "" , i = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {}; _classcallcheck(this, e), this.selector = t, this.elements = [], this.version = "1.3.0", this.vp = this.getviewportsize(), this.body = document.queryselector("body"), this.options = { wrap: i.wrap || !1, wrapwith: i.wrapwith || "", margintop: i.margintop || 0, marginbottom: i.marginbottom || 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 _createclass(e, [{ key: "run", value: function() { var i = this , e = setinterval(function() { if ("complete" === document.readystate) { clearinterval(e); var t = document.queryselectorall(i.selector); i.foreach(t, function(t) { return i.renderelement(t) }) } }, 10) } }, { key: "renderelement", value: 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.marginbottom = parseint(t.getattribute("data-margin-bottom")) || this.options.marginbottom, 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) } }, { key: "wrapelement", value: function(t) { t.insertadjacenthtml("beforebegin", t.getattribute("data-sticky-wrapwith") || this.options.wrapwith), t.previoussibling.appendchild(t) } }, { key: "activate", value: 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 = !0), this.elements.indexof(t) < 0 && this.elements.push(t), t.sticky.resizeevent || (this.initresizeevents(t), t.sticky.resizeevent = !0), t.sticky.scrollevent || (this.initscrollevents(t), t.sticky.scrollevent = !0), this.setposition(t) } }, { key: "initresizeevents", value: function(t) { var i = this; t.sticky.resizelistener = function() { return i.onresizeevents(t) } , window.addeventlistener("resize", t.sticky.resizelistener) } }, { key: "destroyresizeevents", value: function(t) { window.removeeventlistener("resize", t.sticky.resizelistener) } }, { key: "onresizeevents", value: function(t) { this.vp = this.getviewportsize(), t.sticky.rect = this.getrectangle(t), t.sticky.container.rect = this.getrectangle(t.sticky.container), 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 = !0 : (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) } }, { key: "initscrollevents", value: function(t) { var i = this; t.sticky.scrolllistener = function() { return i.onscrollevents(t) } , window.addeventlistener("scroll", t.sticky.scrolllistener) } }, { key: "destroyscrollevents", value: function(t) { window.removeeventlistener("scroll", t.sticky.scrolllistener) } }, { key: "onscrollevents", value: function(t) { t.sticky && t.sticky.active && this.setposition(t) } }, { key: "setposition", value: function(t) { this.css(t, { position: "", width: "", top: "", left: "" }), this.vp.height < t.sticky.rect.height || !t.sticky.active || (t.sticky.rect.width || (t.sticky.rect = this.getrectangle(t)), t.sticky.wrap && this.css(t.parentnode, { display: "block", width: t.sticky.rect.width + "px", height: t.sticky.rect.height + "px" }), 0 === t.sticky.rect.top && t.sticky.container === this.body ? (this.css(t, { position: "fixed", top: t.sticky.rect.top + "px", left: t.sticky.rect.left + "px", width: t.sticky.rect.width + "px" }), t.sticky.stickyclass && t.classlist.add(t.sticky.stickyclass)) : this.scrolltop > t.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.marginbottom ? (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 + t.sticky.marginbottom) + "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: "" }))) } }, { key: "update", value: function() { var i = this; this.foreach(this.elements, function(t) { t.sticky.rect = i.getrectangle(t), t.sticky.container.rect = i.getrectangle(t.sticky.container), i.activate(t), i.setposition(t) }) } }, { key: "destroy", value: function() { var i = this; window.removeeventlistener("load", this.updatescrolltopposition), window.removeeventlistener("scroll", this.updatescrolltopposition), this.foreach(this.elements, function(t) { i.destroyresizeevents(t), i.destroyscrollevents(t), delete t.sticky }) } }, { key: "getstickycontainer", value: 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 } }, { key: "getrectangle", value: function(t) { this.css(t, { position: "", width: "", top: "", left: "" }); for (var i = math.max(t.offsetwidth, t.clientwidth, t.scrollwidth), e = math.max(t.offsetheight, t.clientheight, t.scrollheight), s = 0, n = 0; s += t.offsettop || 0, n += t.offsetleft || 0, t = t.offsetparent; ) ; return { top: s, left: n, width: i, height: e } } }, { key: "getviewportsize", value: function() { return { width: math.max(document.documentelement.clientwidth, window.innerwidth || 0), height: math.max(document.documentelement.clientheight, window.innerheight || 0) } } }, { key: "updatescrolltopposition", value: function() { this.scrolltop = (window.pageyoffset || document.scrolltop) - (document.clienttop || 0) || 0 } }, { key: "foreach", value: function(t, i) { for (var e = 0, s = t.length; e < s; e++) i(t[e]) } }, { key: "css", value: function(t, i) { for (var e in i) i.hasownproperty(e) && (t.style[e] = i[e]) } }]), e }(); !function(t, i) { "undefined" != typeof exports ? module.exports = i : "function" == typeof define && define.amd ? define([], function() { return i }) : t.sticky = i }(this, sticky);