我在jsfiddle上找到了這段偉大的代碼,用於使用jQuery和Color插件在兩個顏色onScroll之間淡入淡出。 我想要做的是能夠在頁面上向下滾動三個或更多顏色。 我已經嘗試過(失敗悲慘)自己編輯代碼,但對JS和jQuery來說太新了。 請幫忙嗎?多個顏色淡入淡出
$(文件)。就緒(函數(){
//** notice we are including jquery and the color plugin at
//** http://code.jquery.com/color/jquery.color-2.1.0.js
var scroll_pos = 0;
var animation_begin_pos = 0; //where you want the animation to begin
var animation_end_pos = 1000; //where you want the animation to stop
var beginning_color = new $.Color('rgb(210,50,98)'); //we can set this here, but it'd probably be better to get it from the CSS; for the example we're setting it here.
var ending_color = new $.Color('rgb(0,197,209)'); ;//what color we want to use in the end
$(document).scroll(function() {
scroll_pos = $(this).scrollTop();
if(scroll_pos >= animation_begin_pos && scroll_pos <= animation_end_pos) {
// console.log('scrolling and animating');
//we want to calculate the relevant transitional rgb value
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 { }
});
});
這裏是小提琴: http://jsfiddle.net/cgspicer/V4qh9/