forked from webpop/jquery.pin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.pin.min.js
1 lines (1 loc) · 1.3 KB
/
jquery.pin.min.js
1
(function(e){"use strict";e.fn.pin=function(t){var n=0,r=[],i=!1;t=t||{};var s=function(){for(var n=0,s=r.length;n<s;n++){var o=r[n];if(t.minWidth&&e(window).width()<=t.minWidth){o.parent().is(".pin-wrapper")&&o.unwrap(),o.css({width:"",left:"",top:"",position:""}),i=!0;continue}i=!1;var u=t.containerSelector?o.closest(t.containerSelector):e(document.body),a=o.offset(),f=u.offset(),l=o.offsetParent().offset();o.parent().is(".pin-wrapper")||o.wrap("<div class='pin-wrapper'>"),o.data("pin",{from:t.containerSelector?f.top:a.top,to:f.top+u.height()-o.outerHeight(),end:f.top+u.height(),parentTop:l.top}),o.css({width:o.outerWidth()}),o.parent().css("height",o.outerHeight())}},o=function(){if(i)return;n=window.scrollY;for(var t=0,s=r.length;t<s;t++){var o=e(r[t]),u=o.data("pin"),a=u.from,f=u.to;if(a+o.outerHeight()>u.end){o.css("position","");continue}a<n&&f>n?o.css("position")!="fixed"&&o.css({left:o.offset().left,top:0}).css("position","fixed"):n>=f?o.css({left:"",top:f-u.parentTop}).css("position","absolute"):o.css({position:"",top:"",left:""})}},u=function(){s(),o()};return this.each(function(){var t=e(this),n=e(this).data("pin")||{};if(n&&n.update)return;r.push(t),e("img",this).one("load",s),n.update=u,e(this).data("pin",n)}),e(window).scroll(o),e(window).resize(function(){s()}),s(),e(window).load(u),this}})(jQuery);