Home >Web Front-end >JS Tutorial >jquery randomly displays avatar code

jquery randomly displays avatar code

ringa_lee
ringa_leeOriginal
2018-05-10 16:20:121625browse

First analyze the implementation idea

A container is needed as the area where the avatar is displayed.
Avatar pictures are required inside the container for display.
Randomize the size, position, and level of each avatar.
The random range of the avatar position should be subtracted equal to the width and height of the avatar to limit the random range from overflowing the container.

css style

Copy code The code is as follows:

*{ margin:0; padding:0;} 
.Icon-Box{ width:960px; height:700px; margin:0 auto; position:relative;} 
.Icon-Box li{ position:absolute; list-style:none;} 
.Icon-Box li img{ width:100%;}

HTML

Copy code The code is as follows:

<ul class="Icon-Box"> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
<li><img src="xx.jpg" /></li> 
</ul>

jquery script

Copy codeThe code is as follows:

function randomICON(){ 
//获取LI作为随机展示的盒子 
var $ico = $(".Icon-Box li"); 
//获取显示容器的宽度 
var $width = $(".Icon-Box").width(); 
//获取显示容器的高度 
var $height = $(".Icon-Box").height(); 
//通过循环为每一个盒子设置单独的属性 
for(i=0;i < $ico.length;i++){ 
//随机一个个整数最小为10,将影响图片大小,层级位置,透明度,位置 
var zindex = Math.floor(Math.random()*110)+10; 
$ico.eq(i).css({"z-index":zindex+&#39;px&#39;, 
width:zindex+&#39;px&#39;, 
height:zindex+&#39;px&#39;, 
//随机宽高度减去zindex以防止溢出显示容器。 
left:Math.floor(Math.random()*($width-zindex))+"px", 
top:Math.floor(Math.random()*($height-zindex))+"px", 
opacity:zindex/100 
}); 
} 
} 
randomICON();


jquery randomly displays avatar code
The script inserted above may not be visible clearly, so I took a screenshot in the editor.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn