我有用於在用戶向下滾動頁面時更改背景顏色的代碼。如何向下滾動時更改背景顏色並重復該過程?
演示:http://jsfiddle.net/Phb4B/前兩個div's.How
背景顏色變化做我調用的函數,在第三格的背景顏色變爲開始顏色,以便再次運行?
function call(){
$(document).scroll(function() {
scroll_pos = $(this).scrollTop();
if(scroll_pos >= animation_begin_pos && scroll_pos <= animation_end_pos) {
var percentScrolled = scroll_pos/(animation_end_pos - animation_begin_pos);
var newRed = beginning_color.red() + ((ending_color.red() - beginning_color.red()) * percentScrolled);
var newGreen = beginning_color.green() + ((ending_color.green() - beginning_color.green()) * percentScrolled);
var newBlue = beginning_color.blue() + ((ending_color.blue() - beginning_color.blue()) * percentScrolled);
var newColor = new $.Color(newRed, newGreen, newBlue);
//console.log(newColor.red(), newColor.green(), newColor.blue());
$('body').animate({ backgroundColor: newColor }, 0);
} else if (scroll_pos > animation_end_pos) {
$('body').animate({ backgroundColor: ending_color }, 0);
} else if (scroll_pos < animation_begin_pos) {
$('body').animate({ backgroundColor: beginning_color }, 0);
} else { }
});
}
真的很好的效果! –