forked from somewebmedia/hc-sticky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.hc-sticky.min.js
12 lines (11 loc) · 8.25 KB
/
jquery.hc-sticky.min.js
1
2
3
4
5
6
7
8
9
10
11
12
// jQuery HC-Sticky
// =============
// Version: 1.2.43
// 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(t,o,e){"use strict";var i=t(o),s=o.document,n=t(s),r=function(){for(var t,o=3,e=s.createElement("div"),i=e.getElementsByTagName("i");e.innerHTML="<!--[if gt IE "+ ++o+"]><i></i><![endif]-->",i[0];);return o>4?o:t}(),a=function(){var t=o.pageXOffset!==e?o.pageXOffset:"CSS1Compat"==s.compatMode?o.document.documentElement.scrollLeft:o.document.body.scrollLeft,i=o.pageYOffset!==e?o.pageYOffset:"CSS1Compat"==s.compatMode?o.document.documentElement.scrollTop:o.document.body.scrollTop;"undefined"==typeof a.x&&(a.x=t,a.y=i),"undefined"==typeof a.distanceX?(a.distanceX=t,a.distanceY=i):(a.distanceX=t-a.x,a.distanceY=i-a.y);var n=a.x-t,r=a.y-i;a.direction=0>n?"right":n>0?"left":0>=r?"down":r>0?"up":"first",a.x=t,a.y=i};i.on("scroll",a),t.fn.style=function(e){if(!e)return null;var i,n=t(this),r=n.clone().css("display","none");r.find("input:radio").attr("name","copy-"+Math.floor(100*Math.random()+1)),n.after(r);var a=function(t,e){var i;return t.currentStyle?i=t.currentStyle[e.replace(/-\w/g,function(t){return t.toUpperCase().replace("-","")})]:o.getComputedStyle&&(i=s.defaultView.getComputedStyle(t,null).getPropertyValue(e)),i=/margin/g.test(e)?parseInt(i)===n[0].offsetLeft?i:"auto":i};return"string"==typeof e?i=a(r[0],e):(i={},t.each(e,function(t,o){i[o]=a(r[0],o)})),r.remove(),i||null},t.fn.extend({hcSticky:function(s){return 0==this.length?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:t.noop,onStop:t.noop,on:!0,fn:null},s||{},{reinit:function(){t(this).hcSticky()},stop:function(){t(this).pluginOptions("hcSticky",{on:!1}).each(function(){var o=t(this),e=o.pluginOptions("hcSticky"),i=o.parent("."+e.wrapperClassName),s=o.offset().top-i.offset().top;o.stop().css({position:"absolute",top:s,bottom:"auto",left:"auto",right:"auto"}).removeClass(e.className)})},off:function(){t(this).pluginOptions("hcSticky",{on:!1}).each(function(){var o=t(this),e=o.pluginOptions("hcSticky"),i=o.parent("."+e.wrapperClassName);o.stop().css({position:"relative",top:"auto",bottom:"auto",left:"auto",right:"auto"}).removeClass(e.className),i.css("height","auto")})},on:function(){t(this).each(function(){t(this).pluginOptions("hcSticky",{on:!0,remember:{offsetTop:i.scrollTop()}}).hcSticky()})},destroy:function(){var o=t(this),e=o.pluginOptions("hcSticky"),s=o.parent("."+e.wrapperClassName);o.removeData("hcStickyInit").stop().css({position:s.css("position"),top:s.css("top"),bottom:s.css("bottom"),left:s.css("left"),right:s.css("right")}).removeClass(e.className),i.off("resize",e.fn.resize).off("scroll",e.fn.scroll),o.unwrap()}}),s&&"undefined"!=typeof s.on&&(s.on?this.hcSticky("on"):this.hcSticky("off")),"string"==typeof s?this:this.each(function(){var s=t(this),c=s.pluginOptions("hcSticky"),p=function(){var t=s.parent("."+c.wrapperClassName);return s.trigger("hcSticky-reinit",[s,t,c]),t.length>0?(t.stop().css({height:s.outerHeight(!0),width:function(){var o=t.style("width");return o.indexOf("%")>=0||"auto"==o?("border-box"==s.css("box-sizing")||"border-box"==s.css("-moz-box-sizing")?s.css("width",t.width()):s.css("width",t.width()-parseInt(s.css("padding-left")-parseInt(s.css("padding-right")))),o):s.outerWidth(!0)}()}),t):!1}()||function(){var o=s.style(["width","margin-left","left","right","top","bottom","float","display"]),e=s.css("display"),i=t("<div>",{"class":c.wrapperClassName}).stop().css({display:e,height:s.outerHeight(!0),width:function(){return o.width.indexOf("%")>=0||"auto"==o.width&&"inline-block"!=e&&"inline"!=e?(s.css("width",parseFloat(s.css("width"))),o.width):"auto"!=o.width||"inline-block"!=e&&"inline"!=e?"auto"==o["margin-left"]?s.outerWidth():s.outerWidth(!0):s.width()}(),margin:o["margin-left"]?"auto":null,position:function(){var t=s.css("position");return"static"==t?"relative":t}(),"float":o["float"]||null,left:o.left,right:o.right,top:o.top,bottom:o.bottom,"vertical-align":"top"});return s.wrap(i),7===r&&0===t("head").find("style#hcsticky-iefix").length&&t('<style id="hcsticky-iefix">.'+c.wrapperClassName+" {zoom: 1;}</style>").appendTo("head"),s.parent()}();if(!s.data("hcStickyInit")){s.data("hcStickyInit",!0);var l=!(!c.stickTo||!("document"==c.stickTo||c.stickTo.nodeType&&9==c.stickTo.nodeType||"object"==typeof c.stickTo&&c.stickTo instanceof("undefined"!=typeof HTMLDocument?HTMLDocument:Document))),f=c.stickTo?l?n:"string"==typeof c.stickTo?t(c.stickTo):c.stickTo:p.parent();s.stop().css({top:"auto",bottom:"auto",left:"auto",right:"auto"}),i.load(function(){s.outerHeight(!0)>f.height()&&(p.css("height",s.outerHeight(!0)),s.hcSticky("reinit"))});var d=function(t){s.hasClass(c.className)||(t=t||{},s.stop().css({position:"fixed",top:t.top||0,left:t.left||p.offset().left}).addClass(c.className),c.onStart.apply(s[0]),p.addClass("sticky-active"))},u=function(t){t=t||{},t.position=t.position||"absolute",t.top=t.top||0,t.left=t.left||0,"fixed"!=s.css("position")&&parseInt(s.css("top"))==t.top||(s.stop().css({position:t.position,top:t.top,left:t.left}).removeClass(c.className),c.onStop.apply(s[0]),p.removeClass("sticky-active"))},h=function(){if(c.on&&s.is(":visible")){if(s.outerHeight(!0)>=f.height())return void u();var o,e=c.innerSticker?t(c.innerSticker).position().top:c.innerTop?c.innerTop:0,n=p.offset().top,r=f.height()-c.bottomEnd+(l?0:n),h=p.offset().top-c.top+e,m=s.outerHeight(!0)+c.bottom,g=i.height(),y=i.scrollTop(),v=s.offset().top,w=v-y;if("undefined"!=typeof c.remember&&c.remember){var k=v-c.top-e;return void(m-e>g&&c.followScroll?y>k&&y+g<=k+s.height()&&(c.remember=!1):c.remember.offsetTop>k?k>=y&&(d({top:c.top-e}),c.remember=!1):y>=k&&(d({top:c.top-e}),c.remember=!1))}y>h?r+c.bottom-(c.followScroll&&m>g?0:c.top)<=y+m-e-(m-e>g-(h-e)&&c.followScroll&&(o=m-g-e)>0?o:0)?u({top:r-m+c.bottom-n}):m-e>g&&c.followScroll?g>=w+m?"down"==a.direction?d({top:g-m}):0>w&&"fixed"==s.css("position")&&u({top:v-(h+c.top-e)-a.distanceY}):"up"==a.direction&&v>=y+c.top-e?d({top:c.top-e}):"down"==a.direction&&v+m>g&&"fixed"==s.css("position")&&u({top:v-(h+c.top-e)-a.distanceY}):d({top:c.top-e}):u()}},m=!1,g=!1,y=function(){if(w(),v(),c.on){var t=function(){"fixed"==s.css("position")?s.css("left",p.offset().left):s.css("left",0)};if(c.responsive){g||(g=s.clone().attr("style","").css({visibility:"hidden",height:0,overflow:"hidden",paddingTop:0,paddingBottom:0,marginTop:0,marginBottom:0}),p.after(g));var o=p.style("width"),e=g.style("width");"auto"==e&&"auto"!=o&&(e=parseInt(s.css("width"))),e!=o&&p.width(e),m&&clearTimeout(m),m=setTimeout(function(){m=!1,g.remove(),g=!1},250)}if(t(),s.outerWidth(!0)!=p.width()){var i="border-box"==s.css("box-sizing")||"border-box"==s.css("-moz-box-sizing")?p.width():p.width()-parseInt(s.css("padding-left"))-parseInt(s.css("padding-right"));i=i-parseInt(s.css("margin-left"))-parseInt(s.css("margin-right")),s.css("width",i)}}};s.pluginOptions("hcSticky",{fn:{scroll:h,resize:y}});var v=function(){if(c.offResolutions){t.isArray(c.offResolutions)||(c.offResolutions=[c.offResolutions]);var o=!0;t.each(c.offResolutions,function(t,e){0>e?i.width()<-1*e&&(o=!1,s.hcSticky("off")):i.width()>e&&(o=!1,s.hcSticky("off"))}),o&&!c.on&&s.hcSticky("on")}};v(),i.on("resize",y);var w=function(){var s=!1;t._data(o,"events").scroll!=e&&t.each(t._data(o,"events").scroll,function(t,o){o.handler==c.fn.scroll&&(s=!0)}),s||(c.fn.scroll(!0),i.on("scroll",c.fn.scroll))};w()}}))}})}(jQuery,this),function(t,o){"use strict";t.fn.extend({pluginOptions:function(e,i,s,n){return this.data(e)||this.data(e,{}),e&&"undefined"==typeof i?this.data(e).options:(s=s||i||{},"object"==typeof s||s===o?this.each(function(){var o=t(this);o.data(e).options?o.data(e,t.extend(o.data(e),{options:t.extend(o.data(e).options,s||{})})):(o.data(e,{options:t.extend(i,s||{})}),n&&(o.data(e).commands=n))}):"string"==typeof s?this.each(function(){var o=t(this).data(e).commands;o&&o[s]&&o[s].call(this)}):this)}})}(jQuery);