Skip to content

Instantly share code, notes, and snippets.

@DocWatson
Created November 14, 2014 19:02
Show Gist options
  • Save DocWatson/a950fe82b4c1fc275ca3 to your computer and use it in GitHub Desktop.
Save DocWatson/a950fe82b4c1fc275ca3 to your computer and use it in GitHub Desktop.
Transition <body> background-color on scroll
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
var sections = [];
function isScrolledIntoView(elem)
{
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
function getSections() {
$('section').each(function() {
sections.push($(this).attr("id"));
});
}
$(function(){
getSections();
$(window).scroll(function(){
$.each(sections, function(index, value){
if (isScrolledIntoView("#" + value)) {
if(!$('body').hasClass(value)) {
$('body').attr('class', '');
$('body').addClass(value);
}
}
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment