midnight.jquery.min.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*!
  2. * Midnight.js 1.1.1
  3. * jQuery plugin to switch between multiple fixed header designs on the fly, so it looks in line with the content below it.
  4. * http://aerolab.github.io/midnight.js/
  5. *
  6. * Copyright (c) 2014 Aerolab <info@aerolab.co>
  7. *
  8. * Released under the MIT license
  9. * http://aerolab.github.io/midnight.js/LICENSE.txt
  10. */
  11. // jQuery Widget
  12. (function(e){"function"==typeof define&&define.amd?define(["jquery"],e):e(jQuery)})(function(e){var t=0,i=Array.prototype.slice;e.cleanData=function(t){return function(i){var s,n,a;for(a=0;null!=(n=i[a]);a++)try{s=e._data(n,"events"),s&&s.remove&&e(n).triggerHandler("remove")}catch(o){}t(i)}}(e.cleanData),e.widget=function(t,i,s){var n,a,o,r,h={},l=t.split(".")[0];return t=t.split(".")[1],n=l+"-"+t,s||(s=i,i=e.Widget),e.expr[":"][n.toLowerCase()]=function(t){return!!e.data(t,n)},e[l]=e[l]||{},a=e[l][t],o=e[l][t]=function(e,t){return this._createWidget?(arguments.length&&this._createWidget(e,t),void 0):new o(e,t)},e.extend(o,a,{version:s.version,_proto:e.extend({},s),_childConstructors:[]}),r=new i,r.options=e.widget.extend({},r.options),e.each(s,function(t,s){return e.isFunction(s)?(h[t]=function(){var e=function(){return i.prototype[t].apply(this,arguments)},n=function(e){return i.prototype[t].apply(this,e)};return function(){var t,i=this._super,a=this._superApply;return this._super=e,this._superApply=n,t=s.apply(this,arguments),this._super=i,this._superApply=a,t}}(),void 0):(h[t]=s,void 0)}),o.prototype=e.widget.extend(r,{widgetEventPrefix:a?r.widgetEventPrefix||t:t},h,{constructor:o,namespace:l,widgetName:t,widgetFullName:n}),a?(e.each(a._childConstructors,function(t,i){var s=i.prototype;e.widget(s.namespace+"."+s.widgetName,o,i._proto)}),delete a._childConstructors):i._childConstructors.push(o),e.widget.bridge(t,o),o},e.widget.extend=function(t){for(var s,n,a=i.call(arguments,1),o=0,r=a.length;r>o;o++)for(s in a[o])n=a[o][s],a[o].hasOwnProperty(s)&&void 0!==n&&(t[s]=e.isPlainObject(n)?e.isPlainObject(t[s])?e.widget.extend({},t[s],n):e.widget.extend({},n):n);return t},e.widget.bridge=function(t,s){var n=s.prototype.widgetFullName||t;e.fn[t]=function(a){var o="string"==typeof a,r=i.call(arguments,1),h=this;return a=!o&&r.length?e.widget.extend.apply(null,[a].concat(r)):a,o?this.each(function(){var i,s=e.data(this,n);return"instance"===a?(h=s,!1):s?e.isFunction(s[a])&&"_"!==a.charAt(0)?(i=s[a].apply(s,r),i!==s&&void 0!==i?(h=i&&i.jquery?h.pushStack(i.get()):i,!1):void 0):e.error("no such method '"+a+"' for "+t+" widget instance"):e.error("cannot call methods on "+t+" prior to initialization; "+"attempted to call method '"+a+"'")}):this.each(function(){var t=e.data(this,n);t?(t.option(a||{}),t._init&&t._init()):e.data(this,n,new s(a,this))}),h}},e.Widget=function(){},e.Widget._childConstructors=[],e.Widget.prototype={widgetName:"widget",widgetEventPrefix:"",defaultElement:"<div>",options:{disabled:!1,create:null},_createWidget:function(i,s){s=e(s||this.defaultElement||this)[0],this.element=e(s),this.uuid=t++,this.eventNamespace="."+this.widgetName+this.uuid,this.bindings=e(),this.hoverable=e(),this.focusable=e(),s!==this&&(e.data(s,this.widgetFullName,this),this._on(!0,this.element,{remove:function(e){e.target===s&&this.destroy()}}),this.document=e(s.style?s.ownerDocument:s.document||s),this.window=e(this.document[0].defaultView||this.document[0].parentWindow)),this.options=e.widget.extend({},this.options,this._getCreateOptions(),i),this._create(),this._trigger("create",null,this._getCreateEventData()),this._init()},_getCreateOptions:e.noop,_getCreateEventData:e.noop,_create:e.noop,_init:e.noop,destroy:function(){this._destroy(),this.element.unbind(this.eventNamespace).removeData(this.widgetFullName).removeData(e.camelCase(this.widgetFullName)),this.widget().unbind(this.eventNamespace).removeAttr("aria-disabled").removeClass(this.widgetFullName+"-disabled "+"ui-state-disabled"),this.bindings.unbind(this.eventNamespace),this.hoverable.removeClass("ui-state-hover"),this.focusable.removeClass("ui-state-focus")},_destroy:e.noop,widget:function(){return this.element},option:function(t,i){var s,n,a,o=t;if(0===arguments.length)return e.widget.extend({},this.options);if("string"==typeof t)if(o={},s=t.split("."),t=s.shift(),s.length){for(n=o[t]=e.widget.extend({},this.options[t]),a=0;s.length-1>a;a++)n[s[a]]=n[s[a]]||{},n=n[s[a]];if(t=s.pop(),1===arguments.length)return void 0===n[t]?null:n[t];n[t]=i}else{if(1===arguments.length)return void 0===this.options[t]?null:this.options[t];o[t]=i}return this._setOptions(o),this},_setOptions:function(e){var t;for(t in e)this._setOption(t,e[t]);return this},_setOption:function(e,t){return this.options[e]=t,"disabled"===e&&(this.widget().toggleClass(this.widgetFullName+"-disabled",!!t),t&&(this.hoverable.removeClass("ui-state-hover"),this.focusable.removeClass("ui-state-focus"))),this},enable:function(){return this._setOptions({disabled:!1})},disable:function(){return this._setOptions({disabled:!0})},_on:function(t,i,s){var n,a=this;"boolean"!=typeof t&&(s=i,i=t,t=!1),s?(i=n=e(i),this.bindings=this.bindings.add(i)):(s=i,i=this.element,n=this.widget()),e.each(s,function(s,o){function r(){return t||a.options.disabled!==!0&&!e(this).hasClass("ui-state-disabled")?("string"==typeof o?a[o]:o).apply(a,arguments):void 0}"string"!=typeof o&&(r.guid=o.guid=o.guid||r.guid||e.guid++);var h=s.match(/^([\w:-]*)\s*(.*)$/),l=h[1]+a.eventNamespace,u=h[2];u?n.delegate(u,l,r):i.bind(l,r)})},_off:function(t,i){i=(i||"").split(" ").join(this.eventNamespace+" ")+this.eventNamespace,t.unbind(i).undelegate(i),this.bindings=e(this.bindings.not(t).get()),this.focusable=e(this.focusable.not(t).get()),this.hoverable=e(this.hoverable.not(t).get())},_delay:function(e,t){function i(){return("string"==typeof e?s[e]:e).apply(s,arguments)}var s=this;return setTimeout(i,t||0)},_hoverable:function(t){this.hoverable=this.hoverable.add(t),this._on(t,{mouseenter:function(t){e(t.currentTarget).addClass("ui-state-hover")},mouseleave:function(t){e(t.currentTarget).removeClass("ui-state-hover")}})},_focusable:function(t){this.focusable=this.focusable.add(t),this._on(t,{focusin:function(t){e(t.currentTarget).addClass("ui-state-focus")},focusout:function(t){e(t.currentTarget).removeClass("ui-state-focus")}})},_trigger:function(t,i,s){var n,a,o=this.options[t];if(s=s||{},i=e.Event(i),i.type=(t===this.widgetEventPrefix?t:this.widgetEventPrefix+t).toLowerCase(),i.target=this.element[0],a=i.originalEvent)for(n in a)n in i||(i[n]=a[n]);return this.element.trigger(i,s),!(e.isFunction(o)&&o.apply(this.element[0],[i].concat(s))===!1||i.isDefaultPrevented())}},e.each({show:"fadeIn",hide:"fadeOut"},function(t,i){e.Widget.prototype["_"+t]=function(s,n,a){"string"==typeof n&&(n={effect:n});var o,r=n?n===!0||"number"==typeof n?i:n.effect||i:t;n=n||{},"number"==typeof n&&(n={duration:n}),o=!e.isEmptyObject(n),n.complete=a,n.delay&&s.delay(n.delay),o&&e.effects&&e.effects.effect[r]?s[t](n):r!==t&&s[r]?s[r](n.duration,n.easing,a):s.queue(function(i){e(this)[t](),a&&a.call(s[0]),i()})}}),e.widget});
  13. ((function ( $ ) {
  14. "use strict";
  15. $.widget('aerolab.midnight', {
  16. options: {
  17. // The class that wraps each header. Used as a clipping mask.
  18. headerClass: 'midnightHeader',
  19. // The class that wraps the contents of each header. Also used as a clipping mask.
  20. innerClass: 'midnightInner',
  21. // The class used by the default header (useful when adding multiple headers with different markup).
  22. defaultClass: 'default',
  23. // Unused: Add a prefix to the header classes (so if you set the "thingy-" prefix, a section with data-midnight="butterfly" will use the "thingy-butterfly" header)
  24. classPrefix: '',
  25. // If you want to use plugin more than once or if you want a different data attribute name (so if you set the "header" in a section use data-header)
  26. sectionSelector: 'midnight'
  27. },
  28. // Cache all the switchable headers (different colors)
  29. _headers: {},
  30. _headerInfo: {top:0, height:0},
  31. // Cache all the sections which cause the header to change colors
  32. _$sections: [],
  33. _sections: [],
  34. // Scroll Cache
  35. _scrollTop: 0,
  36. _documentHeight: 0,
  37. // Tools
  38. _transformMode: false,
  39. refresh: function() {
  40. this._headerInfo = {
  41. // Todo: Add support for top (though it's mostly unnecessary)
  42. top: 0,
  43. height: this.element.outerHeight()
  44. };
  45. // Sections that affect the color of the header (and cache)
  46. this._$sections = $('[data-'+ this.options.sectionSelector +']:not(:hidden)');
  47. this._sections = [];
  48. this._setupHeaders();
  49. this.recalculate();
  50. },
  51. _create: function() {
  52. var context = this;
  53. this._scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  54. this._documentHeight = $(document).height();
  55. this._headers = {};
  56. this._transformMode = this._getSupportedTransform();
  57. // Calculate all sections and create the necessary headers
  58. this.refresh();
  59. // NANANANANANANANA GRASAAAAA
  60. // (This is the ghetto way of keeping the section values updated after any kind of reflow. The overhead is minimal)
  61. setInterval(function(){
  62. context._recalculateSections();
  63. }, 1000);
  64. // We need to recalculate all this._sections and headers
  65. context.recalculate();
  66. // and at every resize
  67. $(window).resize(function(){
  68. context.recalculate();
  69. });
  70. // Start the RequestAnimationFrame loop. This should be done just once.
  71. this._updateHeadersLoop();
  72. },
  73. recalculate: function() {
  74. this._recalculateSections();
  75. this._updateHeaderHeight();
  76. this._recalculateHeaders();
  77. this._updateHeaders();
  78. },
  79. /**
  80. * This is to offer the optimal transform format when updating the header
  81. */
  82. _getSupportedTransform: function() {
  83. var prefixes = ['transform','WebkitTransform','MozTransform','OTransform','msTransform'];
  84. for(var ix = 0; ix < prefixes.length; ix++) {
  85. if(document.createElement('div').style[prefixes[ix]] !== undefined) {
  86. return prefixes[ix];
  87. }
  88. }
  89. return false;
  90. },
  91. /**
  92. * Get the size of the header.
  93. */
  94. _getContainerHeight: function(){
  95. var $customHeaders = this.element.find('> .'+this.options['headerClass']);
  96. var maxHeight = 0;
  97. var height = 0;
  98. var context = this;
  99. if( $customHeaders.length ) {
  100. $customHeaders.each(function() {
  101. var $header = $(this);
  102. var $inner = $header.find('> .'+context.options['innerClass']);
  103. // Disable the fixed height and trigger a reflow to get the proper height
  104. // Get the inner height or just the height of the container
  105. if( $inner.length ) {
  106. // Overflow: Auto fixes an issue with Chrome 41, where outerHeight() no longer takes into account
  107. // the margins of internal elements, creating a smaller container than necessary
  108. $inner.css('bottom', 'auto').css('overflow', 'auto');
  109. height = $inner.outerHeight();
  110. $inner.css('bottom', '0');
  111. } else {
  112. $header.css('bottom', 'auto');
  113. height = $header.outerHeight();
  114. $header.css('bottom', '0');
  115. }
  116. maxHeight = (height > maxHeight) ? height : maxHeight;
  117. });
  118. } else {
  119. maxHeight = height = this.element.outerHeight();
  120. }
  121. return maxHeight;
  122. },
  123. _setupHeaders: function(){
  124. // Get all the different header colors
  125. var context = this;
  126. this._headers[this.options['defaultClass']] = {};
  127. for( var i=0; i<this._$sections.length; i++ ) {
  128. var $section = $(this._$sections[i]);
  129. var headerClass = $section.data(this.options.sectionSelector);
  130. if( typeof headerClass !== 'string' ){ continue; }
  131. headerClass = headerClass.trim();
  132. if( headerClass === '' ){ continue; }
  133. context._headers[headerClass] = {};
  134. }
  135. // Get the padding of the original Header. It will be applied to the internal headers.
  136. // Todo: Implement this
  137. var defaultPaddings = {
  138. top: this.element.css("padding-top"),
  139. right: this.element.css("padding-right"),
  140. bottom: this.element.css("padding-bottom"),
  141. left: this.element.css("padding-left")
  142. };
  143. // Create the fake headers
  144. this.element
  145. .css({
  146. position: 'fixed',
  147. top: 0,
  148. left: 0,
  149. right: 0,
  150. overflow: 'hidden'
  151. });
  152. this._updateHeaderHeight();
  153. var $customHeaders = this.element.find('> .'+this.options['headerClass']);
  154. if( $customHeaders.length ) {
  155. if( ! $customHeaders.filter('.'+ this.options['defaultClass']).length ) {
  156. // If there's no default header, just pick the first one, duplicate it, and set the correct class
  157. $customHeaders.filter('.'+ this.options['headerClass'] +':first').clone(true, true).attr('class', this.options['headerClass'] +' '+ this.options['defaultClass']);
  158. }
  159. } else {
  160. // If there are no custom headers, just wrap the content and make that the default header
  161. this.element.wrapInner('<div class="'+ this.options['headerClass'] +' '+ this.options['defaultClass'] +'"></div>');
  162. }
  163. // Make a copy of the default header for use in the generic ones.
  164. var $customHeaders = this.element.find('> .'+ this.options['headerClass']);
  165. var $defaultHeader = $customHeaders.filter('.'+ this.options['defaultClass']).clone(true, true);
  166. for( var headerClass in this._headers ) {
  167. if( ! this._headers.hasOwnProperty(headerClass) ){ continue; }
  168. if( typeof this._headers[headerClass].element === 'undefined' ) {
  169. // Create the outer clipping mask
  170. // If there's some custom markup, use it, or else just clone the default header
  171. var $existingHeader = $customHeaders.filter('.'+headerClass);
  172. if( $existingHeader.length ) {
  173. this._headers[headerClass].element = $existingHeader;
  174. } else {
  175. this._headers[headerClass].element = $defaultHeader.clone(true, true).removeClass( this.options['defaultClass'] ).addClass(headerClass).appendTo( this.element );
  176. }
  177. var resetStyles = {
  178. position: 'absolute',
  179. overflow: 'hidden',
  180. top: 0,
  181. left: 0,
  182. right: 0,
  183. bottom: 0
  184. };
  185. this._headers[headerClass].element.css(resetStyles);
  186. if( this._transformMode !== false ) {
  187. this._headers[headerClass].element.css(this._transformMode, 'translateZ(0)');
  188. }
  189. // Create the inner clipping mask
  190. if( ! this._headers[headerClass].element.find('> .'+ this.options['innerClass']).length ) {
  191. this._headers[headerClass].element.wrapInner('<div class="'+ this.options['innerClass'] +'"></div>');
  192. }
  193. this._headers[headerClass].inner = this._headers[headerClass].element.find('> .'+ this.options['innerClass'])
  194. this._headers[headerClass].inner.css(resetStyles);
  195. if( this._transformMode !== false ) {
  196. this._headers[headerClass].inner.css(this._transformMode, 'translateZ(0)');
  197. }
  198. // Set the default clipping variables
  199. this._headers[headerClass].from = '';
  200. this._headers[headerClass].progress = 0.0;
  201. }
  202. }
  203. // Headers that weren't initialized have to be hidden
  204. $customHeaders.each(function(){
  205. var $header = $(this);
  206. var hasAnyClass = false;
  207. for( var headerClass in context._headers ) {
  208. if( ! context._headers.hasOwnProperty(headerClass) ){ continue; }
  209. if( $header.hasClass(headerClass) ){ hasAnyClass = true; }
  210. }
  211. // Add the inner clipping mask just in case
  212. if( ! $header.find('> .'+ context.options['innerClass']).length ) {
  213. $header.wrapInner('<div class="'+ context.options['innerClass'] +'"></div>');
  214. }
  215. if( hasAnyClass ) {
  216. $header.show();
  217. } else {
  218. $header.hide();
  219. }
  220. });
  221. },
  222. /**
  223. * Recalculate which headers should be visible at this time based on the scroll position and the (cached) position of each section.
  224. * This doesn't update
  225. */
  226. _recalculateHeaders: function(){
  227. // Check classes are currently active in the header (including the current percentage of each)
  228. this._scrollTop = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
  229. // Some browsers (e.g on OS X) allow scrolling past the top/bottom.
  230. this._scrollTop = Math.max(this._scrollTop, 0);
  231. this._scrollTop = Math.min(this._scrollTop, this._documentHeight);
  232. // Get the header's position relative to the document (given that it's fixed)
  233. var headerHeight = this._headerInfo.height;
  234. var headerStart = this._scrollTop + this._headerInfo.top;
  235. var headerEnd = headerStart + headerHeight;
  236. // Add support for transforms (for plugins like Headroom or general css stuff)
  237. if( typeof window.getComputedStyle === 'function' ) {
  238. var style = window.getComputedStyle(this.element[0], null);
  239. var styleTop = style.top;
  240. var top = 0.0;
  241. var transformY = 0.0;
  242. if( this._transformMode !== false && typeof style.transform === 'string' ) {
  243. // Convert the transform matrix to an array
  244. var transformArray = (style.transform).match(/(-?[0-9\.]+)/g);
  245. if( transformArray !== null && transformArray.length >= 6 && ! isNaN(parseFloat(transformArray[5])) ) {
  246. transformY = parseFloat(transformArray[5]);
  247. }
  248. }
  249. if ( style.top.indexOf('%') >= 0 && ! isNaN(parseFloat(styleTop)) ) {
  250. // SAFARI ISSUE https://bugs.webkit.org/show_bug.cgi?id=29084
  251. top = window.innerHeight * ( parseFloat(styleTop) / 100 );
  252. } else if( (styleTop).indexOf('px') >= 0 && ! isNaN(parseFloat(styleTop)) ) {
  253. top = parseFloat(style.top);
  254. }
  255. headerStart += top + transformY;
  256. headerEnd += top + transformY;
  257. }
  258. // Reset the header status
  259. for( var headerClass in this._headers ) {
  260. if( ! this._headers.hasOwnProperty(headerClass) ){ continue; }
  261. // from == '' signals that the section is inactive
  262. this._headers[ headerClass ].from = '';
  263. this._headers[ headerClass ].progress = 0.0;
  264. }
  265. // Set the header status
  266. for( var ix = 0; ix < this._sections.length; ix++ ) {
  267. // Todo: This isn't exactly the best code.
  268. // If there's some kind of overlap between the header and a section, that class becomes active
  269. if( headerEnd >= this._sections[ix].start && headerStart <= this._sections[ix].end ) {
  270. this._headers[ this._sections[ix].className ].visible = true;
  271. // If the header sits neatly within the section, this is the only active class
  272. if( headerStart >= this._sections[ix].start && headerEnd <= this._sections[ix].end ) {
  273. this._headers[ this._sections[ix].className ].from = 'top';
  274. this._headers[ this._sections[ix].className ].progress += 1.0;
  275. }
  276. // If the header is in the middle of the end of a section, it comes from the top
  277. else if( headerEnd > this._sections[ix].end && headerStart < this._sections[ix].end ) {
  278. this._headers[ this._sections[ix].className ].from = 'top';
  279. this._headers[ this._sections[ix].className ].progress = 1.0 - (headerEnd - this._sections[ix].end) / headerHeight;
  280. }
  281. // If the header is in the middle of the start of a section, it comes from the bottom
  282. else if( headerEnd > this._sections[ix].start && headerStart < this._sections[ix].start ) {
  283. // If the same color continues in the next section, just add the progress to it so we don't switch
  284. if( this._headers[ this._sections[ix].className ].from === 'top' ) {
  285. this._headers[ this._sections[ix].className ].progress += (headerEnd - this._sections[ix].start) / headerHeight;
  286. }
  287. else {
  288. this._headers[ this._sections[ix].className ].from = 'bottom';
  289. this._headers[ this._sections[ix].className ].progress = (headerEnd - this._sections[ix].start) / headerHeight;
  290. }
  291. }
  292. }
  293. }
  294. },
  295. /**
  296. * Update the headers based on the position of each section
  297. */
  298. _updateHeaders: function(){
  299. // Don't do anything if there are no headers
  300. if( typeof this._headers[ this.options['defaultClass'] ] === 'undefined' ){ return; }
  301. // Do some preprocessing to ensure a header is always shown (even if some this._sections haven't been assigned)
  302. var totalProgress = 0.0;
  303. var lastActiveClass = '';
  304. for( var headerClass in this._headers ) {
  305. if( ! this._headers.hasOwnProperty(headerClass) ){ continue; }
  306. if( ! this._headers[headerClass].from === '' ){ continue; }
  307. totalProgress += this._headers[headerClass].progress;
  308. lastActiveClass = headerClass;
  309. }
  310. if( totalProgress < 1.0 ) {
  311. // Complete the header at the bottom with the default class
  312. if( this._headers[ this.options['defaultClass'] ].from === '' ) {
  313. this._headers[ this.options['defaultClass'] ].from = ( this._headers[lastActiveClass].from === 'top' ) ? 'bottom' : 'top';
  314. this._headers[ this.options['defaultClass'] ].progress = 1.0 - totalProgress;
  315. }
  316. else {
  317. this._headers[ this.options['defaultClass'] ].progress += 1.0 - totalProgress;
  318. }
  319. }
  320. for( var ix in this._headers ) {
  321. if( ! this._headers.hasOwnProperty(ix) ){ continue; }
  322. if( ! this._headers[ix].from === '' ){ continue; }
  323. var offset = (1.0 - this._headers[ix].progress) * 100.0;
  324. // Add an extra offset when an area is hidden to prevent clipping/rounding issues.
  325. if( offset >= 100.0 ) { offset = 110.0; }
  326. if( offset <= -100.0 ) { offset = -110.0; }
  327. if( this._headers[ix].from === 'top' ){
  328. if( this._transformMode !== false ) {
  329. this._headers[ix].element[0].style[this._transformMode] = 'translateY(-'+ offset +'%) translateZ(0)';
  330. this._headers[ix].inner[0].style[this._transformMode] = 'translateY(+'+ offset +'%) translateZ(0)';
  331. } else {
  332. this._headers[ix].element[0].style['top'] = '-'+ offset +'%';
  333. this._headers[ix].inner[0].style['top'] = '+'+ offset +'%';
  334. }
  335. }
  336. else {
  337. if( this._transformMode !== false ) {
  338. this._headers[ix].element[0].style[this._transformMode] = 'translateY(+'+ offset +'%) translateZ(0)';
  339. this._headers[ix].inner[0].style[this._transformMode] = 'translateY(-'+ offset +'%) translateZ(0)';
  340. } else {
  341. this._headers[ix].element[0].style['top'] = '+'+ offset +'%';
  342. this._headers[ix].inner[0].style['top'] = '-'+ offset +'%';
  343. }
  344. }
  345. }
  346. },
  347. /**
  348. * Update the size of all the sections.
  349. * This doesn't look for new sections. It only updates the ones that were around when the plugin was started.
  350. * Use .midnight('refresh') to do a full update.
  351. */
  352. _recalculateSections: function(){
  353. this._documentHeight = $(document).height();
  354. // Cache all the this._sections and their start/end positions (where the class starts and ends)
  355. this._sections = [];
  356. for( var ix=0; ix<this._$sections.length; ix++ ) {
  357. var $section = $(this._$sections[ix]);
  358. this._sections.push({
  359. element: $section,
  360. className: $section.data(this.options.sectionSelector),
  361. start: $section.offset().top,
  362. end: $section.offset().top + $section.outerHeight()
  363. });
  364. }
  365. },
  366. _updateHeaderHeight: function(){
  367. this._headerInfo.height = this._getContainerHeight();
  368. this.element.css('height', this._headerInfo.height+'px');
  369. },
  370. _updateHeadersLoop: function(){
  371. // This works using requestAnimationFrame for better compatibility with iOS/Android
  372. var context = this;
  373. this._requestAnimationFrame(function(){
  374. context._updateHeadersLoop();
  375. });
  376. this._recalculateHeaders();
  377. this._updateHeaders();
  378. },
  379. _requestAnimationFrame: function(callback){
  380. // Todo: This should be moved somewhere else
  381. var requestAnimationFrame = (requestAnimationFrame || (function(){
  382. return window.requestAnimationFrame ||
  383. window.webkitRequestAnimationFrame ||
  384. window.mozRequestAnimationFrame ||
  385. function( callback ){
  386. window.setTimeout(callback, 1000 / 60);
  387. };
  388. })());
  389. requestAnimationFrame(callback);
  390. }
  391. });
  392. })(jQuery));