抽獎(jiǎng)小案例(js抽獎(jiǎng)概率+css3旋轉(zhuǎn)動(dòng)畫)
來(lái)源:NiceYours
發(fā)布時(shí)間:2018-07-02 15:24:32
閱讀量:1696
進(jìn)入界面后2s開(kāi)始旋轉(zhuǎn)抽獎(jiǎng),3s后停止,效果圖:
實(shí)現(xiàn)代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./dist/js/zepto.min.js"></script>
<style>
section {
position: relative;
width: 100%;
}
.one {
width: 100%;
}
.two {
position: absolute;
left: 0;
top: 0;
width: 100%;
}
/* 通過(guò)添加類名的方式來(lái)旋轉(zhuǎn) */
.two.jl200 {
/* 轉(zhuǎn)35deg就抽到200元,讓它轉(zhuǎn)十圈+3600deg */
transform: rotate(3635deg);
transition: all 3s;
}
.two.jl100 {
transform: rotate(3683deg);
transition: all 3s;
}
.two.jl50 {
transform: rotate(3745deg);
transition: all 3s;
}
.two.jl20 {
transform: rotate(3818deg);
transition: all 3s;
}
.two.jl10 {
transform: rotate(3904deg);
transition: all 3s;
}
.two.jl500 {
transform: rotate(3600deg);
transition: all 3s;
}
</style>
<script>
$(function () {
// 生成一個(gè)隨機(jī)數(shù)1~100
var num = Math.floor(Math.random() * 100);
// 定義一個(gè)字符串,也就是要添加的類名
var str = '';
// 計(jì)算概率
if (num <= 50) {
// num<=50表示有50%的概率抽到10元
str = 'jl10'
}else if (num < 71 ) {
// num->51~70,概率為20%
str = 'jl20'
} else if (num < 86) {
str = 'jl50'
}else if (num < 95) {
str = 'jl100'
} else if (num < 99) {
str = 'jl200'
}else if(num <= 100){
str = 'jl500'
}
setTimeout(function () {
// 頁(yè)面加載2s后.添加類名,開(kāi)始抽獎(jiǎng)
$('.two').addClass(str);
},2000)
})
</script>
</head>
<body>
<section>
<img class="one" src="./img/01.png" alt="">
<img class="two" src="./img/02.png" alt="">
</section>
</body>
</html>
原文地址https://blog.csdn.net/qq_42209630/article/details/80858809