抽奖功能
1. 主要需求
- 实现一个抽奖功能。
- 点击开始按钮后,开始按钮禁用,停止按钮取消禁用,小图片实现快速切换显示。
- 点击停止按钮后,停止按钮禁用,开始按钮取消禁用,小图片停止切换,将小图片在大图片位置显示。
- 小图片实现快速切换显示。
- 点击停止按钮后,淡入淡出选中图。
2. 素材页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>抽奖程序</title>
<script type="text/javascript" src="../js/jquery-1.11.1.js"></script>
<style type="text/css">
#small {
border: 1px solid blueviolet;
width: 75px;
height: 75px;
margin-bottom: 20px;
}
#smallPhoto {
width: 75px;
height: 75px;
}
#big {
border: 2px solid orangered;
width: 500px;
height: 500px;
position: absolute;
left: 300px;
top: 10px
}
#bigPhoto {
width: 500px;
height: 500px;
}
#btnStart {
width: 100px;
height: 100px;
font-size: 22px;
}
#btnStop {
width: 100px;
height: 100px;
font-size: 22px;
}
</style>
</head>
<body>
<div id="small">
<img id="smallPhoto" src="../img/man00.png"/>
</div>
<div id="big">
<img id="bigPhoto" src="../img/begin.jpg" />
</div>
<input id="btnStart" type="button" value="点击开始"/>
<input id="btnStop" type="button" value="点击停止"/ disabled>
<script type="text/javascript">
</script>
</body>
</html>
3. 代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>抽奖程序</title>
<script type="text/javascript" src="../js/jquery-1.11.3.js"></script>
<style type="text/css">
#small {
border: 1px solid blueviolet;
width: 75px;
height: 75px;
margin-bottom: 20px;
}
#smallPhoto {
width: 75px;
height: 75px;
}
#big {
border: 2px solid orangered;
width: 500px;
height: 500px;
position: absolute;
left: 300px;
top: 10px
}
#bigPhoto {
width: 500px;
height: 500px;
}
#btnStart {
width: 100px;
height: 100px;
font-size: 22px;
}
#btnStop {
width: 100px;
height: 100px;
font-size: 22px;
}
</style>
</head>
<body>
<div id="small">
<img id="smallPhoto" src="../img/man00.jpg"/>
</div>
<div id="big">
<img id="bigPhoto" src="../img/begin.jpg" />
</div>
<input id="btnStart" type="button" value="开始" onclick="gameStart()">
<input id="btnStop" type="button" value="停止" disabled onclick="gameOver()">
<script type="text/javascript">
let imgs = [
"../img/man00.jpg",
"../img/man01.jpg",
"../img/man02.jpg",
"../img/man03.jpg",
"../img/man04.jpg",
"../img/man05.jpg",
"../img/man06.jpg",
];
let counter = null;
function gameStart() {
$("#btnStart").prop("disabled",true);
$("#btnStop").prop("disabled",false);
let num = 0;
counter = setInterval(function () {
let index = num%imgs.length;
$("#smallPhoto").attr("src",imgs[index]);
num++;
},20);
}
function gameOver() {
$("#btnStop").prop("disabled",true);
$("#btnStart").prop("disabled",false);
clearInterval(counter);
num = 0;
let imgUrl = $("#smallPhoto").attr("src");
$("#bigPhoto").attr("src",imgUrl).hide();
$("#bigPhoto").fadeIn(2000);
}
</script>
</body>
</html>
- 示例素材:
原文链接:https://qwert.blog.csdn.net/article/details/105453318