js加jq实现随机颜色

Original 2019-01-24 15:36:08 274
abstract:<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Page Title</title><script src="https://code.jquery.com/jquery-3.1.1.min.js"><

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>Page Title</title>

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

</head>

<style>

a{

float:left;

display:block;

margin:50px;

width:100px;

line-height:100px;

text-align: center;

height: 100px;

color: #fff;

border-radius: 50px;

text-decoration: none;

}

</style>

<body>

<a href=""></a>

<a href=""></a>

<a href=""></a>

<a href=""></a>

</body>

</html>

<script>

//改变标签背景色的函数

function changecolor(tag){

var len = document.getElementsByTagName(tag).length;

for(var i = 0;i<len;i++)

{

document.getElementsByTagName(tag)[i].style.backgroundColor = 'rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+')';

}

}

$(function(){

changecolor('a');

$('a').mouseover(function(){

$bg = $(this).css('backgroundColor');

$(this).css('box-shadow','0px 0px 50px '+$bg);

$(this).css('border-radius','20px');

})

$('a').mouseleave(function(){

$(this).css('box-shadow','none');

$(this).css('border-radius','50px');

})

})

</script>

//Math.random获取的是一个0到1的随机值


Correcting teacher:天蓬老师Correction time:2019-01-24 17:26:02
Teacher's summary:Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256) Math是js内置的重要对象之一, 可以实现很多事情,特别是一些动画效果,必须用到,多多练习

Release Notes

Popular Entries