亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

獲取隨機(jī)顏色

original 2018-11-21 16:31:27 267
abstrait:<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> </head> <style> a{display: block;height: 100px;width: 100px;border-radi

<!DOCTYPE html>

<html>


<head>

<meta charset="UTF-8">

<title></title>

</head>

<style>

a{display: block;height: 100px;width: 100px;border-radius: 50px;text-align: center;line-height: 100px;float: left;margin: 40px;text-decoration: none;}

</style>

<body>

<a href="">1</a>

<a href="">2</a>

<a href="">3</a>

<a href="">4</a>

</body>

<script type="text/javascript" src="../js/jquery-1.11.1.min.js" ></script>

<script>

// function change(tag){

var Ele = document.getElementsByTagName('a')

for (var i=0;i<Ele.length;i++) {

var r = Math.floor(Math.random()*256)

var g = Math.floor(Math.random()*256)

var b = Math.floor(Math.random()*256)

Ele[i].style.backgroundColor = 'rgb('+r+','+g+','+b+')'

}

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

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

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

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

})

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

//$(this).removeAttr('box-shadow')

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

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

})


// }

// change('a')


</script>

</html>

思路:先找到a標(biāo)簽,然后通過(guò)for循環(huán)獲取a標(biāo)簽的長(zhǎng)度,給每個(gè)遍歷的a標(biāo)簽通過(guò)rgb()設(shè)置隨機(jī)色

          Math.random()*256獲取0-256之間的一個(gè)隨機(jī)數(shù),通過(guò)Math.floor()返回一個(gè)整數(shù)

          當(dāng)鼠標(biāo)劃過(guò)時(shí)改變當(dāng)前標(biāo)簽的樣式,當(dāng)鼠標(biāo)離開(kāi)時(shí)還原標(biāo)簽樣式

總結(jié):Math.random()返回0-1之間的一個(gè)隨機(jī)數(shù)       Math.floor(x)返回小于等于x的最大整數(shù)    rgb()顏色值為整數(shù)

         rgb()寫(xiě)法   'rgb('+r+','+g+','+b+')'   字符串類(lèi)型,不需再加大引號(hào)

         box-shadow寫(xiě)法    '0 0 20px '+$bg

         $(this).removeAttr('box-shadow')無(wú)效    removeAttr移除的是元素自有屬性


Professeur correcteur:滅絕師太Temps de correction:2018-11-21 16:36:09
Résumé du professeur:完成的很棒,主要是理解的好,好習(xí)慣繼續(xù)保持!

Notes de version

Entrées populaires