用jQuery實現(xiàn)抽獎程序
更新時間:2020年04月12日 11:28:31 作者:Regino
這篇文章主要為大家詳細介紹了用jQuery實現(xiàn)抽獎程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了jQuery實現(xiàn)抽獎程序的具體代碼,供大家參考,具體內(nèi)容如下
1. 主要需求
- 實現(xiàn)一個抽獎功能。
- 點擊開始按鈕后,開始按鈕禁用,停止按鈕取消禁用,小圖片實現(xiàn)快速切換顯示。
- 點擊停止按鈕后,停止按鈕禁用,開始按鈕取消禁用,小圖片停止切換,將小圖片在大圖片位置顯示。
- 小圖片實現(xiàn)快速切換顯示。
- 點擊停止按鈕后,淡入淡出選中圖。
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. 代碼實現(xiàn)
<!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);
//定義計數(shù)變量
let num = 0;
//使用定時器實現(xiàn)小圖框快速切換圖片
counter = setInterval(function () {
//通過取余,循環(huán)得到數(shù)組得到索引
let index = num%imgs.length;
//修改小圖框中img的src即可
$("#smallPhoto").attr("src",imgs[index]);
//計數(shù)變量自增
num++;
},20);
}
//點擊結(jié)束
function gameOver() {
//禁用結(jié)束按鈕
$("#btnStop").prop("disabled",true);
//取消開始按鈕的禁用
$("#btnStart").prop("disabled",false);
//停止小圖框的抽獎(停止定時器)
clearInterval(counter);
//將計算變量重置為0
num = 0;
//獲取小圖框圖片地址
let imgUrl = $("#smallPhoto").attr("src");
//將抽獎結(jié)果顯示在大圖框中,并隱藏起來
$("#bigPhoto").attr("src",imgUrl).hide();
//將圖片進行淡出
$("#bigPhoto").fadeIn(2000);
}
</script>
</body>
</html>
示例素材:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- jQuery+PHP+Mysql實現(xiàn)抽獎程序
- jQuery實現(xiàn)簡單的抽獎游戲
- 用jQuery旋轉(zhuǎn)插件jqueryrotate制作轉(zhuǎn)盤抽獎
- jQuery自定義數(shù)值抽獎活動代碼
- 基于jQuery實現(xiàn)的雙11天貓拆紅包抽獎效果
- jquery.rotate.js實現(xiàn)可選抽獎次數(shù)和中獎內(nèi)容的轉(zhuǎn)盤抽獎代碼
- jquery 年會抽獎程序
- jquery 抽獎小程序?qū)崿F(xiàn)代碼
- jQuery實現(xiàn)轉(zhuǎn)動隨機數(shù)抽獎效果的方法
- jquery實現(xiàn)九宮格大轉(zhuǎn)盤抽獎
相關(guān)文章
使用jQuery的load方法設(shè)計動態(tài)加載及解決被加載頁面js失效問題
這篇文章主要介紹了使用jQuery的load方法設(shè)計動態(tài)加載及解決被加載頁面js失效問題,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03

