香港专业教育学院取得了剧本,但也包含了一些变化。在你的代码中,你可以追加多个类的'小',因为事件被每一个scrollmovement触发。我刚刚添加了一个类,我首先检查是否已经设置了这个类。如果它没有设置,那么我会改变滚动的颜色。 如果删除了“检查”,颜色会改变每scrollmovement,它可以是,如果你是不是非常酷的癫痫:)
// array with the colors, you can add the colors here.
var colors = ["red", "blue", "yellow", "black", "green"];
$(window).scroll(function() {
if ($(document).scrollTop() == 0)
{
//if scrollbar is at the top (doing nothing atm)
}
else if($("#header_nav").hasClass("colorSet"))
{
//if the colorSet class has been apended (remove this, if you want some fun :))
}
else
{
//first, add the class, so we know we do not have to walk tru this anymore.
$('#header_nav').addClass('colorSet');
// Apend a CSS background-color based on a rand function on our color array
$('#header_nav').css("background-color", colors[Math.floor(Math.random() * colors.length)]);
}
});
现在,如果你愿意,你可以改变的第一个if语句(scrollTop的() )。如果您在此处删除colorSet类,如果某人再次向下滚动 - >上 - >下,您将获得一个新的随机颜色。
对于随机颜色的多个类;
// array with the colors, you can add the colors here.
var colors = ["red", "blue", "yellow", "black", "green"];
$(window).scroll(function() {
if ($(document).scrollTop() == 0)
{
//if scrollbar is at the top (doing nothing atm)
}
else if($("#header_nav").hasClass("colorSet"))
{
//if the colorSet class has been apended (remove this, if you want some fun :))
}
else
{
//first, add the class, so we know we do not have to walk tru this anymore.
$('#header_nav').addClass('colorSet');
// Apend a CSS background-color based on a rand function on our color array
$('#header_nav').css("background-color", colors[Math.floor(Math.random() * colors.length)]);
$('.header').css("background-color", colors[Math.floor(Math.random() * colors.length)]);
$('.header.tiny').css("background-color", colors[Math.floor(Math.random() * colors.length)]);
}
});
对于多个类共享相同的颜色;
// array with the colors, you can add the colors here.
var colors = ["red", "blue", "yellow", "black", "green"];
$(window).scroll(function() {
if ($(document).scrollTop() == 0)
{
//if scrollbar is at the top (doing nothing atm)
}
else if($("#header_nav").hasClass("colorSet"))
{
//if the colorSet class has been apended (remove this, if you want some fun :))
}
else
{
//first, add the class, so we know we do not have to walk tru this anymore.
$('#header_nav').addClass('colorSet');
var color = colors[Math.floor(Math.random() * colors.length)];
// Apend a CSS background-color based on a rand function on our color array
$('#header_nav').css("background-color", color );
$('.header').css("background-color", color);
$('.header.tiny').css("background-color", color);
}
});
你是什么意思的确切的颜色?你的意思是预先确定的颜色,还是像'#3FA3E5'? –
就像一个列表,所以例如我会使用#ff0,#f0f,#0ff,#0f0等,脚本应该从这些颜色中选择一个。 – barnade95