如何修改H5中的audio样式

本文详细介绍了使用HTML、CSS和JavaScript创建一个音频播放器的过程。包括如何设置播放器的样式,实现音频播放、暂停功能,以及播放结束后的状态更新。

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

HTML

<div class="btn-audio clear">
            <img src="image/audioBg.jpg" alt="" class="lf" style="width: 5.2rem;">
            <div class="lf">
                <p style="font-size: 1.2rem; color: #333;margin: 1rem 0 0.3rem 1.2rem;">我要吃肉肉</p>
                <p style="font-size: 1rem; color: #888; margin: 0 0 0 1.2rem">何曼婷 - 我要吃肉肉</p>
            </div>
            <img src="image/musicBg.png" alt="" style="width: 1rem;position: absolute;bottom: 0.5rem;right: 0.5rem;">
            <div class="mp3Box">
                <audio id="mp3Btn">
                    <source src="林玉涵,程辰%20-%20我要吃肉肉(Cover%20何曼婷).mp3" type="audio/mpeg" />
                </audio>
            </div>
        </div>

CSS

.btn-audio{
            width: 100%;
            height: 5.2rem;
            border: 1px solid #ebebeb;
            background-color: #fdfdfd;
            margin-bottom: 1rem;
            margin-top: 1rem;
            position: relative;
        }
        .mp3Box{
            width: 2.8rem;
            height: 2.8rem;
            position: absolute;
            top: 1.2rem;
            left: 1.2rem;
            background:url("image/close.png") no-repeat center bottom;
            background-size:cover;
            z-index: 100;
        }

JS

<script>
    $(function(){
        //播放完毕
        $('#mp3Btn').on('ended', function() {
            console.log("音频已播放完成");
            $('.mp3Box').css({'background':'url(image/close.png) no-repeat center bottom','background-size':'cover'});
        });
        //播放器控制
        var audio = document.getElementById('mp3Btn');
        audio.volume = .3;
        $('.mp3Box').click(function() {
            event.stopPropagation();//防止冒泡
            if(audio.paused){ //如果当前是暂停状态
                $('.mp3Box').css({'background':'url(image/open.png) no-repeat center bottom','background-size':'cover'});
                audio.play(); //播放
            }else{//当前是播放状态
                $('.mp3Box').css({'background':'url(image/close.png) no-repeat center bottom','background-size':'cover'});
                audio.pause(); //暂停
            }
        });
    })
</script>

修改前:

修改后:

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值