经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
VUE创建播发器组件并调用
来源:cnblogs  作者:少年。  时间:2019/9/20 9:27:56  对本文有异议

首先用vue-cli创建前端项目

参考:https://www.cnblogs.com/ouyangkai/p/11549290.html

新建play.vue文件

编写play组件

  1. <template>
  2. <div>
  3. <el-row>
  4. <el-col :span="4">
  5. <el-popover placement="top-start" trigger="hover">
  6. <div style="text-align: center">
  7. <el-progress
  8. color="#67C23A"
  9. type="circle"
  10. :percentage="music.volume"
  11. ></el-progress
  12. ><br />
  13. <el-button
  14. @click="changeVolume(-10)"
  15. icon="el-icon-minus"
  16. circle
  17. ></el-button>
  18. <el-button
  19. @click="changeVolume(10)"
  20. icon="el-icon-plus"
  21. circle
  22. ></el-button>
  23. </div>
  24. <el-button
  25. @click="play"
  26. id="play"
  27. slot="reference"
  28. :icon="music.isPlay ? 'el-icon-refresh' : 'el-icon-caret-right'"
  29. circle
  30. ></el-button>
  31. </el-popover>
  32. </el-col>
  33. <el-col :span="14" style="padding-left: 20px">
  34. <el-slider
  35. @change="changeTime"
  36. :format-tooltip="formatTime"
  37. :max="music.maxTime"
  38. v-model="music.currentTime"
  39. style="width: 100%;"
  40. ></el-slider>
  41. </el-col>
  42. <el-col
  43. :span="6"
  44. style="padding: 9px 0px 0px 10px;color:#909399;font-size: 13px"
  45. >
  46. {{ formatTime(music.currentTime) }}/{{ formatTime(music.maxTime) }}
  47. </el-col>
  48. </el-row>
  49. <audio ref="music" loop autoplay>
  50. <source src="../mp4/pkn0m-iuv0i.mp3" type="audio/mpeg" />
  51. </audio>
  52. </div>
  53. </template>
  54.  
  55. <script>
  56. export default {
  57. data () {
  58. return {
  59. music: {
  60. isPlay: false,
  61. currentTime: 0,
  62. maxTime: 0,
  63. volume: 100
  64. }
  65. }
  66. },
  67. mounted () {
  68. this.$nextTick(() => {
  69. setInterval(this.listenMusic, 1000)
  70. })
  71. },
  72. methods: {
  73. listenMusic () {
  74. if (!this.$refs.music) {
  75. return
  76. }
  77. if (this.$refs.music.readyState) {
  78. this.music.maxTime = this.$refs.music.duration
  79. }
  80. this.music.isPlay = !this.$refs.music.paused
  81. this.music.currentTime = this.$refs.music.currentTime
  82. },
  83. play () {
  84. if (this.$refs.music.paused) {
  85. this.$refs.music.play()
  86. } else {
  87. this.$refs.music.pause()
  88. }
  89. this.music.isPlay = !this.$refs.music.paused
  90. this.$nextTick(() => {
  91. document.getElementById('play').blur()
  92. })
  93. },
  94. changeTime (time) {
  95. this.$refs.music.currentTime = time
  96. },
  97. changeVolume (v) {
  98. this.music.volume += v
  99. if (this.music.volume > 100) {
  100. this.music.volume = 100
  101. }
  102. if (this.music.volume < 0) {
  103. this.music.volume = 0
  104. }
  105. this.$refs.music.volume = this.music.volume / 100
  106. },
  107. formatTime (time) {
  108. let it = parseInt(time)
  109. let m = parseInt(it / 60)
  110. let s = parseInt(it % 60)
  111. return (m < 10 ? '0' : '') + parseInt(it / 60) + ':' + (s < 10 ? '0' : '') + parseInt(it % 60)
  112. }
  113. }
  114. }
  115. </script>
View Code

以上代码直接复制保存即可

 

 引用组件,在app.vue 中的 script 导入组件,并且在components中声明

  1. <script>
  2. import play from '../src/components/play'
  3. export default {
  4. name: 'App',
  5. components: {
  6. play
  7. }
  8. }
  9. </script>

 

在app.vue  template 里面的div 里面添加自定义play组件

  1. <play />

 

运行项目:npm run dev

成功引用!

原文链接:http://www.cnblogs.com/ouyangkai/p/11549673.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号