代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
width: 600px;
height: 300px;
position: relative;
border: 1px solid red;
p{
position: absolute;
left: 30%;
&.p1{
font-size: 30px;
top: 15%;
}
&.p2{
font-size: 25px;
top: 35%;
}
&.p3{
font-size: 20px;
top: 55%;
}
}
}
</style>
</head>
<body>
<div class="box">
<img src="https://20hpa4-1321289440.cos.ap-nanjing.myqcloud.com/teng/applet/static/3c5f630400804b2eadb0d61e69aef4cb.jpg" alt="">
<p class="p1">一等奖:<span>王恩雨</span></p>
<p class="p2">二等奖:<span>张三</span></p>
<p class="p3">三等奖:<span>李四</span></p>
</div>
<script>
let arr = ['王恩雨','张三','李四','陈亚茹','孙忆桐','李星辉','颜浩然','孔杰']
let spans = document.getElementsByTagName('span')
for(var i=0;i<3;i++){
let index= Math.floor(Math.random()*arr.length)
// console.log(index);
// console.log(spans[i]);
spans[i].innerHTML = arr[index]
arr.splice(index,1) //移除已经选中的语素
}
</script>
</body>
</html>
效果图:
标签:index,arr,抽奖,效果,top,JS,spans,font,size From: https://blog.csdn.net/yyyyyyysc/article/details/143116687