Created
October 10, 2012 11:49
-
-
Save thebrainroom/3865117 to your computer and use it in GitHub Desktop.
Detect if an element is in view and execute some fun stuff :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Function to determine whether an element is in view | |
function isScrolledIntoView(elem) { | |
var docViewTop = $(window).scrollTop(); | |
var docViewBottom = docViewTop + $(window).height(); | |
var elemTop = $(elem).offset().top; | |
var elemBottom = elemTop + $(elem).height(); | |
return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)); | |
} | |
$(document).ready(function() { | |
// Timeout functions for the animation classes | |
(function(){ | |
var myelement = $('.node-container'); // the element to act on if viewable | |
var timeout = 400; | |
$(window).scroll(function() { | |
if(myelement && isScrolledIntoView(myelement)) { | |
myelement.addClass("processed"); | |
// Apply class to each li within container | |
$('li', myelement).each(function(i,el) { | |
// Increase timeout | |
timeout = timeout + 75; | |
setTimeout(function(){ | |
$(el).addClass('animated bounceIn').show(); | |
setTimeout(function() { | |
$(el).removeClass('animated bounceIn'); | |
}, 500); | |
},timeout); | |
}); | |
timeout = null; | |
myelement = null; | |
setTimeout(function(){ | |
$('img').addClass('fadeIn').show(); | |
}, 1450); | |
} | |
}); | |
})(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment