鱼C论坛

 找回密码
 立即注册
查看: 2396|回复: 4

[已解决]实现页面跳转bgm连续播放

[复制链接]
发表于 2020-2-13 15:46:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
额 就像问题里面说的。。。请问怎么样才能实现,我的1.html跳转到2.html后,背景音乐能够不间断的连续播放?
最佳答案
2020-2-14 21:23:57
本帖最后由 sukiwhip 于 2020-2-14 21:31 编辑
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cn.vuejs.org/js/vue.min.js"></script>
  <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
  <title>test</title>
</head>

<body>
  <div id="root">
    <router-view></router-view>
    <div id="music">
      <span>模拟歌曲播放计时:</span>
      <span v-text="musicTime"></span>
    </div>
  </div>
</body>

<template id="home">
  <div>
    <router-link to="/home/1">跳到页面1</router-link>
    <br><br>
    <router-link to="/home/2">跳到页面2</router-link>
    <br><br>
    <div>注意,点击后 url 很明显是有变化,路由跳转了,把这条线上面的内容自由选择咔嚓掉,就是两个单独的页面,里面路由再怎么跑自己喜欢就好了,下面的音乐计时在全局上,一点都不影响</div>
    <hr>
    <router-view></router-view>
  </div>
</template>

<template id="one">
  <div>
    我是页面1
  </div>
</template>

<template id="two">
  <div>
    我是页面2
  </div>
</template>

<script>
  const router = new VueRouter({
    routes: [{
      path: "/home",
      component: {
        template: "#home"
      },
      children: [{
        path: "1",
        component: {
          template: "#one"
        }
      }, {
        path: "2",
        component: {
          template: "#two"
        }
      }]
    }, {
      path: "*",
      redirect: "/home/1"
    }]
  })

  new Vue({
    el: "#root",
    router,
    data() {
      return {
        musicTime: 0
      }
    },
    methods: {
      musicGo() {
        let that = this;
        setInterval(function () {
          that.musicTime++
        }, 1000)
      }
    },
    mounted() {
      this.musicGo()
    }
  })
</script>

<style>
  #music {
    position: fixed;
    bottom: 100px;
    color: #f00;
  }
</style>

</html>

直接给你个 demo,把最佳答案抱走
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-13 16:22:02 | 显示全部楼层
比如我在html1播放了一首歌  转到html2页面接着刚刚的继续播放?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-13 17:40:25 | 显示全部楼层
目前光是页面跳转无法实现连续播放

需要后台才可以
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-14 20:03:08 | 显示全部楼层
用单页面刷新的框架开发前端,在框架内的路由跳转,最外层的音乐就不会被刷掉
比如用 vuejs
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-14 21:23:57 | 显示全部楼层    本楼为最佳答案   
本帖最后由 sukiwhip 于 2020-2-14 21:31 编辑
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cn.vuejs.org/js/vue.min.js"></script>
  <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
  <title>test</title>
</head>

<body>
  <div id="root">
    <router-view></router-view>
    <div id="music">
      <span>模拟歌曲播放计时:</span>
      <span v-text="musicTime"></span>
    </div>
  </div>
</body>

<template id="home">
  <div>
    <router-link to="/home/1">跳到页面1</router-link>
    <br><br>
    <router-link to="/home/2">跳到页面2</router-link>
    <br><br>
    <div>注意,点击后 url 很明显是有变化,路由跳转了,把这条线上面的内容自由选择咔嚓掉,就是两个单独的页面,里面路由再怎么跑自己喜欢就好了,下面的音乐计时在全局上,一点都不影响</div>
    <hr>
    <router-view></router-view>
  </div>
</template>

<template id="one">
  <div>
    我是页面1
  </div>
</template>

<template id="two">
  <div>
    我是页面2
  </div>
</template>

<script>
  const router = new VueRouter({
    routes: [{
      path: "/home",
      component: {
        template: "#home"
      },
      children: [{
        path: "1",
        component: {
          template: "#one"
        }
      }, {
        path: "2",
        component: {
          template: "#two"
        }
      }]
    }, {
      path: "*",
      redirect: "/home/1"
    }]
  })

  new Vue({
    el: "#root",
    router,
    data() {
      return {
        musicTime: 0
      }
    },
    methods: {
      musicGo() {
        let that = this;
        setInterval(function () {
          that.musicTime++
        }, 1000)
      }
    },
    mounted() {
      this.musicGo()
    }
  })
</script>

<style>
  #music {
    position: fixed;
    bottom: 100px;
    color: #f00;
  }
</style>

</html>

直接给你个 demo,把最佳答案抱走
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-2 14:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表