1 line
5.6 KiB
JavaScript
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
|