自定义滑块Vue组件

1
2
3
4
5
6
7
8
9
10
11
12
<div class="audio">
     <audio id="audio" ref="audio" src="http://www.w3school.com.cn/i/horse.ogg"></audio>
     <div class="stopbtn" @click="play" v-if="flag"></div>
     <div class="playbtn" @click="paused" v-else></div>
     <div class="timebar" ref="timebare" @touchmove="timebarmove($event)" @click="clickbar($event)">
       <div class="bar" ref="bar" @touchstart="touchstart($event)">
         <span></span>
       </div>
       <div class="mask" ref="mask"></div>
     </div>
     <div class="time">{{ currentTime }} / {{ alltime }}</div>
   </div>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
data () {
    return {
      currentTime: 0,
      statu: false,
      ox: 0,
      left: 0,
      alltime: '',
      state: false,
      flag: true
    }
  },
  mounted () {
    var audio = document.getElementById('audio')
    audio.addEventListener('canplay', () => {
      this.alltime = audio.duration//获取总时长
    })
  },
  methods: {
    play () {
      this.flag = false
      let timer = setInterval(() => {
        this.currentTime += 1
        if (this.currentTime > this.alltime) {
          clearInterval(timer)
          this.currentTime = this.alltime
          this.flag = true
          this.currentTime = 0
        }
        this.$refs.bar.style.cssText = 'left:' + this.currentTime / this.alltime * 213 + 'px'
        this.$refs.mask.style.cssText = 'width:' + this.currentTime / this.alltime * 213 + 'px'
      }, 1000)
    },
    paused () {
      this.flag = false
    },
    touchstart (e) {
      this.ox = e.touches[0].pageX - this.left
      this.statu = true
    },
    timebarmove (e) {
      if (this.statu) {
        this.left = e.touches[0].pageX - Number(this.ox)
        if (this.left < 0) {
          this.left = 0
        }
        if (this.left > 213) {
          this.left = 213
        }
        this.$refs.bar.style.cssText = 'left:' + this.currentTime / this.alltime * 213 + 'px'
        this.$refs.mask.style.cssText = 'width:' + this.currentTime / this.alltime * 213 + 'px'
      }
    },
    clickbar (e) {
      if (!this.statu) {
        this.left = e.x - 82
        if (this.left < 0) {
          this.left = 0
        }
        if (this.left > 213) {
          this.left = 213
        }
        this.$refs.bar.style.cssText = 'left:' + this.currentTime / this.alltime * 213 + 'px'
        this.$refs.mask.style.cssText = 'width:' + this.currentTime / this.alltime * 213 + 'px'
      }
    },
    touchend () {
      this.statu = false
    }
  }

  

posted @   地铁程序员  阅读(2858)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示