forked from Aerolab/midnight.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midnight.jquery.min.js
11 lines (11 loc) · 4.61 KB
/
midnight.jquery.min.js
1
2
3
4
5
6
7
8
9
10
11
/*!
* Midnight.js 1.0.3
* jQuery plugin to switch between multiple fixed header designs on the fly, so it looks in line with the content below it.
* http://aerolab.github.io/midnight.js/
*
* Copyright (c) 2014 Aerolab <[email protected]>
*
* Released under the MIT license
* http://aerolab.github.io/midnight.js/LICENSE.txt
*/
!function(e){e.fn.midnight=function(t){return"object"!=typeof t&&(t={}),this.each(function(){var s={headerClass:"midnightHeader",innerClass:"midnightInner",defaultClass:"default"};e.extend(s,t);var a=window.pageYOffset||document.documentElement.scrollTop,n=e(document).height(),r=e(this),o={},l={top:0,height:r.outerHeight()},d=e("[data-midnight]"),f=[],h=function(){for(var e="transform WebkitTransform MozTransform OTransform msTransform".split(" "),t=0;t<e.length;t++)if(void 0!==document.createElement("div").style[e[t]])return e[t];return!1},m=h();if(0!=d.length){var c=function(){var t=r.find("> ."+s.headerClass),a=0,n=0;return t.length?t.each(function(){var t=e(this),i=t.find("> ."+s.innerClass);i.length?(i.css("bottom","auto"),n=i.outerHeight(),i.css("bottom","0")):(t.css("bottom","auto"),n=t.outerHeight(),t.css("bottom","0")),a=n>a?n:a}):a=n=r.outerHeight(),a},u=function(){l.height=c(),r.css("height",l.height+"px")},g=function(){o["default"]={},d.each(function(){var t=e(this),s=t.data("midnight");"string"==typeof s&&(s=s.trim(),""!==s&&(o[s]={}))});({top:r.css("padding-top"),right:r.css("padding-right"),bottom:r.css("padding-bottom"),left:r.css("padding-left")});r.css({position:"fixed",top:0,left:0,right:0,overflow:"hidden"}),u();var t=r.find("> ."+s.headerClass);t.length?t.filter("."+s.defaultClass).length||t.filter("."+s.headerClass+":first").clone(!0,!0).attr("class",s.headerClass+" "+s.defaultClass):r.wrapInner('<div class="'+s.headerClass+" "+s.defaultClass+'"></div>');var t=r.find("> ."+s.headerClass),a=t.filter("."+s.defaultClass).clone(!0,!0);for(headerClass in o)if("undefined"==typeof o[headerClass].element){var n=t.filter("."+headerClass);o[headerClass].element=n.length?n:a.clone(!0,!0).removeClass(s.defaultClass).addClass(headerClass).appendTo(r);var i={position:"absolute",overflow:"hidden",top:0,left:0,right:0,bottom:0};o[headerClass].element.css(i),m!==!1&&o[headerClass].element.css(m,"translateZ(0)"),o[headerClass].element.find("> ."+s.innerClass).length||o[headerClass].element.wrapInner('<div class="'+s.innerClass+'"></div>'),o[headerClass].inner=o[headerClass].element.find("> ."+s.innerClass),o[headerClass].inner.css(i),m!==!1&&o[headerClass].inner.css(m,"translateZ(0)"),o[headerClass].from="",o[headerClass].progress=0}t.each(function(){var t=e(this),a=!1;for(headerClass in o)t.hasClass(headerClass)&&(a=!0);t.find("> ."+s.innerClass).length||t.wrapInner('<div class="'+s.innerClass+'"></div>'),a||t.hide()})};g();var p=function(){for(n=e(document).height(),f=[],i=0;i<d.length;i++){var t=e(d[i]);f.push({element:t,"class":t.data("midnight"),start:t.offset().top,end:t.offset().top+t.outerHeight()})}};setInterval(p,1e3);var x=function(){a=window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop,a=Math.max(a,0),a=Math.min(a,n);var e=l.height,t=a+l.top,s=a+l.top+e;for(ix in o)o[ix].from="",o[ix].progress=0;for(ix in f)s>=f[ix].start&&t<=f[ix].end&&(o[f[ix].class].visible=!0,t>=f[ix].start&&s<=f[ix].end?(o[f[ix].class].from="top",o[f[ix].class].progress+=1):s>f[ix].end&&t<f[ix].end?(o[f[ix].class].from="top",o[f[ix].class].progress=1-(s-f[ix].end)/e):s>f[ix].start&&t<f[ix].start&&("top"===o[f[ix].class].from?o[f[ix].class].progress+=(s-f[ix].start)/e:(o[f[ix].class].from="bottom",o[f[ix].class].progress=(s-f[ix].start)/e)))},C=function(){var e=0,t="";for(ix in o)""!==!o[ix].from&&(e+=o[ix].progress,t=ix);1>e&&(""===o[s.defaultClass].from?(o[s.defaultClass].from="top"===o[t].from?"bottom":"top",o[s.defaultClass].progress=1-e):o[s.defaultClass].progress+=1-e);for(ix in o)if(""!==!o[ix].from){var a=100*(1-o[ix].progress);"top"===o[ix].from?m!==!1?(o[ix].element[0].style[m]="translateY(-"+a+"%) translateZ(0)",o[ix].inner[0].style[m]="translateY(+"+a+"%) translateZ(0)"):(o[ix].element[0].style.top="-"+a+"%",o[ix].inner[0].style.top="+"+a+"%"):m!==!1?(o[ix].element[0].style[m]="translateY(+"+a+"%) translateZ(0)",o[ix].inner[0].style[m]="translateY(-"+a+"%) translateZ(0)"):(o[ix].element.style.top="+"+a+"%",o[ix].inner.style.top="-"+a+"%")}};e(window).resize(function(){p(),u(),x(),C()}).trigger("resize"),requestAnimationFrame=window.requestAnimationFrame||function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}();var v=function(){requestAnimationFrame(v),x(),C()};v()}})}}(jQuery);