I want to make an background color animation change when the user gets to the specific section.
Here is the jQuery code I wrote:
var initialColors = [];
$('section.changecolorbg').each(function(i){
initialColors[i] = $(this).css("backgroundColor");
});
$(window).scroll(function() {
$('section.changecolorbg').each(function(i){
if(isScrolledIntoView($(this))){
var bgc = initialColors[i];
$(this).parent().children('.changecolorbg').each(function(){
$(this).css("backgroundColor", bgc);
});
}
})
});
function isScrolledIntoView(elem)
{
var hT = elem.offset().top,
hH = elem.outerHeight(),
wH = $(window).height(),
wS = $(this).scrollTop() + 200;
return (wS > (hT+hH-wH))
}
The sections will have a background-color initially, this is why I saved them in a variable.
The problem with this is that it's working pretty slow. I think is that because all the checking needs to be done in the .scroll
function.
Is there a way I can improve the code?
P.S. The effect I'm trying to achieve is same as on http://sfcd.com/