前端js html css 基础巩固4

这是生成了不同的按钮 进行显示

每一个按钮对应一个音频

点击按钮 会播放对应的音频

直接上代码

复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');


        * {
            box-sizing: border-box;
        }

        body {
            background-color: rgb(161, 100, 223);
            font-family: 'Roboto', sans-serif;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: center;
            text-align: center;
            height: 100vh;
            overflow: hidden;
            margin: 0;
        }

        .btn {
            background-color: rebeccapurple;
            border-radius: 3px;
            border: none;
            color: #fff;
            margin: 1rem;
            padding: 1.5rem 3rem;
            font-size: 1.2rem;
            font-family: inherit;
            cursor: pointer;
        }

        .btn:hover {
            opacity: .9;
        }

        .btn:focus {
            outline: none;
        }
    </style>
</head>

<body>
    <audio id="applause" src="./sounds/applause.mp3"></audio>
    <audio id="boo" src="./sounds/boo.mp3"></audio>
    <audio id="gasp" src="./sounds/gasp.mp3"></audio>
    <audio id="tada" src="./sounds/tada.mp3"></audio>
    <audio id="victory" src="./sounds/victory.mp3"></audio>
    <audio id="wrong" src="./sounds/wrong.mp3"></audio>
    <div id="buttons"></div>

    <script>
        const sounds = ['applause', 'boo', 'gasp', 'tada', 'victory', 'wrong'];
        sounds.forEach((sound) => {
            const btn = document.createElement('button')
            btn.classList.add('btn')
            btn.innerText = sound

            btn.addEventListener('click', () => {
                stopSongs()
                document.getElementById(sound).play()
            })
            document.getElementById("buttons").appendChild(btn)

        })

        function stopSongs() {
            sounds.forEach((sound) => {
                const song = document.getElementById(sound)
                song.pause()
                song.currrentTime = 0

            })
        }
    </script>







</body>

</html>

主要是考验的是 当前 生成 元素的语法 以及 audio 标签的使用 相关的播放 暂停的 方法

相关推荐
珑墨1 天前
【大语言模型】从历史到未来
前端·人工智能·后端·ai·语言模型·自然语言处理·chatgpt
Qin_jiangshan1 天前
flutter实现透明导航栏
前端·javascript·flutter
亿元程序员1 天前
亿元Cocos小游戏实战合集
前端
黎雁·泠崖1 天前
C 语言预处理(下):宏与函数对比 +#/## 运算符 + 条件编译 + 头文件包含
c语言·开发语言
fengyucaihong_1231 天前
vue加声音播放
javascript·vue.js·ecmascript
2503_928411561 天前
12.26 小程序代码片段【添加WeaUI内容】
前端·微信小程序·小程序
华仔啊1 天前
Vue3 的设计目标是什么?相比 Vue2 做了哪些关键优化?
前端·vue.js
wjs20241 天前
PHP 文件上传
开发语言
鹏多多1 天前
前端纯js实现图片模糊和压缩
前端·javascript·vue.js
长安即是故里1 天前
搭建一个现代化视频聚合播放平台(含视频源)
前端·音视频·开源项目··部署教程