jpscontracting/static/scripts/sticky_header.js
2017-01-17 00:42:30 -05:00

1 line
5.6 KiB
JavaScript

(function(e){e.isScrollToFixed=function(t){return!!e(t).data("ScrollToFixed")};e.ScrollToFixed=function(t,n){function g(){s.trigger("preUnfixed.ScrollToFixed");T();s.trigger("unfixed.ScrollToFixed");p=-1;l=s.offset().top;c=s.offset().left;if(r.options.offsets){c+=s.offset().left-s.position().left}if(h==-1){h=c}o=s.css("position");i=true;if(r.options.bottom!=-1){s.trigger("preFixed.ScrollToFixed");S();s.trigger("fixed.ScrollToFixed")}}function y(){var e=r.options.limit;if(!e)return 0;if(typeof e==="function"){return e.apply(s)}return e}function b(){return o==="fixed"}function w(){return o==="absolute"}function E(){return!(b()||w())}function S(){if(!b()){d.css({display:s.css("display"),"z-index":1});cssOptions={"z-index":r.options.zIndex,position:"fixed",top:r.options.bottom==-1?C():"",bottom:r.options.bottom==-1?"":r.options.bottom,"margin-left":"0px"};if(!r.options.dontSetWidth){cssOptions["width"]=s.css("width")}s.css(cssOptions);s.addClass(r.options.baseClassName);if(r.options.className){s.addClass(r.options.className)}o="fixed"}}function x(){var e=y();var t=c;if(r.options.removeOffsets){t="";e=e-l}cssOptions={position:"absolute",top:e,left:t,"margin-left":"0px",bottom:""};if(!r.options.dontSetWidth){cssOptions["width"]=s.css("width")}s.css(cssOptions);o="absolute"}function T(){if(!E()){p=-1;d.css("display","none");s.css({"z-index":f,width:"",position:u,left:"",top:a,"margin-left":""});s.removeClass("scroll-to-fixed-fixed");if(r.options.className){s.removeClass(r.options.className)}o=null}}function N(e){if(e!=p){s.css("left",c-e);p=e}}function C(){var e=r.options.marginTop;if(!e)return 0;if(typeof e==="function"){return e.apply(s)}return e}function k(){if(!e.isScrollToFixed(s))return;var t=i;if(!i){g()}else if(E()){l=s.offset().top;c=s.offset().left}var n=e(window).scrollLeft();var o=e(window).scrollTop();var a=y();if(r.options.minWidth&&e(window).width()<r.options.minWidth){if(!E()||!t){A();s.trigger("preUnfixed.ScrollToFixed");T();s.trigger("unfixed.ScrollToFixed")}}else if(r.options.maxWidth&&e(window).width()>r.options.maxWidth){if(!E()||!t){A();s.trigger("preUnfixed.ScrollToFixed");T();s.trigger("unfixed.ScrollToFixed")}}else if(r.options.bottom==-1){if(a>0&&o>=a-C()){if(!w()||!t){A();s.trigger("preAbsolute.ScrollToFixed");x();s.trigger("unfixed.ScrollToFixed")}}else if(o>=l-C()){if(!b()||!t){A();s.trigger("preFixed.ScrollToFixed");S();p=-1;s.trigger("fixed.ScrollToFixed")}N(n)}else{if(!E()||!t){A();s.trigger("preUnfixed.ScrollToFixed");T();s.trigger("unfixed.ScrollToFixed")}}}else{if(a>0){if(o+e(window).height()-s.outerHeight(true)>=a-(C()||-L())){if(b()){A();s.trigger("preUnfixed.ScrollToFixed");if(u==="absolute"){x()}else{T()}s.trigger("unfixed.ScrollToFixed")}}else{if(!b()){A();s.trigger("preFixed.ScrollToFixed");S()}N(n);s.trigger("fixed.ScrollToFixed")}}else{N(n)}}}function L(){if(!r.options.bottom)return 0;return r.options.bottom}function A(){var e=s.css("position");if(e=="absolute"){s.trigger("postAbsolute.ScrollToFixed")}else if(e=="fixed"){s.trigger("postFixed.ScrollToFixed")}else{s.trigger("postUnfixed.ScrollToFixed")}}var r=this;r.$el=e(t);r.el=t;r.$el.data("ScrollToFixed",r);var i=false;var s=r.$el;var o;var u;var a;var f;var l=0;var c=0;var h=-1;var p=-1;var d=null;var v;var m;var O=function(e){if(s.is(":visible")){i=false;k()}};var M=function(e){!!window.requestAnimationFrame?requestAnimationFrame(k):k()};var _=function(){var e=document.body;if(document.createElement&&e&&e.appendChild&&e.removeChild){var t=document.createElement("div");if(!t.getBoundingClientRect)return null;t.innerHTML="x";t.style.cssText="position:fixed;top:100px;";e.appendChild(t);var n=e.style.height,r=e.scrollTop;e.style.height="3000px";e.scrollTop=500;var i=t.getBoundingClientRect().top;e.style.height=n;var s=i===100;e.removeChild(t);e.scrollTop=r;return s}return null};var D=function(e){e=e||window.event;if(e.preventDefault){e.preventDefault()}e.returnValue=false};r.init=function(){r.options=e.extend({},e.ScrollToFixed.defaultOptions,n);f=s.css("z-index");r.$el.css("z-index",r.options.zIndex);d=e("<span />");o=s.css("position");u=s.css("position");a=s.css("top");if(E())r.$el.after(d);e(window).bind("resize.ScrollToFixed",O);e(window).bind("scroll.ScrollToFixed",M);if("ontouchmove"in window){e(window).bind("touchmove.ScrollToFixed",k)}if(r.options.preFixed){s.bind("preFixed.ScrollToFixed",r.options.preFixed)}if(r.options.postFixed){s.bind("postFixed.ScrollToFixed",r.options.postFixed)}if(r.options.preUnfixed){s.bind("preUnfixed.ScrollToFixed",r.options.preUnfixed)}if(r.options.postUnfixed){s.bind("postUnfixed.ScrollToFixed",r.options.postUnfixed)}if(r.options.preAbsolute){s.bind("preAbsolute.ScrollToFixed",r.options.preAbsolute)}if(r.options.postAbsolute){s.bind("postAbsolute.ScrollToFixed",r.options.postAbsolute)}if(r.options.fixed){s.bind("fixed.ScrollToFixed",r.options.fixed)}if(r.options.unfixed){s.bind("unfixed.ScrollToFixed",r.options.unfixed)}if(r.options.spacerClass){d.addClass(r.options.spacerClass)}s.bind("resize.ScrollToFixed",function(){d.height(s.height())});s.bind("scroll.ScrollToFixed",function(){s.trigger("preUnfixed.ScrollToFixed");T();s.trigger("unfixed.ScrollToFixed");k()});s.bind("detach.ScrollToFixed",function(t){D(t);s.trigger("preUnfixed.ScrollToFixed");T();s.trigger("unfixed.ScrollToFixed");e(window).unbind("resize.ScrollToFixed",O);e(window).unbind("scroll.ScrollToFixed",M);s.unbind(".ScrollToFixed");d.remove();r.$el.removeData("ScrollToFixed")});O()};r.init()};e.ScrollToFixed.defaultOptions={marginTop:0,limit:0,bottom:-1,zIndex:1e3,baseClassName:"scroll-to-fixed-fixed"};e.fn.scrollToFixed=function(t){return this.each(function(){new e.ScrollToFixed(this,t)})}})(jQuery)