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.
 
 
 

11 lines
9.2 KiB

/*
* # Semantic UI
* https://github.com/Semantic-Org/Semantic-UI
* http://beta.semantic-ui.com/
*
* Copyright 2014 Contributors
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/
!function(e,t,o,n){"use strict";e.fn.sticky=function(o){var i,s=e(this),r=s.selector||"",c=(new Date).getTime(),a=[],l=arguments[0],f="string"==typeof l,m=[].slice.call(arguments,1);return s.each(function(){var s,u,d,p=e.extend(!0,{},e.fn.sticky.settings,o),h=p.className,g=p.namespace,b=p.error,v="."+g,x="module-"+g,C=e(this),y=e(t),S=C.offsetParent(),k=e(p.scrollContext),T=(C.selector||"",C.data(x)),w=t.requestAnimationFrame||t.mozRequestAnimationFrame||t.webkitRequestAnimationFrame||t.msRequestAnimationFrame||function(e){setTimeout(e,0)},z=this;d={initialize:function(){return s=p.context?e(p.context):S,0===s.size()?void d.error(b.invalidContext,p.context,C):(d.verbose("Initializing sticky",p,S),d.save.positions(),d.is.hidden()&&d.error(b.visible,C),d.cache.element.height>d.cache.context.height?(d.reset(),void d.error(b.elementSize,C)):(y.on("resize"+v,d.event.resize),k.on("scroll"+v,d.event.scroll),d.observeChanges(),void d.instantiate()))},instantiate:function(){d.verbose("Storing instance of module",d),T=d,C.data(x,d)},destroy:function(){d.verbose("Destroying previous module"),d.reset(),y.off("resize"+v,d.event.resize),k.off("scroll"+v,d.event.scroll),C.removeData(x)},observeChanges:function(){var e=s[0];"MutationObserver"in t&&(u=new MutationObserver(function(){clearTimeout(d.timer),d.timer=setTimeout(function(){d.verbose("DOM tree modified, updating sticky menu"),d.refresh()},200)}),u.observe(z,{childList:!0,subtree:!0}),u.observe(e,{childList:!0,subtree:!0}),d.debug("Setting up mutation observer",u))},event:{resize:function(){w(function(){d.refresh(),d.stick()})},scroll:function(){w(function(){d.stick(),e.proxy(p.onScroll,z)()})}},refresh:function(t){d.reset(),t&&(S=C.offsetParent()),d.save.positions(),d.stick(),e.proxy(p.onReposition,z)()},supports:{sticky:function(){{var t=e("<div/>");t.get()}return t.addClass(h.supported),t.css("position").match("sticky")}},save:{scroll:function(e){d.lastScroll=e},positions:function(){var e={height:y.height()},t={margin:{top:parseInt(C.css("margin-top"),10),bottom:parseInt(C.css("margin-bottom"),10)},offset:C.offset(),width:C.outerWidth(),height:C.outerHeight()},o={offset:s.offset(),height:s.outerHeight()};d.cache={fits:t.height<e.height,window:{height:e.height},element:{margin:t.margin,top:t.offset.top-t.margin.top,left:t.offset.left,width:t.width,height:t.height,bottom:t.offset.top+t.height},context:{top:o.offset.top,height:o.height,bottom:o.offset.top+o.height}},d.set.containerSize(),d.set.size(),d.stick(),d.debug("Caching element positions",d.cache)}},get:{direction:function(e){var t="down";return e=e||k.scrollTop(),d.lastScroll!==n&&(d.lastScroll<e?t="down":d.lastScroll>e&&(t="up")),t},scrollChange:function(e){return e=e||k.scrollTop(),d.lastScroll?e-d.lastScroll:0},currentElementScroll:function(){return d.is.top()?Math.abs(parseInt(C.css("top"),10))||0:Math.abs(parseInt(C.css("bottom"),10))||0},elementScroll:function(e){e=e||k.scrollTop();var t,o=d.cache.element,n=d.cache.window,i=d.get.scrollChange(e),s=o.height-n.height+p.offset,r=d.get.currentElementScroll(),c=r+i;return t=d.cache.fits||0>c?0:c>s?s:c}},remove:{offset:function(){C.css("margin-top","")}},set:{offset:function(){d.verbose("Setting offset on element",p.offset),C.css("margin-top",p.offset)},containerSize:function(){var e=S.get(0).tagName;"HTML"===e||"body"==e?S=C.offsetParent():(d.debug("Settings container size",d.cache.context.height),S.height(d.cache.context.height))},scroll:function(e){d.debug("Setting scroll on element",e),d.is.top()&&C.css("bottom","").css("top",-e),d.is.bottom()&&C.css("top","").css("bottom",e)},size:function(){0!==d.cache.element.height&&0!==d.cache.element.width&&C.css({width:d.cache.element.width,height:d.cache.element.height})}},is:{top:function(){return C.hasClass(h.top)},bottom:function(){return C.hasClass(h.bottom)},initialPosition:function(){return!d.is.fixed()&&!d.is.bound()},hidden:function(){return!C.is(":visible")},bound:function(){return C.hasClass(h.bound)},fixed:function(){return C.hasClass(h.fixed)}},stick:function(){var e=d.cache,t=e.fits,o=e.element,n=e.window,i=e.context,s={top:k.scrollTop()+p.offset,bottom:k.scrollTop()+p.offset+n.height},r=(d.get.direction(s.top),d.get.elementScroll(s.top)),c=!t,a=0!==o.height;d.save.scroll(s.top),a&&(d.is.initialPosition()?s.top>=o.top&&(d.debug("Element passed, fixing element to page"),d.fixTop()):d.is.fixed()?d.is.top()?s.top<o.top?(d.debug("Fixed element reached top of container"),d.setInitialPosition()):o.height+s.top-r>i.bottom?(d.debug("Fixed element reached bottom of container"),d.bindBottom()):c&&d.set.scroll(r):d.is.bottom()&&(s.bottom-o.height<o.top?(d.debug("Bottom fixed rail has reached top of container"),d.setInitialPosition()):s.bottom>i.bottom?(d.debug("Bottom fixed rail has reached bottom of container"),d.bindBottom()):c&&d.set.scroll(r)):d.is.bottom()&&d.is.bottom()&&(p.pushing?d.is.bound()&&s.bottom<i.bottom&&(d.debug("Fixing bottom attached element to bottom of browser."),d.fixBottom()):d.is.bound()&&s.top<i.bottom-o.height&&(d.debug("Fixing bottom attached element to top of browser."),d.fixTop())))},bindTop:function(){d.debug("Binding element to top of parent container"),d.remove.offset(),C.css("left","").css("top","").css("bottom","").removeClass(h.fixed).removeClass(h.bottom).addClass(h.bound).addClass(h.top),e.proxy(p.onTop,z)(),e.proxy(p.onUnstick,z)()},bindBottom:function(){d.debug("Binding element to bottom of parent container"),d.remove.offset(),C.css("left","").css("top","").css("bottom","").removeClass(h.fixed).removeClass(h.top).addClass(h.bound).addClass(h.bottom),e.proxy(p.onBottom,z)(),e.proxy(p.onUnstick,z)()},setInitialPosition:function(){d.unfix(),d.unbind()},fixTop:function(){d.debug("Fixing element to top of page"),d.set.offset(),C.css("left",d.cache.element.left).removeClass(h.bound).removeClass(h.bottom).addClass(h.fixed).addClass(h.top),e.proxy(p.onStick,z)()},fixBottom:function(){d.debug("Sticking element to bottom of page"),d.set.offset(),C.css("left",d.cache.element.left).removeClass(h.bound).removeClass(h.top).addClass(h.fixed).addClass(h.bottom),e.proxy(p.onStick,z)()},unbind:function(){d.debug("Removing absolute position on element"),d.remove.offset(),C.removeClass(h.bound).removeClass(h.top).removeClass(h.bottom)},unfix:function(){d.debug("Removing fixed position on element"),d.remove.offset(),C.removeClass(h.fixed).removeClass(h.top).removeClass(h.bottom),e.proxy(p.onUnstick,this)()},reset:function(){d.debug("Reseting elements position"),d.unbind(),d.unfix(),d.resetCSS()},resetCSS:function(){C.css({top:"",bottom:"",width:"",height:""}),S.css({height:""})},setting:function(t,o){if(e.isPlainObject(t))e.extend(!0,p,t);else{if(o===n)return p[t];p[t]=o}},internal:function(t,o){if(e.isPlainObject(t))e.extend(!0,d,t);else{if(o===n)return d[t];d[t]=o}},debug:function(){p.debug&&(p.performance?d.performance.log(arguments):(d.debug=Function.prototype.bind.call(console.info,console,p.name+":"),d.debug.apply(console,arguments)))},verbose:function(){p.verbose&&p.debug&&(p.performance?d.performance.log(arguments):(d.verbose=Function.prototype.bind.call(console.info,console,p.name+":"),d.verbose.apply(console,arguments)))},error:function(){d.error=Function.prototype.bind.call(console.error,console,p.name+":"),d.error.apply(console,arguments)},performance:{log:function(e){var t,o,n;p.performance&&(t=(new Date).getTime(),n=c||t,o=t-n,c=t,a.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:z,"Execution Time":o})),clearTimeout(d.performance.timer),d.performance.timer=setTimeout(d.performance.display,0)},display:function(){var t=p.name+":",o=0;c=!1,clearTimeout(d.performance.timer),e.each(a,function(e,t){o+=t["Execution Time"]}),t+=" "+o+"ms",r&&(t+=" '"+r+"'"),(console.group!==n||console.table!==n)&&a.length>0&&(console.groupCollapsed(t),console.table?console.table(a):e.each(a,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),a=[]}},invoke:function(t,o,s){var r,c,a,l=T;return o=o||m,s=z||s,"string"==typeof t&&l!==n&&(t=t.split(/[\. ]/),r=t.length-1,e.each(t,function(o,i){var s=o!=r?i+t[o+1].charAt(0).toUpperCase()+t[o+1].slice(1):t;if(e.isPlainObject(l[s])&&o!=r)l=l[s];else{if(l[s]!==n)return c=l[s],!1;if(!e.isPlainObject(l[i])||o==r)return l[i]!==n?(c=l[i],!1):!1;l=l[i]}})),e.isFunction(c)?a=c.apply(s,o):c!==n&&(a=c),e.isArray(i)?i.push(a):i!==n?i=[i,a]:a!==n&&(i=a),c}},f?(T===n&&d.initialize(),d.invoke(l)):(T!==n&&d.destroy(),d.initialize())}),i!==n?i:this},e.fn.sticky.settings={name:"Sticky",namespace:"sticky",debug:!1,verbose:!1,performance:!1,pushing:!1,context:!1,scrollContext:t,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",elementSize:"Sticky element is larger than its container, cannot create sticky."},className:{bound:"bound",fixed:"fixed",supported:"native",top:"top",bottom:"bottom"}}}(jQuery,window,document);