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.
 
 
 

1 lines
8.2 KiB

!function(a,b,c,d){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 u.is(":visible")||n.error(r.visible,u),f=o.context?a(o.context):w,0===f.size()?void n.error(r.invalidContext,o.context,u):(n.supports.sticky(),v.on("resize"+s,n.event.resize),x.on("scroll"+s,n.event.scroll),n.verbose("Initializing sticky",o,w),n.save.positions(),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",n.event.resize).off("scroll",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(n.refresh)},scroll:function(){z(function(){n.stick(),a.proxy(o.onScroll,A)()})}},refresh:function(b){n.reset(),b&&(w=u.offsetParent()),n.save.positions(),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<v.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},currentOffset:function(){return n.is.top()?parseInt(u.css("top"),10)||0:parseInt(u.css("bottom"),10)||0},offsetChange:function(a){return a=a||x.scrollTop(),n.lastScroll?Math.abs(a-n.lastScroll):0},newOffset:function(a){a=a||x.scrollTop();var b=n.get.currentOffset(),c=n.get.offsetChange(a);return Math.abs(b-c)}},set:{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))},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)},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.element,d=a.window,e=a.context,f=x.scrollTop(),g={top:f+o.offset,bottom:f+d.height+o.offset},h=n.get.direction(f),i=(n.get.currentOffset(),n.get.newOffset(f)),j=g.bottom>c.top+c.height,k=a.element.height+g.top;n.save.scroll(f),0!==c.height&&(n.is.fixed()?b?(n.is.top()&&(g.top<c.top?n.unfix():k>e.bottom&&(n.debug("Top attached rail has reached bottom of container"),n.bindBottom())),n.is.bottom()&&(g.bottom-c.height<c.top?n.unfix():g.bottom>e.bottom&&(n.debug("Bottom attached rail has reached bottom of container"),n.bindBottom())),k>e.bottom&&n.bindBottom()):g.bottom>e.bottom?n.bindBottom():j?(n.is.top()&&"down"==h&&(n.debug("Stuck content at bottom edge"),i>=c.height-d.height?(u.css("top",""),n.stickBottom()):u.css("top",-i)),n.is.bottom()&&"up"==h&&(n.debug("Stuck content at top edge"),i>=c.height-d.height?(u.css("bottom",""),n.stickTop()):u.css("bottom",-i))):n.unfix():(g.top+c.height>e.bottom&&n.bindBottom(),b?n.is.bottom()?o.pushing?n.is.bound()&&g.bottom<e.bottom&&n.stickBottom():n.is.bound()&&g.top<e.bottom-c.height&&n.stickTop():g.top>=c.top&&g.top<e.bottom-c.height&&n.stickTop():j&&g.bottom<e.bottom&&n.stickBottom()))},bindTop:function(){n.debug("Binding element to top of parent container"),u.css("left","").removeClass(p.fixed).removeClass(p.bottom).addClass(p.bound).addClass(p.top),a.proxy(o.onTop,A)(),a.proxy(o.onUnstick,A)()},bindBottom:function(){n.debug("Binding element to bottom of parent container"),u.css("left","").removeClass(p.fixed).removeClass(p.top).addClass(p.bound).addClass(p.bottom),a.proxy(o.onBottom,A)(),a.proxy(o.onUnstick,A)()},stickTop:function(){n.debug("Fixing element to top of page"),u.css("left",n.cache.element.left).removeClass(p.bound).removeClass(p.bottom).addClass(p.fixed).addClass(p.top),a.proxy(o.onStick,A)()},stickBottom:function(){n.debug("Sticking element to bottom of page"),u.css("left",n.cache.element.left).removeClass(p.bound).removeClass(p.top).addClass(p.fixed).addClass(p.bottom),a.proxy(o.onStick,A)()},unbind:function(){n.debug("Removing absolute position on element"),u.removeClass(p.bound).removeClass(p.top).removeClass(p.bottom)},unfix:function(){n.debug("Removing fixed position on element"),u.removeClass(p.fixed).removeClass(p.top).removeClass(p.bottom),a.proxy(o.onUnstick,this)()},reset:function(){n.debug("Reseting elements position"),n.unbind(),n.unfix(),n.resetCSS()},resetCSS:function(){u.css({top:"",bottom:"",width:"",height:""}),w.css({height:""})},setting:function(b,c){if(a.isPlainObject(b))a.extend(!0,o,b);else{if(c===d)return o[b];o[b]=c}},internal:function(b,c){if(a.isPlainObject(b))a.extend(!0,n,b);else{if(c===d)return n[b];n[b]=c}},debug:function(){o.debug&&(o.performance?n.performance.log(arguments):(n.debug=Function.prototype.bind.call(console.info,console,o.name+":"),n.debug.apply(console,arguments)))},verbose:function(){o.verbose&&o.debug&&(o.performance?n.performance.log(arguments):(n.verbose=Function.prototype.bind.call(console.info,console,o.name+":"),n.verbose.apply(console,arguments)))},error:function(){n.error=Function.prototype.bind.call(console.error,console,o.name+":"),n.error.apply(console,arguments)},performance:{log:function(a){var b,c,d;o.performance&&(b=(new Date).getTime(),d=h||b,c=b-d,h=b,i.push({Element:A,Name:a[0],Arguments:[].slice.call(a,1)||"","Execution Time":c})),clearTimeout(n.performance.timer),n.performance.timer=setTimeout(n.performance.display,0)},display:function(){var b=o.name+":",c=0;h=!1,clearTimeout(n.performance.timer),a.each(i,function(a,b){c+=b["Execution Time"]}),b+=" "+c+"ms",g&&(b+=" '"+g+"'"),(console.group!==d||console.table!==d)&&i.length>0&&(console.groupCollapsed(b),console.table?console.table(i):a.each(i,function(a,b){console.log(b.Name+": "+b["Execution Time"]+"ms")}),console.groupEnd()),i=[]}},invoke:function(b,c,f){var g,h,i,j=y;return c=c||l,f=A||f,"string"==typeof b&&j!==d&&(b=b.split(/[\. ]/),g=b.length-1,a.each(b,function(c,e){var f=c!=g?e+b[c+1].charAt(0).toUpperCase()+b[c+1].slice(1):b;if(a.isPlainObject(j[f])&&c!=g)j=j[f];else{if(j[f]!==d)return h=j[f],!1;if(!a.isPlainObject(j[e])||c==g)return j[e]!==d?(h=j[e],!1):!1;j=j[e]}})),a.isFunction(h)?i=h.apply(f,c):h!==d&&(i=h),a.isArray(e)?e.push(i):e!==d?e=[e,i]:i!==d&&(e=i),h}},k?(y===d&&n.initialize(),n.invoke(j)):(y!==d&&n.destroy(),n.initialize())}),e!==d?e:this},a.fn.sticky.settings={name:"Sticky",namespace:"sticky",verbose:!1,debug:!1,performance:!1,pushing:!1,context:!1,scrollContext:b,offset:0,onReposition:function(){},onScroll:function(){},onStick:function(){},onUnstick:function(){},onTop:function(){},onBottom:function(){},error:{container:"Sticky element must be inside a relative container",visible:"Element is hidden, you must call refresh after element becomes visible",method:"The method you called is not defined.",invalidContext:"Context specified does not exist"},className:{bound:"bound",fixed:"fixed",supported:"native",top:"top",bottom:"bottom"}}}(jQuery,window,document);