技术宅

当前位置:首页 > 网站教程 > 代码学习

代码学习

网站添加随机BGM背景音乐代码

时间:10-08 作者:
每当网页刷新及打开一次随机播放一首BGM内容十首音乐,网易云、酷狗音乐外链,可以自定义更换,首先,给博客侧边栏加一个空div:<div id="music"></div>
然后就是js了,下面奉上js代码:
每当网页刷新及打开一次随机播放一首BGM内容十首音乐,网易云、酷狗音乐外链,可以自定义更换,首先,给博客侧边栏加一个空div:<div id="music"></div>
然后就是js了,下面奉上js代码:

 
  1. <!--音乐内容可自行换-->
  2. <div id="luyumusic"> </div>
  3. <script>
  4. //还是要分开
  5. var m1 = "http://fs.pc.kugou.com/202009131950/1d645834a0eab1e796f26c9bdd574c18/G231/M06/00/17/Jw4DAF9MabmIQegcAAtJ97FDY1QAAAjigPARswAC0oP166.mp3";
  6. //不是因为寂寞才想你
  7. var m2 = "http://fs.pc.kugou.com/202009131952/5ab17e603c6b0849f7cd2d19e4f15a38/G187/M0A/02/11/W4cBAF3zmdGASfhpAHetlns32CY546.mp3";
  8. //七秒钟的记忆
  9. var m3 = "http://m7.music.126.net/20200913202022/e9647f3d3fa8cbae102779d146d60849/ymusic/5608/070b/0e52/ab50cc08820a0466700298bc61047eb9.mp3";
  10. //黄梅戏DJ
  11. var m4 = "http://m7.music.126.net/20200913202102/69e6651d34be2440ef85ac22df53df0f/ymusic/0652/5208/0553/188e33eab5eb7cf16e95297f62100e15.mp3";
  12. //你是人间四月天
  13. var m5 = "http://m8.music.126.net/20200913210602/d62c9137f921c66f491eae891b33529c/ymusic/5152/5552/030c/f860fa842eb8201274264f2d9eda6139.mp3";
  14. //爸爸妈妈
  15. var m6 = "http://m8.music.126.net/20200913210655/803e0fc5914faf5209101173289a3552/ymusic/78e7/1070/78e8/08e435a238de98cbdab01c6cd32f2743.mp3";
  16. //夏天的风
  17. var m7 = "http://m701.music.126.net/20200913210749/8dd4c571522b8f2e07b7d563a2f230b3/jdymusic/obj/w5zDlMODwrDDiGjCn8Ky/2180586519/98b2/4d51/b6d3/c8c65de6a0bf053a2cd94d1f9473d069.mp3";
  18. // 收敛
  19. var m8 = "http://m701.music.126.net/20200913210907/a8b47a8667f4652108b9d2e1fd0fe483/jdymusic/obj/w5zDlMODwrDDiGjCn8Ky/1982156329/01cb/e133/624f/d2778cef75e3bcadaf389b573ad39170.mp3";
  20. //所念皆星河
  21. var m9 = "http://m7.music.126.net/20200913203025/c125757b55c8c51cd292aac6d421a844/ymusic/020c/5652/075e/5fcded78c7a8df3f91e404ddbdaf226b.mp3";
  22. //你走后1.0
  23. var m10 = "http://m8.music.126.net/20200913211210/464f9206535c7452e200f5a12e82730b/ymusic/015c/0658/000e/664f4e822612bec1739b7d2803cb5f1d.mp3";
  24. var x = Math.round(Math.random() * 9); console.log(x); if (x == 0) x = m1 else if (x == 1) x = m2 else if (x == 2) x = m3 else if (x == 3) x = m4 else if (x == 4) x = m5 else if (x == 5) x = m6 else if (x == 6) x = m7 else if (x == 7) x = m8 else if (x == 8) x = m9 else x = m10 var obj = document.getElementById("luyumusic"); obj.innerHTML = "<audio src=" + """ + x + """ + " " + "height="0"" + "autoplay="true"" + "loop="true"" + ">" + "</audio>";</script>
  25. <!--音乐内容可自行换 -->