下是一個簡單的國慶風格的活動抽獎頁面的示例代碼。頁面中有一個醒目的十一標志,以及一些國慶相關的元素和活動信息。
html復制代碼
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>國慶抽獎活動</title> | |
<style> | |
body { | |
background-color: #f2f2f2; | |
font-family: Arial, sans-serif; | |
text-align: center; | |
} | |
.container { | |
max-width: 600px; | |
margin: 0 auto; | |
padding: 20px; | |
} | |
.logo { | |
width: 200px; | |
height: 200px; | |
background-image: url('path/to/logo.png'); | |
background-size: cover; | |
margin: 0 auto; | |
display: block; | |
} | |
.prizes { | |
list-style-type: none; | |
padding: 0; | |
} | |
.prize { | |
margin-bottom: 20px; | |
} | |
.prize-item { | |
font-weight: bold; | |
} | |
.button { | |
display: inline-block; | |
background-color: #ff0000; | |
color: #fff; | |
text-align: center; | |
padding: 10px 20px; | |
margin: 10px 2px; | |
cursor: pointer; | |
border: none; | |
border-radius: 5px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="logo"></div> | |
<h1>國慶抽獎活動</h1> | |
<p>歡慶國慶,贏取豐厚獎品!</p> | |
<ul class="prizes"> | |
<li class="prize"> | |
<div class="prize-item">iPhone 13</div> | |
<div class="prize-item">價值 6999 元</div> | |
</li> | |
<li class="prize"> | |
<div class="prize-item">Kindle Paperwhite</div> | |
<div class="prize-item">價值 1299 元</div> | |
</li> | |
<li class="prize"> | |
<div class="prize-item">國慶紀念品</div> | |
<div class="prize-item">價值 50 元</div> | |
</li> | |
</ul> | |
<a href="#" class="button">立即參與</a> | |
</div> | |
</body> | |
</html> |
動策劃人員策劃這個抽獎頁面,用于app內。
當時,這個轉盤布局我踩坑了,我本以為這么簡單的布局應該不用絕對定位的,是我想多了!然后改為絕對定位來實現,因為要簡單些。
抽獎轉盤
一、九個格子和開始按鈕,頁面布局的實現思路
這個用絕對定位,小格子相對于大轉盤定位,這個我就給個簡單例子就好了哈,我相信你們能懂起的,如果沒理解到我再詳說。
標注
如上圖所示,大框為父容器,九個小格子為子容器
<div class="parent"> <div class="child child1"></div> <div class="child child2"></div> <div class="child child3"></div> ....... <div class="child child9" id="start"></div> </div> <style> .parent{ position: relative; .child{ position: absolute; } .child1{ top: 0; left: 0; } ...... .active{ background-color: darkgoldenrod; } } </style>
二、轉動效果實現:(下面貼出vue文件的html和js代碼,css代碼沒有。因為全貼出來太多了,如果想看詳細代碼,就到我的github倉庫去觀看或者下載)
轉動前.png
轉動后.png
app.vue
// template <template> <div id="rotary-table"> <div class="award" v-for="(award,index) in awards" :class="['award'+index,{'active': index==current}]"> {{award.name}} </div> <div id="start-btn" @click="start">開始</div> </div> </template> // js export default { name: 'get-award', data() { return { current: 0, awards: [ // 獎品數組 { id: 1, name: '空' }, { id: 2, name: '眼鏡' }, { id: 3, name: '包' }, { id: 4, name: '笨驢' }, { id: 5, name: '書' }, { id: 6, name: '手鏈' }, { id: 7, name: '美女' }, { id: 8, name: 'iphone' } ], speed: 200, // 速度 diff: 15, // 速度增加的值 award: {}, // 抽中的獎品 time: 0 // 記錄開始抽獎時的時間 }; }, methods: { start(){ // 開始抽獎 this.drawAward(); this.time = Date.now(); this.speed = 200; this.diff = 15; }, drawAward(){ // 請求接口, 這里我就模擬請求后的數據(請求時間為2s) setTimeout( () => { this.award = { id: '4', name: '笨驢', }; }, 2000 ); this.move(); }, move(){ window.timeout = setTimeout( () => { this.current++; if ( this.current > 7 ) { this.current = 0; } // 若抽中的獎品id存在,則開始減速轉動 if ( this.award.id && ( Date.now() - this.time ) / 1000 > 2 ) { this.speed += this.diff; // 轉動減速 // 若轉動時間超過4秒,并且獎品id等于小格子的獎品id,則停下來! if ( ( Date.now() - this.time ) / 1000 > 4 && this.award.id == this.awards[ this.current ].id ) { clearTimeout( window.timeout ); setTimeout( () => { alert( this.award.name ); }, 0 ); return; } // 若抽中的獎品不存在,則加速轉動 } else { this.speed -= this.diff; // 轉動加速 } this.move(); }, this.speed ); } } };
結尾發言
如果沒有理解到,可以留言問我哈。這是我專門寫的小demo,希望能幫到大家。謝謝!
代碼倉庫地址:https://github.com/lingziyb/get-award
年快樂
每當我們在元旦,年會的時刻,總是無法避免地出現抽獎環節。身為專業程序員的我們自然應當負起這份責任,確保這場抽獎活動能夠順利流暢地進行下去。然而,面對眾多待完成的工作任務,我們恐怕又需要進一步延長加班時間來應對。請別擔憂,在此,為您提供了一款卓越出色的現成抽獎網頁,可用于此次年度盛典的抽獎環節中。首先,讓我們共同欣賞一下實際應用表現吧!
抽獎效果
此頁面所有實現均采用最基本的JavaScript、html和jQuery技術完成,無需使用任何框架,開箱即用。
參與抽獎的數據需要提前設定好,在 js 目錄中,data.js 里,文件和結構如下:
人員數據
personArray.push({
id: 546,
image :"img/tx.png",
thumb_image :"img/tx.png",
name: "張三"
});
每個數據都按此標準結構構建即可。img 可以使用默認的, 也可以自行提供照片,id 需要隨機指定一個數字,保證不與列表內的其他數據相同即可。設置新數據后刷新后就能使用。
現有的功能里沒有作弊選項(不能設置必中項),而且一個人可以重復多次中獎,因此需要添加以下內容來擴展。
# 編輯lucy.js
# 在Obj 對象下定義兩個變量
Obj.writeList = [0] # 用于存放白名單列表
Obj.hasLuck = [] # 用于記錄已中獎下標
# 新增去重函數
function doRepeatResult(num){
console.log(Obj.hasLuck.length, personArray.length)
if(Obj.hasLuck.includes(num)){
console.log('存在重復中獎, 重新抽獎', num )
num = num - 1
if(num < 0){
num = num + personArray.length
}
if(Obj.hasLuck.length >= personArray.length){
return 0
}
return doRepeatResult(num)
}
return num
}
# 重寫中獎隨機函數
function randomLuckyArr() {
Obj.luckyResult = [];
for (var i = 0; i < Obj.luckyNum; i++) {
var random = Math.floor(Math.random() * personArray.length);
if (Obj.luckyResult.indexOf(random) == -1) {
if(Obj.writeList.length > 0 && Obj.hasLuck.length > 2){
random = Obj.writeList[0]
random = doRepeatResult(random)
Obj.luckyResult.push(random)
Obj.writeList.shift()
} else {
random = doRepeatResult(random)
Obj.luckyResult.push(random)
}
Obj.hasLuck.push(random)
} else {
i--;
}
console.log("已中獎下標", Obj.hasLuck)
}
}
在上述代碼中, 提供一個去重函數,并使用遞歸調用,保證每次數據都做去重校驗。如果數據有重復,則使用當前下標減一,如果小于 0 則,直接將下標調到最大,如果所有人都中獎了, 則始終返回第一個人的結果。
在randomLuckyArr 中, 則調用去重函數,并將每次中獎的數據都保存到 hasLuck 中。
對于需要暗箱的內容的內容則在luckyResult.push 之前,寫好相應的邏輯即可,如上代碼中,就是抽 3 回后保證第 4 回,中獎的人為writeList 中的(暗箱雖好,可不要貪杯[靈光一閃])。
有需要的朋友請來個三連獲取。
#文章首發挑戰賽#
#頭條創作挑戰賽#
#挑戰30天在頭條寫日記#
#我的2023年終總結#
*請認真填寫需求信息,我們會在24小時內與您取得聯系。