這篇文章主要講解了用jQuery實現(xiàn)抽獎程序的方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
目前創(chuàng)新互聯(lián)已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計、鐵山網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
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. 代碼實現(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)容,是不是對用jQuery實現(xiàn)抽獎程序的方法有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章名稱:用jQuery實現(xiàn)抽獎程序的方法
文章路徑:http://www.chinadenli.net/article36/gpcpsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、App開發(fā)、移動網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)