Skip to content

Commit

Permalink
Addresses issues #22 and #18 with strict mode and tidier loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoGonzalez committed Oct 29, 2014
1 parent 13665c5 commit a299d2b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
48 changes: 26 additions & 22 deletions midnight.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
((function ( $ ) {

"use strict";

$.fn.midnight = function( customOptions ) {

if( typeof customOptions !== "object" ) {
Expand All @@ -26,10 +28,8 @@
innerClass: 'midnightInner',
// The class used by the default header (useful when adding multiple headers with different markup).
defaultClass: 'default',
/*
// 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)
// 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)
classPrefix: ''
*/
};

$.extend(settings, customOptions);
Expand All @@ -55,10 +55,10 @@
var sections = [];

var getSupportedTransform = function() {
var prefixes = 'transform WebkitTransform MozTransform OTransform msTransform'.split(' ');
for(var i = 0; i < prefixes.length; i++) {
if(document.createElement('div').style[prefixes[i]] !== undefined) {
return prefixes[i];
var prefixes = ['transform','WebkitTransform','MozTransform','OTransform','msTransform'];
for(var ix = 0; ix < prefixes.length; ix++) {
if(document.createElement('div').style[prefixes[ix]] !== undefined) {
return prefixes[ix];
}
}
return false;
Expand Down Expand Up @@ -166,7 +166,8 @@



for( headerClass in headers ) {
for( var headerClass in headers ) {
if( ! headers.hasOwnProperty(headerClass) ){ continue; }
if( typeof headers[headerClass].element === 'undefined' ) {

// Create the outer clipping mask
Expand Down Expand Up @@ -214,7 +215,8 @@
$customHeaders.each(function(){
var $header = $(this);
var hasAnyClass = false;
for( headerClass in headers ) {
for( var headerClass in headers ) {
if( ! headers.hasOwnProperty(headerClass) ){ continue; }
if( $header.hasClass(headerClass) ){ hasAnyClass = true; }
}

Expand All @@ -238,8 +240,8 @@
// Cache all the sections and their start/end positions (where the class starts and ends)
sections = [];

for( i=0; i<$sections.length; i++ ) {
var $section = $($sections[i]);
for( var ix=0; ix<$sections.length; ix++ ) {
var $section = $($sections[ix]);

sections.push({
element: $section,
Expand Down Expand Up @@ -271,14 +273,15 @@
var headerEnd = scrollTop + headerInfo.top + headerHeight;

// Reset the header status
for( ix in headers ) {
for( var headerClass in headers ) {
if( ! headers.hasOwnProperty(headerClass) ){ continue; }
// from == '' signals that the section is inactive
headers[ ix ].from = '';
headers[ ix ].progress = 0.0;
headers[ headerClass ].from = '';
headers[ headerClass ].progress = 0.0;
}

// Set the header status
for( ix in sections ) {
for( var ix = 0; ix < sections.length; ix++ ) {

// Todo: This isn't exactly the best code.

Expand Down Expand Up @@ -325,10 +328,11 @@
// Do some preprocessing to ensure a header is always shown (even if some sections haven't been assigned)
var totalProgress = 0.0;
var lastActiveClass = '';
for( ix in headers ) {
if( ! headers[ix].from === '' ){ continue; }
totalProgress += headers[ix].progress;
lastActiveClass = ix;
for( var headerClass in headers ) {
if( ! headers.hasOwnProperty(headerClass) ){ continue; }
if( ! headers[headerClass].from === '' ){ continue; }
totalProgress += headers[headerClass].progress;
lastActiveClass = headerClass;
}


Expand All @@ -344,8 +348,8 @@
}


for( ix in headers ) {

for( var ix in headers ) {
if( ! headers.hasOwnProperty(ix) ){ continue; }
if( ! headers[ix].from === '' ){ continue; }

var offset = (1.0 - headers[ix].progress) * 100.0;
Expand Down Expand Up @@ -388,7 +392,7 @@


// This works using requestAnimationFrame for better compatibility with iOS/Android
requestAnimationFrame = window.requestAnimationFrame || (function(){
var requestAnimationFrame = window.requestAnimationFrame || (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
Expand Down
2 changes: 1 addition & 1 deletion midnight.jquery.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 26 additions & 22 deletions midnight.jquery.src.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
((function ( $ ) {

"use strict";

$.fn.midnight = function( customOptions ) {

if( typeof customOptions !== "object" ) {
Expand All @@ -16,10 +18,8 @@
innerClass: 'midnightInner',
// The class used by the default header (useful when adding multiple headers with different markup).
defaultClass: 'default',
/*
// 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)
// 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)
classPrefix: ''
*/
};

$.extend(settings, customOptions);
Expand All @@ -45,10 +45,10 @@
var sections = [];

var getSupportedTransform = function() {
var prefixes = 'transform WebkitTransform MozTransform OTransform msTransform'.split(' ');
for(var i = 0; i < prefixes.length; i++) {
if(document.createElement('div').style[prefixes[i]] !== undefined) {
return prefixes[i];
var prefixes = ['transform','WebkitTransform','MozTransform','OTransform','msTransform'];
for(var ix = 0; ix < prefixes.length; ix++) {
if(document.createElement('div').style[prefixes[ix]] !== undefined) {
return prefixes[ix];
}
}
return false;
Expand Down Expand Up @@ -156,7 +156,8 @@



for( headerClass in headers ) {
for( var headerClass in headers ) {
if( ! headers.hasOwnProperty(headerClass) ){ continue; }
if( typeof headers[headerClass].element === 'undefined' ) {

// Create the outer clipping mask
Expand Down Expand Up @@ -204,7 +205,8 @@
$customHeaders.each(function(){
var $header = $(this);
var hasAnyClass = false;
for( headerClass in headers ) {
for( var headerClass in headers ) {
if( ! headers.hasOwnProperty(headerClass) ){ continue; }
if( $header.hasClass(headerClass) ){ hasAnyClass = true; }
}

Expand All @@ -228,8 +230,8 @@
// Cache all the sections and their start/end positions (where the class starts and ends)
sections = [];

for( i=0; i<$sections.length; i++ ) {
var $section = $($sections[i]);
for( var ix=0; ix<$sections.length; ix++ ) {
var $section = $($sections[ix]);

sections.push({
element: $section,
Expand Down Expand Up @@ -261,14 +263,15 @@
var headerEnd = scrollTop + headerInfo.top + headerHeight;

// Reset the header status
for( ix in headers ) {
for( var headerClass in headers ) {
if( ! headers.hasOwnProperty(headerClass) ){ continue; }
// from == '' signals that the section is inactive
headers[ ix ].from = '';
headers[ ix ].progress = 0.0;
headers[ headerClass ].from = '';
headers[ headerClass ].progress = 0.0;
}

// Set the header status
for( ix in sections ) {
for( var ix = 0; ix < sections.length; ix++ ) {

// Todo: This isn't exactly the best code.

Expand Down Expand Up @@ -315,10 +318,11 @@
// Do some preprocessing to ensure a header is always shown (even if some sections haven't been assigned)
var totalProgress = 0.0;
var lastActiveClass = '';
for( ix in headers ) {
if( ! headers[ix].from === '' ){ continue; }
totalProgress += headers[ix].progress;
lastActiveClass = ix;
for( var headerClass in headers ) {
if( ! headers.hasOwnProperty(headerClass) ){ continue; }
if( ! headers[headerClass].from === '' ){ continue; }
totalProgress += headers[headerClass].progress;
lastActiveClass = headerClass;
}


Expand All @@ -334,8 +338,8 @@
}


for( ix in headers ) {

for( var ix in headers ) {
if( ! headers.hasOwnProperty(ix) ){ continue; }
if( ! headers[ix].from === '' ){ continue; }

var offset = (1.0 - headers[ix].progress) * 100.0;
Expand Down Expand Up @@ -378,7 +382,7 @@


// This works using requestAnimationFrame for better compatibility with iOS/Android
requestAnimationFrame = window.requestAnimationFrame || (function(){
var requestAnimationFrame = window.requestAnimationFrame || (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
Expand Down

0 comments on commit a299d2b

Please sign in to comment.