【发布时间】:2018-01-08 14:27:45
【问题描述】:
如何仅在先单击元素时触发鼠标移动? 我正在尝试将其用于音频播放器时间线。
.player__time--bar(@mousedown="setNewCurrentPosition($event)")
.slider(role="slider" aria-valuemin="0" :aria-valuenow="currentPosition" :aria-valuemax="trackTotalDuration" aria-orientation="horizontal")
.player__time--bar-current-position(:style="{width: (100 / (trackTotalDuration / currentPosition)) + '%'}")
方法:
setNewCurrentPosition(e) {
let tag = e.target
// if the click is not on 'slider', grab div with class 'slider'
if (e.target.className === 'player__time--bar') tag = e.target.firstElementChild
else if (e.target.className === 'player__time--bar-current-position') tag = e.target.parentElement
const pos = tag.getBoundingClientRect()
const seekPos = (e.clientX - pos.left) / pos.width
this.currentPosition = parseInt(this.trackTotalDuration * seekPos)
// updates the time in the html
this.$refs.player.currentTime = this.currentPosition
},
【问题讨论】:
-
您遇到什么错误?您提供的代码中的某些内容不起作用吗?
-
在时间栏上的 mousedown 上设置一个变量。在任何地方取消设置 mouseup 上的变量。将 mousemove 包裹在
if中,检查变量是否已设置。 -
@RoyJ 好的,这很可行,但是我如何调用函数 mousemove
-
@PatrickSteele 我所拥有的只是点击。如果您单击并拖动,我希望能够移动位置。基本上是模拟输入类型范围
标签: javascript vue.js vuejs2 mousedown