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
7.6 KiB
1 lines
7.6 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=a.extend(!0,{},a.fn.sticky.settings,c),o=n.className,p=n.namespace,q=n.error,r="."+p,s="module-"+p,t=a(this),u=a(b),v=t.offsetParent(),w=(t.selector||"",t.data(s)),x=b.requestAnimationFrame||b.mozRequestAnimationFrame||b.webkitRequestAnimationFrame||b.msRequestAnimationFrame||function(a){setTimeout(a,0)},y=this;m={initialize:function(){return f=n.context?a(n.context):v,0===f.size()?void m.error(q.invalidContext,n.context,t):(m.supports.sticky(),u.on("resize"+r,m.event.resize).on("scroll"+r,m.event.scroll),m.verbose("Initializing sticky",n,v),m.save.positions(),void m.instantiate())},instantiate:function(){m.verbose("Storing instance of module",m),w=m,t.data(s,m)},destroy:function(){m.verbose("Destroying previous module"),m.reset(),u.off("resize",m.event.resize).off("scroll",m.event.scroll),t.removeData(s)},event:{resize:function(){x(m.refresh)},scroll:function(){x(function(){m.stick(),a.proxy(n.onScroll,y)()})}},refresh:function(b){m.reset(),b&&(v=t.offsetParent()),m.save.positions(),a.proxy(n.onReposition,y)()},supports:{sticky:function(){{var b=a("<div/>");b.get()}return b.addClass(o.supported),b.css("position").match("sticky")}},save:{scroll:function(a){m.lastScroll=a},positions:function(){var a={height:u.height()},b={margin:{top:parseInt(t.css("margin-top"),10),bottom:parseInt(t.css("margin-bottom"),10)},offset:t.offset(),width:t.outerWidth(),height:t.outerHeight()},c={offset:f.offset(),height:f.outerHeight()};m.cache={fits:b.height<u.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}},m.set.containerSize(),m.set.size(),m.stick(),m.debug("Caching element positions",m.cache)}},get:{direction:function(a){var b="down";return a=a||u.scrollTop(),m.lastScroll!==d&&(m.lastScroll<a?b="down":m.lastScroll>a&&(b="up")),b},currentOffset:function(){return m.is.top()?parseInt(t.css("top"),10)||0:parseInt(t.css("bottom"),10)||0},offsetChange:function(a){return a=a||u.scrollTop(),m.lastScroll?Math.abs(a-m.lastScroll):0},newOffset:function(a){a=a||u.scrollTop();var b=m.get.currentOffset(),c=m.get.offsetChange(a);return Math.abs(b-c)}},set:{containerSize:function(){var a=v.get(0).tagName;"HTML"===a||"body"==a?t.is(":visible")&&(m.error(q.container,a,t),v=t.offsetParent()):(m.debug("Settings container size",m.cache.context.height),v.height(m.cache.context.height))},size:function(){t.css({width:m.cache.element.width,height:m.cache.element.height})}},is:{top:function(){return t.hasClass(o.top)},bottom:function(){return t.hasClass(o.bottom)},bound:function(){return t.hasClass(o.bound)},fixed:function(){return t.hasClass(o.fixed)}},stick:function(){var a=m.cache,b=a.fits,c=a.element,d=a.window,e=a.context,f=u.scrollTop(),g={top:f+n.offset,bottom:f+d.height+n.offset},h=m.get.direction(f),i=(m.get.currentOffset(),m.get.newOffset(f)),j=g.bottom>c.top+c.height,k=a.element.height+g.top;m.save.scroll(f),m.is.fixed()?b?(m.is.top()&&(g.top<c.top?m.unfix():k>e.bottom&&(m.debug("Top attached rail has reached bottom of container"),m.bindBottom())),m.is.bottom()&&(g.bottom-c.height<c.top?m.unfix():g.bottom>e.bottom&&(m.debug("Bottom attached rail has reached bottom of container"),m.bindBottom())),k>e.bottom&&m.bindBottom()):g.bottom>e.bottom?m.bindBottom():j?(m.is.top()&&"down"==h&&(m.debug("Stuck content at bottom edge"),i>=c.height-d.height?(t.css("top",""),m.stickBottom()):t.css("top",-i)),m.is.bottom()&&"up"==h&&(m.debug("Stuck content at top edge"),i>=c.height-d.height?(t.css("bottom",""),m.stickTop()):t.css("bottom",-i))):m.unfix():(g.top+c.height>e.bottom&&m.bindBottom(),b?m.is.bottom()?n.pushing?m.is.bound()&&g.bottom<e.bottom&&m.stickBottom():m.is.bound()&&g.top<e.bottom-c.height&&m.stickTop():g.top>=c.top&&g.top<e.bottom-c.height&&m.stickTop():j&&g.bottom<e.bottom&&m.stickBottom())},bindTop:function(){m.debug("Binding element to top of parent container"),t.css("left","").removeClass(o.fixed).removeClass(o.bottom).addClass(o.bound).addClass(o.top),a.proxy(n.onTop,y)(),a.proxy(n.onUnstick,y)()},bindBottom:function(){m.debug("Binding element to bottom of parent container"),t.css("left","").removeClass(o.fixed).removeClass(o.top).addClass(o.bound).addClass(o.bottom),a.proxy(n.onBottom,y)(),a.proxy(n.onUnstick,y)()},stickTop:function(){m.debug("Fixing element to top of page"),t.css("left",m.cache.element.left).removeClass(o.bound).removeClass(o.bottom).addClass(o.fixed).addClass(o.top),a.proxy(n.onStick,y)()},stickBottom:function(){m.debug("Sticking element to bottom of page"),t.css("left",m.cache.element.left).removeClass(o.bound).removeClass(o.top).addClass(o.fixed).addClass(o.bottom),a.proxy(n.onStick,y)()},unbind:function(){m.debug("Removing absolute position on element"),t.removeClass(o.bound).removeClass(o.top).removeClass(o.bottom)},unfix:function(){m.debug("Removing fixed position on element"),t.removeClass(o.fixed).removeClass(o.top).removeClass(o.bottom),a.proxy(n.onUnstick,this)()},reset:function(){m.debug("Reseting elements position"),m.unbind(),m.unfix(),m.resetCSS()},resetCSS:function(){t.css({top:"",bottom:"",width:"",height:""})},setting:function(b,c){if(a.isPlainObject(b))a.extend(!0,n,b);else{if(c===d)return n[b];n[b]=c}},internal:function(b,c){if(a.isPlainObject(b))a.extend(!0,m,b);else{if(c===d)return m[b];m[b]=c}},debug:function(){n.debug&&(n.performance?m.performance.log(arguments):(m.debug=Function.prototype.bind.call(console.info,console,n.name+":"),m.debug.apply(console,arguments)))},verbose:function(){n.verbose&&n.debug&&(n.performance?m.performance.log(arguments):(m.verbose=Function.prototype.bind.call(console.info,console,n.name+":"),m.verbose.apply(console,arguments)))},error:function(){m.error=Function.prototype.bind.call(console.error,console,n.name+":"),m.error.apply(console,arguments)},performance:{log:function(a){var b,c,d;n.performance&&(b=(new Date).getTime(),d=h||b,c=b-d,h=b,i.push({Element:y,Name:a[0],Arguments:[].slice.call(a,1)||"","Execution Time":c})),clearTimeout(m.performance.timer),m.performance.timer=setTimeout(m.performance.display,100)},display:function(){var b=n.name+":",c=0;h=!1,clearTimeout(m.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=w;return c=c||l,f=y||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?(w===d&&m.initialize(),m.invoke(j)):(w!==d&&m.destroy(),m.initialize())}),e!==d?e:this},a.fn.sticky.settings={name:"Sticky",namespace:"sticky",verbose:!0,debug:!1,performance:!0,pushing:!1,context:!1,offset:0,onReposition:function(){},onScroll:function(){},onStick:function(){},onUnstick:function(){},onTop:function(){},onBottom:function(){},error:{container:"Sticky element must be inside a relative container",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);
|