web前端(四)

 一、结合抽奖案例完成随机点名程序

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机点名</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
 
        .box {
            position: relative;
            box-sizing: border-box;
            margin: 50px auto;
            border: 2px solid #ccc;
            padding: 20px;
            width: 500px;
            height: 200px;
            text-align: center;
        }
 
        .box h3 {
            line-height: 75px;
        }
 
        .box button {
            position: absolute;
            bottom: 30px;
            width: 60px;
            height: 30px;
        }
 
        .box .start {
            left: 130px;
        }
 
        .box .over {
            right: 130px;
        }
    </style>
</head>
 
<body>
    <div class="box">
        <h2>随机点名</h2>
        <h3>姓名:旭旭宝宝</h3>
        <button class="start">开始</button>
        <button class="over">结束</button>
    </div>
    <script>
        const arr = ['旭旭宝宝', '一阵雨', '大硕', '银雪', '泣雨', '张佳乐', '  B叔']
 
        const h3 = document.querySelector('.box h3')
        const start = document.querySelector('.start')
        const over = document.querySelector('.over')
 
        let timer
 
        let rand
 
        start.addEventListener('click', () => {
            start.disabled = 1
            over.disabled = 0
            timer = setInterval(() => {
                rand = Math.floor(Math.random() * arr.length)
                h3.innerHTML = `姓名:${arr[rand]}`
            }, 100);
        })
 
        over.addEventListener('click', () => {
            start.disabled = 0
            over.disabled = 1
            clearInterval(timer)
            //数组长度为 1 时,禁用按钮
            if (arr.length === 1)
                start.disabled = over.disabled = 1
            else
                arr.splice(rand, 1)
        })
 
    </script>
</body>
 
</html>

二、使用js及html、css完成秒表计时器

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>秒表计时器</title>
<style>
    body {
        font-family: Arial, sans-serif;
        background-color: #f4f4f4;
        margin: 0;
        padding: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
    }
    .container {
        text-align: center;
    }
    #timerDisplay {
        font-size: 36px;
        margin-bottom: 20px;
    }
    .clock {
        width: 200px;
        height: 200px;
        border: 8px solid #007bff;
        border-radius: 50%;
        position: relative;
        margin-bottom: 20px;
    }
    .hand {
        width: 2px;
        background-color: #007bff;
        position: absolute;
        top: 50%;
        left: 50%;
        transform-origin: bottom center;
    }
    .hour-hand {
        height: 50px;
    }
    .minute-hand {
        height: 70px;
    }
    .second-hand {
        height: 80px;
        background-color: red;
    }
    .center-dot {
        width: 8px;
        height: 8px;
        background-color: #007bff;
        border-radius: 50%;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    .buttons {
        display: flex;
        justify-content: center;
    }
    .button {
        background-color: #007bff;
        color: #fff;
        border: none;
        padding: 10px 20px;
        cursor: pointer;
        margin-right: 10px;
    }
</style>
</head>
<body>
 
<div class="container">
    <div id="timerDisplay">00:00:00</div>
    <div class="clock">
        <div class="hand hour-hand"></div>
        <div class="hand minute-hand"></div>
        <div class="hand second-hand"></div>
        <div class="center-dot"></div>
    </div>
    <div class="buttons">
        <button id="startButton" class="button">开始</button>
        <button id="pauseButton" class="button">暂停</button>
        <button id="resetButton" class="button">重置</button>
    </div>
</div>
 
<script>
var timer;
var hours = 0;
var minutes = 0;
var seconds = 0;
 
function startTimer() {
    timer = setInterval(updateTimer, 1000);
}
 
function pauseTimer() {
    clearInterval(timer);
}
 
function resetTimer() {
    clearInterval(timer);
    hours = 0;
    minutes = 0;
    seconds = 0;
    updateDisplay();
}
 
function updateTimer() {
    seconds++;
    if (seconds == 60) {
        seconds = 0;
        minutes++;
    }
    if (minutes == 60) {
        minutes = 0;
        hours++;
    }
    updateDisplay();
    updateClock();
}
 
function updateDisplay() {
    var displayHours = (hours < 10) ? "0" + hours : hours;
    var displayMinutes = (minutes < 10) ? "0" + minutes : minutes;
    var displaySeconds = (seconds < 10) ? "0" + seconds : seconds;
    document.getElementById("timerDisplay").innerText = displayHours + ":" + displayMinutes + ":" + displaySeconds;
}
 
function updateClock() {
    var hourHand = document.querySelector(".hour-hand");
    var minuteHand = document.querySelector(".minute-hand");
    var secondHand = document.querySelector(".second-hand");
 
    var hourRotation = (hours % 12) * 30 + minutes * 0.5;
    var minuteRotation = minutes * 6 + seconds * 0.1;
    var secondRotation = seconds * 6;
 
    hourHand.style.transform = `translate(-1px, -100%) rotate(${hourRotation}deg)`;
    minuteHand.style.transform = `translate(-1px, -100%) rotate(${minuteRotation}deg)`;
    secondHand.style.transform = `translate(-1px, -100%) rotate(${secondRotation}deg)`;
}
 
document.getElementById("startButton").addEventListener("click", startTimer);
document.getElementById("pauseButton").addEventListener("click", pauseTimer);
document.getElementById("resetButton").addEventListener("click", resetTimer);
</script>
 
</body>
</html>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚风替我看朝夕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值