h5背景音乐 <music>

这篇博客介绍了如何使用CSS动画实现音乐图片的旋转效果,并通过JavaScript实现点击切换音乐播放与暂停,以及监听首次触摸播放音乐的功能。同时,代码示例展示了在微信环境下自动播放音乐的处理方式。

 音乐图片旋转动画

#musicImage{
            animation: rotateArround 3.5s linear 3s infinite alternate;
            -webkit-animation: rotateArround 3.5s linear 3s infinite alternate;
            -moz-animation: rotateArround 3.5s linear 3s infinite;
            -ms-animation: rotateArround 3.5s linear 3s infinite;
            -o-animation: rotateArround 3.5s linear 3s infinite;
        }
        @keyframes rotateArround {
            from {transform: rotateZ(0deg);}
            to {transform: rotateZ(360deg);}
        }

        @-webkit-keyframes rotateArround {
            from {-webkit-transform: rotate(0deg);
            }
            to {-webkit-transform: rotate(360deg);}
        }

        @-moz-keyframes rotateArround {
            from {-moz-transform: rotate(0deg);}
            to {-moz-transform: rotate(360deg);}
        }

        @-ms-keyframes rotateArround {
            from {-ms-transform: rotate(0deg);}
            to {-ms-transform: rotate(360deg);}
        }

        @-o-keyframes rotateArround {
            from {-o-transform: rotate(0deg);}
            to {-o-transform: rotate(360deg);}
        }

 进入页面监听 第一次触摸屏幕播放音乐

标签 自动播放 第一次禁音 触摸屏幕解除禁音

<audio id="music" style="display: none" class="musicControl" autoplay loop muted src="https://xxxxx/victory.mp3"></audio>
<img id="musicImage" style="position: fixed;z-index: 999;height:40px;right: 30px;top: 30px;" src="../static/webapp/mp3/icon_pause@3x.png" alt=""/>
<script>

    let musicImage = document.getElementById('musicImage');
    let is = 1;
    musicImage.addEventListener('click', () => {
        let music = document.getElementById('music');
        if (is === 0) {
            is = 1;
            musicImage.src = '../static/webapp/mp3/icon_pause@3x.png';
            music.play();
        } else {
            is = 0;
            musicImage.src = '../static/webapp/mp3/icon_play@3x.png';
            music.pause();
        }
    });
    if (typeof WeixinJSBridge == "undefined") {
        document.addEventListener('WeixinJSBridgeReady', function () {
            document.getElementById('music').play();
        }, false);
    } else {
        WeixinJSBridge.invoke("getNetworkType", {}, function () {
            document.getElementById('music').play();
        });
    }
    let play = document.addEventListener('touchstart',function () {
        let music = document.getElementById('music');
        music.muted=false;
        musicImage.src = '../static/webapp/mp3/icon_pause@3x.png';
        document.removeEventListener('touchstart',play);
    });


</script>

 

<style> body { margin: 0; padding: 0; font-family: "微软雅黑", sans-serif; background-image: url("20210709-150306-9826.png"); /* 请将这里替换为你这张图片的实际路径,比如如果图片和HTML文件同目录,就写图片文件名 */ background-size: contain; background-repeat: no-repeat; background-attachment: fixed; color: #333; } .header { background: linear-gradient(135deg, #4ecdc4, #556270); background-image: url("20210709-150306-9826.png"); background-size: cover; background-repeat: no-repeat; color: white; padding: 90px 0; text-align: center; } .header h1 { font-size: 2.5em; margin: 0; } .header p { font-size: 1.2em; opacity: 0.9; margin: 10px 0 0; } .navbar { background: #333; padding: 10px 0; } .navbar a { color: white; text-decoration: none; padding: 8px 15px; margin: 0 5px; border-radius: 4px; transition: background 0.3s; } .navbar a:hover { background: #556270; } .row { display: flex; gap: 20px; margin: 20px; } .side, .main { padding: 20px; box-sizing: border-box; border-radius: 8px; background: #f5f5f5; } .side { flex: 1; } .main { flex: 3; } h2 { color: #4ecdc4; border-bottom: 2px solid #4ecdc4; padding-bottom: 10px; margin-top: 0; } .fakingimg { background: #ddd; padding: 10px; border-radius: 4px; } </style> <div class="header"> <h1>=……=的自由个人博客</h1> <p>欢迎来到我的博客!!</p > </div> <div class="navbar"> <a href=" ">首页</a > <a href="https://music.163.com/#/artist?id=30846" target="_blank">网易云林俊杰</a > <a href="https://www.xiaohongshu.com/" target="_blank">小红书</a > <a href="#" class="right">精选博文</a > </div> <div class="row"> <div class="side"> <h2>关于我</h2> <h5>我的照片</h5> < img src="你的头像.jpg" alt="我的头像" style="width: 100%; border-radius: 50%; margin-bottom: 10px;"> <div class="fakingimg" style="height: 200px;"> 大家好,我是陈玉洁~ 喜欢音乐、旅行和手工,这里是我的小小世界,记录生活里的美好瞬间。 </div> <p>关于我的介绍</p > <h3>更多内容</h3> <p>我的更多内容</p > </div> <div class="main"> <h2>图片区</h2> <h5 style="top: 20px;">下面是我喜欢的图片</h5> <div style="display: flex; flex-wrap: wrap; gap: 10px;"> < img src="20210709-150306-9826.png" alt="20210709-150306-9826.png" style="width: 30%; border-radius: 8px;"> < img src="图片2.jpg" alt="喜欢的图片2" style="width: 30%; border-radius: 8px;"> < img src="图片3.jpg" alt="喜欢的图片3" style="width: 30%; border-radius: 8px;"> </div> </div> <div class="main"> <h2>音乐区</h2> <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="100%" height="320" src="https://music.163.com/outchain/player?type=artist&id=30846&auto=1&height=320"> </iframe> </div> </div> <button onclick="window.scrollTo({top: 0, behavior: 'smooth'})" style="position: fixed; bottom: 20px; right: 20px; background: #4ecdc4; color: white; border: none; padding: 10px 15px; border-radius: 50%; cursor: pointer; display: none;" id="backToTop">↑</button> <script> window.addEventListener('scroll', function() { const btn = document.getElementById('backToTop'); if (window.scrollY > 300) { btn.style.display = 'block'; } else { btn.style.display = 'none'; } }); </script>解析一下这段代码,要求小白也能听懂
最新发布
10-13
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

十方来财

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

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

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

打赏作者

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

抵扣说明:

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

余额充值