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
8.2 KiB

10 years ago
  1. !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