You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
9.0 KiB

  1. !function(a,b,c,d){"use strict";a.fn.sticky=function(c){var e,f=a(this),g=f.selector||"",h=(new Date).getTime(),i=[],j=arguments[0],k="string"==typeof j,l=[].slice.call(arguments,1);return f.each(function(){var f,m,n,o=a.extend(!0,{},a.fn.sticky.settings,c),p=o.className,q=o.namespace,r=o.error,s="."+q,t="module-"+q,u=a(this),v=a(b),w=u.offsetParent(),x=a(o.scrollContext),y=(u.selector||"",u.data(t)),z=b.requestAnimationFrame||b.mozRequestAnimationFrame||b.webkitRequestAnimationFrame||b.msRequestAnimationFrame||function(a){setTimeout(a,0)},A=this;n={initialize:function(){return f=o.context?a(o.context):w,0===f.size()?void n.error(r.invalidContext,o.context,u):(n.verbose("Initializing sticky",o,w),n.save.positions(),n.is.hidden()&&n.error(r.visible,u),n.cache.element.height>n.cache.context.height?(n.reset(),void n.error(r.elementSize,u)):(v.on("resize"+s,n.event.resize),x.on("scroll"+s,n.event.scroll),n.observeChanges(),void n.instantiate()))},instantiate:function(){n.verbose("Storing instance of module",n),y=n,u.data(t,n)},destroy:function(){n.verbose("Destroying previous module"),n.reset(),v.off("resize"+s,n.event.resize),x.off("scroll"+s,n.event.scroll),u.removeData(t)},observeChanges:function(){var a=f[0];MutationObserver!==d&&(m=new MutationObserver(function(){clearTimeout(n.timer),n.timer=setTimeout(function(){n.verbose("DOM tree modified, updating sticky menu"),n.refresh()},200)}),m.observe(A,{childList:!0,subtree:!0}),m.observe(a,{childList:!0,subtree:!0}),n.debug("Setting up mutation observer",m))},event:{resize:function(){z(function(){n.refresh(),n.stick()})},scroll:function(){z(function(){n.stick(),a.proxy(o.onScroll,A)()})}},refresh:function(b){n.reset(),b&&(w=u.offsetParent()),n.save.positions(),n.stick(),a.proxy(o.onReposition,A)()},supports:{sticky:function(){{var b=a("<div/>");b.get()}return b.addClass(p.supported),b.css("position").match("sticky")}},save:{scroll:function(a){n.lastScroll=a},positions:function(){var a={height:v.height()},b={margin:{top:parseInt(u.css("margin-top"),10),bottom:parseInt(u.css("margin-bottom"),10)},offset:u.offset(),width:u.outerWidth(),height:u.outerHeight()},c={offset:f.offset(),height:f.outerHeight()};n.cache={fits:b.height<a.height,window:{height:a.height},element:{margin:b.margin,top:b.offset.top-b.margin.top,left:b.offset.left,width:b.width,height:b.height,bottom:b.offset.top+b.height},context:{top:c.offset.top,height:c.height,bottom:c.offset.top+c.height}},n.set.containerSize(),n.set.size(),n.stick(),n.debug("Caching element positions",n.cache)}},get:{direction:function(a){var b="down";return a=a||x.scrollTop(),n.lastScroll!==d&&(n.lastScroll<a?b="down":n.lastScroll>a&&(b="up")),b},scrollChange:function(a){return a=a||x.scrollTop(),n.lastScroll?a-n.lastScroll:0},currentElementScroll:function(){return n.is.top()?Math.abs(parseInt(u.css("top"),10))||0:Math.abs(parseInt(u.css("bottom"),10))||0},elementScroll:function(a){a=a||x.scrollTop();var b,c=n.cache.element,d=n.cache.window,e=n.get.scrollChange(a),f=c.height-d.height+o.offset,g=n.get.currentElementScroll(),h=g+e;return b=n.cache.fits||0>h?0:h>f?f:h}},remove:{offset:function(){u.css("margin-top","")}},set:{offset:function(){n.verbose("Setting offset on element",o.offset),u.css("margin-top",o.offset)},containerSize:function(){var a=w.get(0).tagName;"HTML"===a||"body"==a?(n.error(r.container,a,u),w=u.offsetParent()):(n.debug("Settings container size",n.cache.context.height),w.height(n.cache.context.height))},scroll:function(a){n.debug("Setting scroll on element",a),n.is.top()&&u.css("bottom","").css("top",-a),n.is.bottom()&&u.css("top","").css("bottom",a)},size:function(){0!==n.cache.element.height&&0!==n.cache.element.width&&u.css({width:n.cache.element.width,height:n.cache.element.height})}},is:{top:function(){return u.hasClass(p.top)},bottom:function(){return u.hasClass(p.bottom)},initialPosition:function(){return!n.is.fixed()&&!n.is.bound()},hidden:function(){return!u.is(":visible")},bound:function(){return u.hasClass(p.bound)},fixed:function(){return u.hasClass(p.fixed)}},stick:function(){var a=n.cache,b=a.fits,c=a.