当前位置:   article > 正文

使用html5与js实现音乐播放器_html音乐手动播放器代码大全

html音乐手动播放器代码大全

实现效果如下:

1、HTML代码

  1. <div id="music">
  2. <div id="container">
  3. <h3 id="musicName">一月,银装轻舞-紫竹笛韵</h3>
  4. <img src="image/一月,银装轻舞-紫竹笛韵.jpg" id="musicImg">
  5. <audio src="./music/一月,银装轻舞-紫竹笛韵.mp3" controls id="audio"></audio>
  6. <div class="btn">
  7. <button id="play">play</button>
  8. <button id="pause">pause</button>
  9. <button id="prev">prev</button>
  10. <button id="next">next</button>
  11. </div>
  12. </div>
  13. </div>

2、播放暂停切换效果实现

  1. // 播放
  2. play.onclick = function(){
  3. if(audio.paused){
  4. audio.play();
  5. }
  6. }
  7. // 暂停
  8. pause.onclick = function(){
  9. if(audio.played){
  10. audio.pause();
  11. }
  12. }

自动切换下一首

  1. audio.addEventListener('ended',function(){
  2. next.onclick();
  3. },false);

3、歌曲切换时歌曲图片与当前背景也随着改变

  1. // 上一首
  2. prev.onclick = function(){
  3. num = (num + len - 1) % len;
  4. audio.src = './music/' + music[num] + '.mp3';
  5. musicName.innerHTML = music[num];
  6. bgImage.style.backgroundImage = 'url(./image/' + music[num] + '.jpg)';
  7. musicImg.src = './image/' + music[num] + '.jpg';
  8. audio.play();
  9. }
  10. // 下一首
  11. next.onclick = function(){
  12. num = (num + 1) % len;
  13. audio.src = './music/' + music[num] + '.mp3';
  14. musicName.innerHTML = music[num];
  15. bgImage.style.backgroundImage = 'url(./image/' + music[num] + '.jpg)';
  16. musicImg.src = './image/' + music[num] + '.jpg';
  17. audio.play();
  18. }

4、实现背景图片透明,内容不透明效果

  1. #music {
  2. width: 500px;
  3. height: 500px;
  4. border-radius: 10px;
  5. margin: 20px auto;
  6. position: relative;
  7. background: url(./image/一月,银装轻舞-紫竹笛韵.jpg) no-repeat;
  8. background-size: cover;
  9. text-align: center;
  10. }
  11. #container {
  12. position: absolute;
  13. left: 0;
  14. right: 0;
  15. top: 0;
  16. bottom: 0;
  17. width: 500px;
  18. height: 500px;
  19. text-align: center;
  20. background:rgba(255,255,255,0.6);
  21. }

demo预览效果:musicPlay

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/247673
推荐阅读
相关标签
  

闽ICP备14008679号