Created
November 14, 2014 19:02
-
-
Save DocWatson/a950fe82b4c1fc275ca3 to your computer and use it in GitHub Desktop.
Transition <body> background-color on scroll
This file contains 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
<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