js 控制音频音量的大小

本文介绍了一个使用HTML和jQuery实现的音频播放器,该播放器具有基本的播放功能,并通过两个按钮实现了音量的上下调节。通过监听'canplay'事件,确保音频加载完成后自动播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
    <div>
        <audio id="audio" controls  src="http://m10.music.126.net/20170719161110/02863ba74071062bce6dd56bf5907797/ymusic/450e/0b69/b118/e3a5be41869989ca3723c8cca2bf82ad.mp3">
            
        </audio>
        <button id="down">↓</button>
        <button id="up">↑</button>
    </div>
</body>
<script>
    $(function(){
        let vol = $('#audio')[0].volume;
        $('#audio').on('canplay',function(){
            this.play()
        });
        $('#down').click(function(){
            console.log( vol)
            vol =vol>0?(vol*10 -1)/10:0;
            $('#audio')[0].volume = vol;
        })
          $('#up').click(function(){
          console.log( vol)
            vol =vol<1?(vol*10 +1)/10:1;
            $('#audio')[0].volume = vol;
        })
    })
</script>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值