forked from somewebmedia/hc-sticky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.hc-sticky.min.js
27 lines (26 loc) · 8.13 KB
/
jquery.hc-sticky.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// jQuery HC-Sticky
// =============
// Version: 1.2.42
// Copyright: Some Web Media
// Author: Some Web Guy
// Author URL: http://twitter.com/some_web_guy
// Website: http://someweblog.com/
// Plugin URL: https://github.com/somewebmedia/hc-sticky
// License: Released under the MIT License www.opensource.org/licenses/mit-license.php
// Description: Cross-browser jQuery plugin that makes any element attached to the page and always visible while you scroll.
(function(c,g,k){var l=c(g),h=g.document,s=c(h),t=function(){for(var c=3,b=h.createElement("div"),a=b.getElementsByTagName("i");b.innerHTML="\x3c!--[if gt IE "+ ++c+"]><i></i><![endif]--\x3e",a[0];);return 4<c?c:void 0}(),f=function(){var c=g.pageXOffset!==k?g.pageXOffset:"CSS1Compat"==h.compatMode?g.document.documentElement.scrollLeft:g.document.body.scrollLeft,b=g.pageYOffset!==k?g.pageYOffset:"CSS1Compat"==h.compatMode?g.document.documentElement.scrollTop:g.document.body.scrollTop;"undefined"==
typeof f.x&&(f.x=c,f.y=b);"undefined"==typeof f.distanceX?(f.distanceX=c,f.distanceY=b):(f.distanceX=c-f.x,f.distanceY=b-f.y);var a=f.x-c,e=f.y-b;f.direction=0>a?"right":0<a?"left":0>=e?"down":0<e?"up":"first";f.x=c;f.y=b};l.on("scroll",f);c.fn.style=function(f){if(!f)return null;var b=c(this),a,e=b.clone().css("display","none");e.find("input:radio").attr("name","copy-"+Math.floor(100*Math.random()+1));b.after(e);var k=function(a,c){var e;a.currentStyle?e=a.currentStyle[c.replace(/-\w/g,function(b){return b.toUpperCase().replace("-",
"")})]:g.getComputedStyle&&(e=h.defaultView.getComputedStyle(a,null).getPropertyValue(c));return e=/margin/g.test(c)?parseInt(e)===b[0].offsetLeft?e:"auto":e};"string"==typeof f?a=k(e[0],f):(a={},c.each(f,function(b,c){a[c]=k(e[0],c)}));e.remove();return a||null};c.fn.extend({hcSticky:function(h){if(0==this.length)return this;this.pluginOptions("hcSticky",{top:0,bottom:0,bottomEnd:0,innerTop:0,innerSticker:null,className:"sticky",wrapperClassName:"wrapper-sticky",stickTo:null,responsive:!0,followScroll:!0,
offResolutions:null,onStart:c.noop,onStop:c.noop,on:!0,fn:null},h||{},{reinit:function(){c(this).hcSticky()},stop:function(){c(this).pluginOptions("hcSticky",{on:!1}).each(function(){var b=c(this),a=b.pluginOptions("hcSticky"),e=b.parent("."+a.wrapperClassName),e=b.offset().top-e.offset().top;b.css({position:"absolute",top:e,bottom:"auto",left:"auto",right:"auto"}).removeClass(a.className)})},off:function(){c(this).pluginOptions("hcSticky",{on:!1}).each(function(){var b=c(this),a=b.pluginOptions("hcSticky"),
e=b.parent("."+a.wrapperClassName);b.css({position:"relative",top:"auto",bottom:"auto",left:"auto",right:"auto"}).removeClass(a.className);e.css("height","auto")})},on:function(){c(this).each(function(){c(this).pluginOptions("hcSticky",{on:!0,remember:{offsetTop:l.scrollTop()}}).hcSticky()})},destroy:function(){var b=c(this),a=b.pluginOptions("hcSticky"),e=b.parent("."+a.wrapperClassName);b.removeData("hcStickyInit").css({position:e.css("position"),top:e.css("top"),bottom:e.css("bottom"),left:e.css("left"),
right:e.css("right")}).removeClass(a.className);l.off("resize",a.fn.resize).off("scroll",a.fn.scroll);b.unwrap()}});h&&"undefined"!=typeof h.on&&(h.on?this.hcSticky("on"):this.hcSticky("off"));return"string"==typeof h?this:this.each(function(){var b=c(this),a=b.pluginOptions("hcSticky"),e=function(){var d=b.parent("."+a.wrapperClassName);return 0<d.length?(d.css({height:b.outerHeight(!0),width:function(){var a=d.style("width");return 0<=a.indexOf("%")||"auto"==a?("border-box"==b.css("box-sizing")||
"border-box"==b.css("-moz-box-sizing")?b.css("width",d.width()):b.css("width",d.width()-parseInt(b.css("padding-left")-parseInt(b.css("padding-right")))),a):b.outerWidth(!0)}()}),d):!1}()||function(){var d=b.style("width margin-left left right top bottom float display".split(" ")),e=b.css("display"),f=c("<div>",{"class":a.wrapperClassName}).css({display:e,height:b.outerHeight(!0),width:function(){return 0<=d.width.indexOf("%")||"auto"==d.width&&"inline-block"!=e&&"inline"!=e?(b.css("width",parseFloat(b.css("width"))),
d.width):"auto"!=d.width||"inline-block"!=e&&"inline"!=e?"auto"==d["margin-left"]?b.outerWidth():b.outerWidth(!0):b.width()}(),margin:d["margin-left"]?"auto":null,position:function(){var a=b.css("position");return"static"==a?"relative":a}(),"float":d["float"]||null,left:d.left,right:d.right,top:d.top,bottom:d.bottom,"vertical-align":"top"});b.wrap(f);7===t&&0===c("head").find("style#hcsticky-iefix").length&&c('<style id="hcsticky-iefix">.'+a.wrapperClassName+" {zoom: 1;}</style>").appendTo("head");
return b.parent()}();if(!b.data("hcStickyInit")){b.data("hcStickyInit",!0);var h=a.stickTo&&("document"==a.stickTo||a.stickTo.nodeType&&9==a.stickTo.nodeType||"object"==typeof a.stickTo&&a.stickTo instanceof("undefined"!=typeof HTMLDocument?HTMLDocument:Document))?!0:!1,v=a.stickTo?h?s:"string"==typeof a.stickTo?c(a.stickTo):a.stickTo:e.parent();b.css({top:"auto",bottom:"auto",left:"auto",right:"auto"});l.load(function(){b.outerHeight(!0)>v.height()&&(e.css("height",b.outerHeight(!0)),b.hcSticky("reinit"))});
var u=function(d){b.hasClass(a.className)||(d=d||{},b.css({position:"fixed",top:d.top||0,left:d.left||e.offset().left}).addClass(a.className),a.onStart.apply(b[0]),e.addClass("sticky-active"))},w=function(d){d=d||{};d.position=d.position||"absolute";d.top=d.top||0;d.left=d.left||0;if("fixed"==b.css("position")||parseInt(b.css("top"))!=d.top)b.css({position:d.position,top:d.top,left:d.left}).removeClass(a.className),a.onStop.apply(b[0]),e.removeClass("sticky-active")},x=!1,m=!1,A=function(){y();z();
var d=function(){"fixed"==b.css("position")?b.css("left",e.offset().left):b.css("left",0)};if(a.responsive){m||(m=b.clone().attr("style","").css({visibility:"hidden",height:0,overflow:"hidden",paddingTop:0,paddingBottom:0,marginTop:0,marginBottom:0}),e.after(m));var c=e.style("width"),f=m.style("width");"auto"==f&&"auto"!=c&&(f=parseInt(b.css("width")));f!=c&&e.width(f);x&&clearTimeout(x);x=setTimeout(function(){x=!1;m.remove();m=!1;d()},100)}else d();b.outerWidth(!0)!=e.width()&&(c="border-box"==
b.css("box-sizing")||"border-box"==b.css("-moz-box-sizing")?e.width():e.width()-parseInt(b.css("padding-left"))-parseInt(b.css("padding-right")),c=c-parseInt(b.css("margin-left"))-parseInt(b.css("margin-right")),b.css("width",c))};b.pluginOptions("hcSticky",{fn:{scroll:function(d){if(a.on&&!(b.outerHeight(!0)>=v.height())){d=a.innerSticker?c(a.innerSticker).position().top:a.innerTop?a.innerTop:0;var k=e.offset().top,g=v.height()-a.bottomEnd+(h?0:k),t=e.offset().top-a.top+d,n=b.outerHeight(!0)+a.bottom,
p=l.height(),q=l.scrollTop(),m=b.offset().top,s=m-q,r;"undefined"!=typeof a.remember&&a.remember?(r=m-a.top-d,n-d>p&&a.followScroll?r<q&&q+p<=r+b.height()&&(a.remember=!1):a.remember.offsetTop>r?q<=r&&(u({top:a.top-d}),a.remember=!1):q>=r&&(u({top:a.top-d}),a.remember=!1)):q>t?g+a.bottom-(a.followScroll&&p<n?0:a.top)<=q+n-d-(n-d>p-(t-d)&&a.followScroll?0<(r=n-p-d)?r:0:0)?w({top:g-n+a.bottom-k}):n-d>p&&a.followScroll?s+n<=p?"down"==f.direction?u({top:p-n}):0>s&&"fixed"==b.css("position")&&w({top:m-
(t+a.top-d)-f.distanceY}):"up"==f.direction&&m>=q+a.top-d?u({top:a.top-d}):"down"==f.direction&&m+n>p&&"fixed"==b.css("position")&&w({top:m-(t+a.top-d)-f.distanceY}):u({top:a.top-d}):w()}},resize:A}});var z=function(){if(a.offResolutions){c.isArray(a.offResolutions)||(a.offResolutions=[a.offResolutions]);var d=!0;c.each(a.offResolutions,function(a,c){0>c?l.width()<-1*c&&(d=!1,b.hcSticky("off")):l.width()>c&&(d=!1,b.hcSticky("off"))});d&&!a.on&&b.hcSticky("on")}};z();l.on("resize",A);var y=function(){if(b.outerHeight(!0)<
v.height()){var d=!1;c._data(g,"events").scroll!=k&&c.each(c._data(g,"events").scroll,function(b,c){c.handler==a.fn.scroll&&(d=!0)});d||(a.fn.scroll(!0),l.on("scroll",a.fn.scroll))}};y()}})}})})(jQuery,this);
(function(c,g){c.fn.extend({pluginOptions:function(k,l,h,s){this.data(k)||this.data(k,{});if(k&&"undefined"==typeof l)return this.data(k).options;h=h||l||{};return"object"==typeof h||h===g?this.each(function(){var g=c(this);g.data(k).options?g.data(k,c.extend(g.data(k),{options:c.extend(g.data(k).options,h||{})})):(g.data(k,{options:c.extend(l,h||{})}),s&&(g.data(k).commands=s))}):"string"==typeof h?this.each(function(){c(this).data(k).commands[h].call(this)}):this}})})(jQuery);