利用前端寫一個(gè)隨機(jī)點(diǎn)名網(wǎng)頁(yè)完整代碼
一、CSS的樣式
用于實(shí)現(xiàn)網(wǎng)頁(yè)的樣式。
<style>
/* 全局樣式重置 */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* 頁(yè)面背景 */
body {
background-color: #ebf2ff;
}
/* 主容器樣式 */
.container {
margin: 250px auto; /* 垂直居中 */
width: 500px;
height: 300px;
background-color: #fff;
text-align: center;
border-radius: 20px;
box-shadow:5px 5px 5px rgb(180, 178, 178); /* 添加陰影效果 */
}
/* 標(biāo)題區(qū)域 */
.h {
margin-top: 20px;
}
h1 {
font-family: 'Times New Roman', Times, serif;
}
/* 顯示名字的區(qū)域 */
.name {
margin: 50px 0;
text-align: center;
}
.name .draw {
font-family: 'Times New Roman', Times, serif;
font-size: 40px;
color: red; /* 強(qiáng)調(diào)顯示的名字 */
}
/* 按鈕區(qū)域 */
.btns {
display: flex;
margin: 50px auto;
width: 400px;
justify-content: space-around; /* 按鈕均勻分布 */
}
.btns button {
border: none;
border-radius: 10px;
width: 80px;
height: 40px;
font-family: 'Times New Roman', Times, serif;
font-size: 20px;
}
/* 開(kāi)始按鈕樣式 */
.btns .start {
background-color: #90e2ae; /* 綠色系 */
}
.btns .start:hover {
background-color: #63e492;
cursor: pointer;
}
/* 結(jié)束按鈕樣式 */
.btns .end {
background-color: #f7a1a1; /* 紅色系 */
}
.btns .end:hover {
background-color: #f07777;
cursor: pointer;
}
/* 重置按鈕樣式 */
.btns .reset {
background-color: #9dc0fa; /* 藍(lán)色系 */
}
.btns .reset:hover {
background-color: #70a2f1;
cursor: pointer;
}
</style>二、JS的代碼
用于實(shí)現(xiàn)交互功能。
<script>
// 數(shù)據(jù):存儲(chǔ)待抽取的名單數(shù)組
let arr = ['張飛', '趙云', '狂鐵', '關(guān)羽', '曹操']
// 獲取DOM元素
const draw = document.querySelector('.draw') // 顯示名字的元素
const start = document.querySelector('.start') // 開(kāi)始按鈕
const end = document.querySelector('.end') // 結(jié)束按鈕
const reset = document.querySelector('.reset') // 重置按鈕
// 全局變量
let timeId = 0 // 用于存儲(chǔ)計(jì)時(shí)器ID,便于清除
let random = 0 // 存儲(chǔ)隨機(jī)生成的數(shù)組索引
// 初始化按鈕狀態(tài)
start.disabled = false // 開(kāi)始按鈕可用
end.disabled = true // 結(jié)束按鈕不可用
reset.disabled = true // 重置按鈕不可用
// 開(kāi)始抽取按鈕事件監(jiān)聽(tīng)
start.addEventListener('click', ()=>{
// 設(shè)置定時(shí)器,每80毫秒更新一次顯示的名字
timeId = setInterval(() => {
random = parseInt(Math.random() * arr.length) // 生成0到數(shù)組長(zhǎng)度-1的隨機(jī)整數(shù)
draw.innerHTML = arr[random] // 顯示隨機(jī)名字
}, 80)
// 更新按鈕狀態(tài)
start.disabled = true
end.disabled = false
reset.disabled = true
// 檢查是否只剩最后一個(gè)名字
if (arr.length === 1){
end.disabled = true
reset.disabled = false
alert('名單已抽取完!')
}
})
// 停止抽取按鈕事件監(jiān)聽(tīng)
end.addEventListener('click', ()=>{
clearInterval(timeId) // 清除定時(shí)器
end.disabled = true // 禁用結(jié)束按鈕
start.disabled = false // 啟用開(kāi)始按鈕
arr.splice(random, 1) // 從數(shù)組中移除已被抽中的名字
console.log(arr); // 調(diào)試用:打印剩余名單
})
// 重置按鈕事件監(jiān)聽(tīng)
reset.addEventListener('click', ()=>{
arr = ['張飛', '趙云', '狂鐵', '關(guān)羽', '曹操'] // 重置名單數(shù)組
clearInterval(timeId) // 清除可能存在的定時(shí)器
draw.innerHTML = "Here will show your name!" // 恢復(fù)默認(rèn)顯示文本
start.disabled = false // 啟用開(kāi)始按鈕
end.disabled = true // 禁用結(jié)束按鈕
reset.disabled = true // 禁用重置按鈕
})
</script>
三、完整代碼
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 基礎(chǔ)元信息 -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>隨機(jī)點(diǎn)名案例</title>
<style>
/* 全局樣式重置 */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* 頁(yè)面背景 */
body {
background-color: #ebf2ff;
}
/* 主容器樣式 */
.container {
margin: 250px auto; /* 垂直居中 */
width: 500px;
height: 300px;
background-color: #fff;
text-align: center;
border-radius: 20px;
box-shadow:5px 5px 5px rgb(180, 178, 178); /* 添加陰影效果 */
}
/* 標(biāo)題區(qū)域 */
.h {
margin-top: 20px;
}
h1 {
font-family: 'Times New Roman', Times, serif;
}
/* 顯示名字的區(qū)域 */
.name {
margin: 50px 0;
text-align: center;
}
.name .draw {
font-family: 'Times New Roman', Times, serif;
font-size: 40px;
color: red; /* 強(qiáng)調(diào)顯示的名字 */
}
/* 按鈕區(qū)域 */
.btns {
display: flex;
margin: 50px auto;
width: 400px;
justify-content: space-around; /* 按鈕均勻分布 */
}
.btns button {
border: none;
border-radius: 10px;
width: 80px;
height: 40px;
font-family: 'Times New Roman', Times, serif;
font-size: 20px;
}
/* 開(kāi)始按鈕樣式 */
.btns .start {
background-color: #90e2ae; /* 綠色系 */
}
.btns .start:hover {
background-color: #63e492;
cursor: pointer;
}
/* 結(jié)束按鈕樣式 */
.btns .end {
background-color: #f7a1a1; /* 紅色系 */
}
.btns .end:hover {
background-color: #f07777;
cursor: pointer;
}
/* 重置按鈕樣式 */
.btns .reset {
background-color: #9dc0fa; /* 藍(lán)色系 */
}
.btns .reset:hover {
background-color: #70a2f1;
cursor: pointer;
}
</style>
</head>
<body>
<!-- 主容器 -->
<div class="container">
<!-- 標(biāo)題區(qū)域 -->
<div class="h">
<h1>Draw names by random:</h1>
</div>
<!-- 顯示隨機(jī)名字的區(qū)域 -->
<div class="name">
<span class="draw">Here will show your name!</span>
</div>
<!-- 操作按鈕區(qū)域 -->
<div class="btns">
<button class="start">Start</button>
<button class="end">End</button>
<button class="reset">Reset</button>
</div>
</div>
<script>
// 數(shù)據(jù):存儲(chǔ)待抽取的名單數(shù)組
let arr = ['張飛', '趙云', '狂鐵', '關(guān)羽', '曹操']
// 獲取DOM元素
const draw = document.querySelector('.draw') // 顯示名字的元素
const start = document.querySelector('.start') // 開(kāi)始按鈕
const end = document.querySelector('.end') // 結(jié)束按鈕
const reset = document.querySelector('.reset') // 重置按鈕
// 全局變量
let timeId = 0 // 用于存儲(chǔ)計(jì)時(shí)器ID,便于清除
let random = 0 // 存儲(chǔ)隨機(jī)生成的數(shù)組索引
// 初始化按鈕狀態(tài)
start.disabled = false // 開(kāi)始按鈕可用
end.disabled = true // 結(jié)束按鈕不可用
reset.disabled = true // 重置按鈕不可用
// 開(kāi)始抽取按鈕事件監(jiān)聽(tīng)
start.addEventListener('click', ()=>{
// 設(shè)置定時(shí)器,每80毫秒更新一次顯示的名字
timeId = setInterval(() => {
random = parseInt(Math.random() * arr.length) // 生成0到數(shù)組長(zhǎng)度-1的隨機(jī)整數(shù)
draw.innerHTML = arr[random] // 顯示隨機(jī)名字
}, 80)
// 更新按鈕狀態(tài)
start.disabled = true
end.disabled = false
reset.disabled = true
// 檢查是否只剩最后一個(gè)名字
if (arr.length === 1){
end.disabled = true
reset.disabled = false
alert('名單已抽取完!')
}
})
// 停止抽取按鈕事件監(jiān)聽(tīng)
end.addEventListener('click', ()=>{
clearInterval(timeId) // 清除定時(shí)器
end.disabled = true // 禁用結(jié)束按鈕
start.disabled = false // 啟用開(kāi)始按鈕
arr.splice(random, 1) // 從數(shù)組中移除已被抽中的名字
console.log(arr); // 調(diào)試用:打印剩余名單
})
// 重置按鈕事件監(jiān)聽(tīng)
reset.addEventListener('click', ()=>{
arr = ['張飛', '趙云', '狂鐵', '關(guān)羽', '曹操'] // 重置名單數(shù)組
clearInterval(timeId) // 清除可能存在的定時(shí)器
draw.innerHTML = "Here will show your name!" // 恢復(fù)默認(rèn)顯示文本
start.disabled = false // 啟用開(kāi)始按鈕
end.disabled = true // 禁用結(jié)束按鈕
reset.disabled = true // 禁用重置按鈕
})
</script>
</body>
</html>四、運(yùn)行的結(jié)果
desktop 2025-10-24 17-11-53
五、其它說(shuō)明
如果你想要換成你的名單,請(qǐng)更改下面代碼中的數(shù)組。
let arr = ['張飛', '趙云', '狂鐵', '關(guān)羽', '曹操']
到此這篇關(guān)于利用前端寫一個(gè)隨機(jī)點(diǎn)名網(wǎng)頁(yè)的文章就介紹到這了,更多相關(guān)前端隨機(jī)點(diǎn)名網(wǎng)頁(yè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- javascript實(shí)現(xiàn)的一個(gè)隨機(jī)點(diǎn)名功能
- JS+CSS實(shí)現(xiàn)隨機(jī)點(diǎn)名(實(shí)例代碼)
- 使用javascript做的一個(gè)隨機(jī)點(diǎn)名程序
- js實(shí)現(xiàn)隨機(jī)點(diǎn)名系統(tǒng)(實(shí)例講解)
- JS實(shí)現(xiàn)課堂隨機(jī)點(diǎn)名和順序點(diǎn)名
- JS實(shí)現(xiàn)隨機(jī)點(diǎn)名器
- js實(shí)現(xiàn)隨機(jī)點(diǎn)名小功能
- JavaScript實(shí)現(xiàn)隨機(jī)點(diǎn)名程序
- js實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)點(diǎn)名器
- JavaScript實(shí)現(xiàn)隨機(jī)點(diǎn)名器
相關(guān)文章
JavaScript判斷數(shù)組重復(fù)內(nèi)容的兩種方法(推薦)
本文給大家介紹兩種JavaScript判斷數(shù)組重復(fù)內(nèi)容的方法(推薦)非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-06-06
用js實(shí)現(xiàn)的檢測(cè)瀏覽器和系統(tǒng)的函數(shù)
檢測(cè)各種瀏覽器、系統(tǒng)的JS代碼2009-04-04
利用Js+Css實(shí)現(xiàn)折紙動(dòng)態(tài)導(dǎo)航效果實(shí)例源碼
這篇文章主要給大家介紹了利用Js+Css實(shí)現(xiàn)折紙動(dòng)態(tài)導(dǎo)航的效果,實(shí)現(xiàn)后的效果非常不錯(cuò),文中給出了簡(jiǎn)單的介紹和完整的實(shí)例代碼,對(duì)大家具有一定的參考價(jià)值,有需要的朋友們下面來(lái)一起看看吧。2017-01-01
javascript 操作Word和Excel的實(shí)現(xiàn)代碼
javascript 操作Word和Excel的實(shí)現(xiàn)代碼, 需要的朋友可以參考下。2009-10-10
JS獲取瀏覽器版本及名稱實(shí)現(xiàn)函數(shù)
獲取瀏覽器名稱及版本信息,如果當(dāng)前瀏覽器是IE,彈出瀏覽器版本,否則彈出當(dāng)前瀏覽器名稱和版本,詳細(xì)實(shí)現(xiàn)代碼請(qǐng)參考本文2013-04-04
JavaScript復(fù)制變量三種方法實(shí)例詳解
這篇文章主要介紹了JavaScript復(fù)制變量三種方法實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01

