jQuery中的随机颜色 [英] Random color in jQuery
问题描述
所以我把一个标题,当我向下滚动时最小化,并向上滚动时增长。当标题合拢时,它变为浅灰色,打开时更改颜色。这是我找到的代码:
$(window).scroll(function(){
if($ ).scrollTop()== 0){
$('#header_nav')。removeClass('tiny');
} else {
$('#header_nav')。addClass 'tiny');
}
});
我希望标题在刷新页面时随机更改颜色,但我想使用确切的颜色。我发现如何改变背景颜色,我可以为'微小的'头,但我太愚蠢的jQuery,所以我不能写色的随机数,然后将其插入到上面的代码。 p>
你能帮我吗?
感谢您的帮助:)
您可以在Jquery上使用这样的函数: / p>
$(document).ready(function(){
var back = [#ff0000,blue ,grey];
var rand = back [Math.floor(Math.random()* back.length)];
$('div')css ;
})
有var c $ c>你可以写所有你想要的确切颜色。然后,您可以为标题设置选择器,而不是
$('div')
。
检查此演示 http://jsfiddle.net/sJTHc/ 5 /
So i've put a header that minimizes when i scroll down, and grow back when scroll up. When the header collapses, it turns light gray, and when opens, changes it's color back. Here's the code i found:
$(window).scroll(function () {
if ($(document).scrollTop() == 0) {
$('#header_nav').removeClass('tiny');
} else {
$('#header_nav').addClass('tiny');
}
});
I want the header to change it's color randomly when i refresh the page, but i would like to use exact colors. I found out how to change the background color, i could, for the 'tiny' header, but i'm too dumb to jQuery yet, so i couldn't write the color randomizer and then insert it to the code above.
Would you help me? Thank you for your help in advance :)
Hi you can use a function like this on Jquery:
$(document).ready(function () {
var back = ["#ff0000","blue","gray"];
var rand = back[Math.floor(Math.random() * back.length)];
$('div').css('background',rand);
})
There on the var back
you can write all exact colors you want. Then instead of $('div')
you can set the selector for your header.
Check this demo http://jsfiddle.net/sJTHc/5/
这篇关于jQuery中的随机颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!