From 8133e0d71f905670b425ff799ba35aac87786399 Mon Sep 17 00:00:00 2001 From: Joseph Cava-Lynch Date: Fri, 11 Apr 2014 06:52:53 -0700 Subject: [PATCH] See #128, #130, #137: Merged pull requests. --- bower.json | 2 +- jquery-scrolltofixed-min.js | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/bower.json b/bower.json index 57f6eb0..a53125f 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "ScrollToFixed", - "version": "1.0.4", + "version": "1.0.5", "homepage": "http://bigspotteddog.github.io/ScrollToFixed/", "description": "jQuery plugin used to fix elements on the page (top, bottom, anywhere); however, it still allows the element to continue to move left or right with the horizontal scroll.", "main": "jquery-scrolltofixed.js", diff --git a/jquery-scrolltofixed-min.js b/jquery-scrolltofixed-min.js index c7652eb..8d55886 100644 --- a/jquery-scrolltofixed-min.js +++ b/jquery-scrolltofixed-min.js @@ -1,8 +1 @@ -/* - * ScrollToFixed - * https://github.com/bigspotteddog/ScrollToFixed - * - * Copyright (c) 2011 Joseph Cava-Lynch - * MIT license - */ -(function($){$.isScrollToFixed=function(el){return!!$(el).data("ScrollToFixed")};$.ScrollToFixed=function(el,options){var base=this;base.$el=$(el);base.el=el;base.$el.data("ScrollToFixed",base);var isReset=false;var target=base.$el;var position;var originalPosition;var originalOffsetTop;var originalZIndex;var offsetTop=0;var offsetLeft=0;var originalOffsetLeft=-1;var lastOffsetLeft=-1;var spacer=null;var spacerClass;var className;function resetScroll(){target.trigger("preUnfixed.ScrollToFixed");setUnfixed();target.trigger("unfixed.ScrollToFixed");lastOffsetLeft=-1;offsetTop=target.offset().top;offsetLeft=target.offset().left;if(base.options.offsets){offsetLeft+=target.offset().left-target.position().left}if(originalOffsetLeft==-1){originalOffsetLeft=offsetLeft}position=target.css("position");isReset=true;if(base.options.bottom!=-1){target.trigger("preFixed.ScrollToFixed");setFixed();target.trigger("fixed.ScrollToFixed")}}function getLimit(){var limit=base.options.limit;if(!limit)return 0;if(typeof limit==="function"){return limit.apply(target)}return limit}function isFixed(){return position==="fixed"}function isAbsolute(){return position==="absolute"}function isUnfixed(){return!(isFixed()||isAbsolute())}function setFixed(){if(!isFixed()){spacer.css({display:target.css("display"),width:target.outerWidth(true),height:target.outerHeight(true),"float":target.css("float")});cssOptions={"z-index":base.options.zIndex,position:"fixed",top:base.options.bottom==-1?getMarginTop():"",bottom:base.options.bottom==-1?"":base.options.bottom,"margin-left":"0px"};if(!base.options.dontSetWidth){cssOptions["width"]=target.width()}target.css(cssOptions);target.addClass(base.options.baseClassName);if(base.options.className){target.addClass(base.options.className)}position="fixed"}}function setAbsolute(){var top=getLimit();var left=offsetLeft;if(base.options.removeOffsets){left="";top=top-offsetTop}cssOptions={position:"absolute",top:top,left:left,"margin-left":"0px",bottom:""};if(!base.options.dontSetWidth){cssOptions["width"]=target.width()}target.css(cssOptions);position="absolute"}function setUnfixed(){if(!isUnfixed()){lastOffsetLeft=-1;spacer.css("display","none");target.css({"z-index":originalZIndex,width:"",position:originalPosition,left:"",top:originalOffsetTop,"margin-left":""});target.removeClass("scroll-to-fixed-fixed");if(base.options.className){target.removeClass(base.options.className)}position=null}}function setLeft(x){if(x!=lastOffsetLeft){target.css("left",offsetLeft-x);lastOffsetLeft=x}}function getMarginTop(){var marginTop=base.options.marginTop;if(!marginTop)return 0;if(typeof marginTop==="function"){return marginTop.apply(target)}return marginTop}function checkScroll(){if(!$.isScrollToFixed(target))return;var wasReset=isReset;if(!isReset){resetScroll()}var x=$(window).scrollLeft();var y=$(window).scrollTop();var limit=getLimit();if(base.options.minWidth&&$(window).width()base.options.maxWidth){if(!isUnfixed()||!wasReset){postPosition();target.trigger("preUnfixed.ScrollToFixed");setUnfixed();target.trigger("unfixed.ScrollToFixed")}}else if(base.options.bottom==-1){if(limit>0&&y>=limit-getMarginTop()){if(!isAbsolute()||!wasReset){postPosition();target.trigger("preAbsolute.ScrollToFixed");setAbsolute();target.trigger("unfixed.ScrollToFixed")}}else if(y>=offsetTop-getMarginTop()){if(!isFixed()||!wasReset){postPosition();target.trigger("preFixed.ScrollToFixed");setFixed();lastOffsetLeft=-1;target.trigger("fixed.ScrollToFixed")}setLeft(x)}else{if(!isUnfixed()||!wasReset){postPosition();target.trigger("preUnfixed.ScrollToFixed");setUnfixed();target.trigger("unfixed.ScrollToFixed")}}}else{if(limit>0){if(y+$(window).height()-target.outerHeight(true)>=limit-(getMarginTop()||-getBottom())){if(isFixed()){postPosition();target.trigger("preUnfixed.ScrollToFixed");if(originalPosition==="absolute"){setAbsolute()}else{setUnfixed()}target.trigger("unfixed.ScrollToFixed")}}else{if(!isFixed()){postPosition();target.trigger("preFixed.ScrollToFixed");setFixed()}setLeft(x);target.trigger("fixed.ScrollToFixed")}}else{setLeft(x)}}}function getBottom(){if(!base.options.bottom)return 0;return base.options.bottom}function postPosition(){var position=target.css("position");if(position=="absolute"){target.trigger("postAbsolute.ScrollToFixed")}else if(position=="fixed"){target.trigger("postFixed.ScrollToFixed")}else{target.trigger("postUnfixed.ScrollToFixed")}}var windowResize=function(event){if(target.is(":visible")){isReset=false;checkScroll()}};var windowScroll=function(event){checkScroll()};var isPositionFixedSupported=function(){var container=document.body;if(document.createElement&&container&&container.appendChild&&container.removeChild){var el=document.createElement("div");if(!el.getBoundingClientRect)return null;el.innerHTML="x";el.style.cssText="position:fixed;top:100px;";container.appendChild(el);var originalHeight=container.style.height,originalScrollTop=container.scrollTop;container.style.height="3000px";container.scrollTop=500;var elementTop=el.getBoundingClientRect().top;container.style.height=originalHeight;var isSupported=elementTop===100;container.removeChild(el);container.scrollTop=originalScrollTop;return isSupported}return null};var preventDefault=function(e){e=e||window.event;if(e.preventDefault){e.preventDefault()}e.returnValue=false};base.init=function(){base.options=$.extend({},$.ScrollToFixed.defaultOptions,options);originalZIndex=target.css("z-index");base.$el.css("z-index",base.options.zIndex);spacer=$("
");position=target.css("position");originalPosition=target.css("position");originalOffsetTop=target.css("top");if(isUnfixed())base.$el.after(spacer);$(window).bind("resize.ScrollToFixed",windowResize);$(window).bind("scroll.ScrollToFixed",windowScroll);if(base.options.preFixed){target.bind("preFixed.ScrollToFixed",base.options.preFixed)}if(base.options.postFixed){target.bind("postFixed.ScrollToFixed",base.options.postFixed)}if(base.options.preUnfixed){target.bind("preUnfixed.ScrollToFixed",base.options.preUnfixed)}if(base.options.postUnfixed){target.bind("postUnfixed.ScrollToFixed",base.options.postUnfixed)}if(base.options.preAbsolute){target.bind("preAbsolute.ScrollToFixed",base.options.preAbsolute)}if(base.options.postAbsolute){target.bind("postAbsolute.ScrollToFixed",base.options.postAbsolute)}if(base.options.fixed){target.bind("fixed.ScrollToFixed",base.options.fixed)}if(base.options.unfixed){target.bind("unfixed.ScrollToFixed",base.options.unfixed)}if(base.options.spacerClass){spacer.addClass(base.options.spacerClass)}target.bind("resize.ScrollToFixed",function(){spacer.height(target.height())});target.bind("scroll.ScrollToFixed",function(){target.trigger("preUnfixed.ScrollToFixed");setUnfixed();target.trigger("unfixed.ScrollToFixed");checkScroll()});target.bind("detach.ScrollToFixed",function(ev){preventDefault(ev);target.trigger("preUnfixed.ScrollToFixed");setUnfixed();target.trigger("unfixed.ScrollToFixed");$(window).unbind("resize.ScrollToFixed",windowResize);$(window).unbind("scroll.ScrollToFixed",windowScroll);target.unbind(".ScrollToFixed");spacer.remove();base.$el.removeData("ScrollToFixed")});windowResize()};base.init()};$.ScrollToFixed.defaultOptions={marginTop:0,limit:0,bottom:-1,zIndex:1e3,baseClassName:"scroll-to-fixed-fixed"};$.fn.scrollToFixed=function(options){return this.each(function(){new $.ScrollToFixed(this,options)})}})(jQuery); +(function(a){a.isScrollToFixed=function(b){return !!a(b).data("ScrollToFixed")};a.ScrollToFixed=function(d,i){var l=this;l.$el=a(d);l.el=d;l.$el.data("ScrollToFixed",l);var c=false;var G=l.$el;var H;var E;var e;var y;var D=0;var q=0;var j=-1;var f=-1;var t=null;var z;var g;function u(){G.trigger("preUnfixed.ScrollToFixed");k();G.trigger("unfixed.ScrollToFixed");f=-1;D=G.offset().top;q=G.offset().left;if(l.options.offsets){q+=(G.offset().left-G.position().left)}if(j==-1){j=q}H=G.css("position");c=true;if(l.options.bottom!=-1){G.trigger("preFixed.ScrollToFixed");w();G.trigger("fixed.ScrollToFixed")}}function n(){var I=l.options.limit;if(!I){return 0}if(typeof(I)==="function"){return I.apply(G)}return I}function p(){return H==="fixed"}function x(){return H==="absolute"}function h(){return !(p()||x())}function w(){if(!p()){t.css({display:G.css("display"),width:G.outerWidth(true),height:G.outerHeight(true),"float":G.css("float")});cssOptions={"z-index":l.options.zIndex,position:"fixed",top:l.options.bottom==-1?s():"",bottom:l.options.bottom==-1?"":l.options.bottom,"margin-left":"0px"};if(!l.options.dontSetWidth){cssOptions.width=G.width()}G.css(cssOptions);G.addClass(l.options.baseClassName);if(l.options.className){G.addClass(l.options.className)}H="fixed"}}function b(){var J=n();var I=q;if(l.options.removeOffsets){I="";J=J-D}cssOptions={position:"absolute",top:J,left:I,"margin-left":"0px",bottom:""};if(!l.options.dontSetWidth){cssOptions.width=G.width()}G.css(cssOptions);H="absolute"}function k(){if(!h()){f=-1;t.css("display","none");G.css({"z-index":y,width:"",position:E,left:"",top:e,"margin-left":""});G.removeClass("scroll-to-fixed-fixed");if(l.options.className){G.removeClass(l.options.className)}H=null}}function v(I){if(I!=f){G.css("left",q-I);f=I}}function s(){var I=l.options.marginTop;if(!I){return 0}if(typeof(I)==="function"){return I.apply(G)}return I}function A(){if(!a.isScrollToFixed(G)){return}var K=c;if(!c){u()}else{if(h()){D=G.offset().top;q=G.offset().left}}var I=a(window).scrollLeft();var L=a(window).scrollTop();var J=n();if(l.options.minWidth&&a(window).width()l.options.maxWidth){if(!h()||!K){o();G.trigger("preUnfixed.ScrollToFixed");k();G.trigger("unfixed.ScrollToFixed")}}else{if(l.options.bottom==-1){if(J>0&&L>=J-s()){if(!x()||!K){o();G.trigger("preAbsolute.ScrollToFixed");b();G.trigger("unfixed.ScrollToFixed")}}else{if(L>=D-s()){if(!p()||!K){o();G.trigger("preFixed.ScrollToFixed");w();f=-1;G.trigger("fixed.ScrollToFixed")}v(I)}else{if(!h()||!K){o();G.trigger("preUnfixed.ScrollToFixed");k();G.trigger("unfixed.ScrollToFixed")}}}}else{if(J>0){if(L+a(window).height()-G.outerHeight(true)>=J-(s()||-m())){if(p()){o();G.trigger("preUnfixed.ScrollToFixed");if(E==="absolute"){b()}else{k()}G.trigger("unfixed.ScrollToFixed")}}else{if(!p()){o();G.trigger("preFixed.ScrollToFixed");w()}v(I);G.trigger("fixed.ScrollToFixed")}}else{v(I)}}}}}function m(){if(!l.options.bottom){return 0}return l.options.bottom}function o(){var I=G.css("position");if(I=="absolute"){G.trigger("postAbsolute.ScrollToFixed")}else{if(I=="fixed"){G.trigger("postFixed.ScrollToFixed")}else{G.trigger("postUnfixed.ScrollToFixed")}}}var C=function(I){if(G.is(":visible")){c=false;A()}};var F=function(I){(!!window.requestAnimationFrame)?requestAnimationFrame(A):A()};var B=function(){var J=document.body;if(document.createElement&&J&&J.appendChild&&J.removeChild){var L=document.createElement("div");if(!L.getBoundingClientRect){return null}L.innerHTML="x";L.style.cssText="position:fixed;top:100px;";J.appendChild(L);var M=J.style.height,N=J.scrollTop;J.style.height="3000px";J.scrollTop=500;var I=L.getBoundingClientRect().top;J.style.height=M;var K=(I===100);J.removeChild(L);J.scrollTop=N;return K}return null};var r=function(I){I=I||window.event;if(I.preventDefault){I.preventDefault()}I.returnValue=false};l.init=function(){l.options=a.extend({},a.ScrollToFixed.defaultOptions,i);y=G.css("z-index");l.$el.css("z-index",l.options.zIndex);t=a("
");H=G.css("position");E=G.css("position");e=G.css("top");if(h()){l.$el.after(t)}a(window).bind("resize.ScrollToFixed",C);a(window).bind("scroll.ScrollToFixed",F);if(l.options.preFixed){G.bind("preFixed.ScrollToFixed",l.options.preFixed)}if(l.options.postFixed){G.bind("postFixed.ScrollToFixed",l.options.postFixed)}if(l.options.preUnfixed){G.bind("preUnfixed.ScrollToFixed",l.options.preUnfixed)}if(l.options.postUnfixed){G.bind("postUnfixed.ScrollToFixed",l.options.postUnfixed)}if(l.options.preAbsolute){G.bind("preAbsolute.ScrollToFixed",l.options.preAbsolute)}if(l.options.postAbsolute){G.bind("postAbsolute.ScrollToFixed",l.options.postAbsolute)}if(l.options.fixed){G.bind("fixed.ScrollToFixed",l.options.fixed)}if(l.options.unfixed){G.bind("unfixed.ScrollToFixed",l.options.unfixed)}if(l.options.spacerClass){t.addClass(l.options.spacerClass)}G.bind("resize.ScrollToFixed",function(){t.height(G.height())});G.bind("scroll.ScrollToFixed",function(){G.trigger("preUnfixed.ScrollToFixed");k();G.trigger("unfixed.ScrollToFixed");A()});G.bind("detach.ScrollToFixed",function(I){r(I);G.trigger("preUnfixed.ScrollToFixed");k();G.trigger("unfixed.ScrollToFixed");a(window).unbind("resize.ScrollToFixed",C);a(window).unbind("scroll.ScrollToFixed",F);G.unbind(".ScrollToFixed");t.remove();l.$el.removeData("ScrollToFixed")});C()};l.init()};a.ScrollToFixed.defaultOptions={marginTop:0,limit:0,bottom:-1,zIndex:1000,baseClassName:"scroll-to-fixed-fixed"};a.fn.scrollToFixed=function(b){return this.each(function(){(new a.ScrollToFixed(this,b))})}})(jQuery); \ No newline at end of file