2021-08-18 00:01:11 +02:00
|
|
|
<template>
|
2021-08-21 01:29:10 +02:00
|
|
|
<div class="relative" v-click-outside="clickOutside" @mouseover="mouseover" @mouseleave="mouseleave">
|
2022-06-19 17:04:15 +02:00
|
|
|
<div class="cursor-pointer text-gray-300 hover:text-white" @mousedown.prevent @mouseup.prevent @click="clickVolumeIcon">
|
|
|
|
<span class="material-icons text-2xl sm:text-3xl">{{ volumeIcon }}</span>
|
2021-08-18 00:01:11 +02:00
|
|
|
</div>
|
2021-08-21 01:29:10 +02:00
|
|
|
<transition name="menux">
|
|
|
|
<div v-show="isOpen" class="volumeMenu h-6 absolute bottom-2 w-28 px-2 bg-bg shadow-sm rounded-lg" style="left: -116px">
|
|
|
|
<div ref="volumeTrack" class="h-1 w-full bg-gray-500 my-2.5 relative cursor-pointer rounded-full" @mousedown="mousedownTrack" @click="clickVolumeTrack">
|
|
|
|
<div class="bg-gray-100 h-full absolute left-0 top-0 pointer-events-none rounded-full" :style="{ width: volume * trackWidth + 'px' }" />
|
|
|
|
<div class="w-2.5 h-2.5 bg-white shadow-sm rounded-full absolute pointer-events-none" :class="isDragging ? 'transform scale-125 origin-center' : ''" :style="{ left: cursorLeft + 'px', top: '-3px' }" />
|
|
|
|
</div>
|
2021-08-18 00:01:11 +02:00
|
|
|
</div>
|
2021-08-21 01:29:10 +02:00
|
|
|
</transition>
|
2021-08-18 00:01:11 +02:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
value: Number
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
isOpen: false,
|
|
|
|
isDragging: false,
|
2021-08-21 01:29:10 +02:00
|
|
|
isHovering: false,
|
|
|
|
posX: 0,
|
|
|
|
lastValue: 0.5,
|
|
|
|
isMute: false,
|
|
|
|
trackWidth: 112 - 20,
|
|
|
|
openTimeout: null
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
volume: {
|
|
|
|
get() {
|
|
|
|
return this.value
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
this.$emit('input', val)
|
|
|
|
}
|
|
|
|
},
|
2021-08-21 01:29:10 +02:00
|
|
|
cursorLeft() {
|
|
|
|
var left = this.trackWidth * this.volume
|
|
|
|
return left - 3
|
|
|
|
},
|
|
|
|
volumeIcon() {
|
|
|
|
if (this.volume <= 0) return 'volume_mute'
|
|
|
|
else if (this.volume <= 0.5) return 'volume_down'
|
|
|
|
else return 'volume_up'
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2022-02-17 22:19:50 +01:00
|
|
|
scroll(e) {
|
|
|
|
if (!e || !e.wheelDeltaY) return
|
|
|
|
if (e.wheelDeltaY > 0) {
|
|
|
|
this.volume = Math.min(1, this.volume + 0.1)
|
|
|
|
} else {
|
|
|
|
this.volume = Math.max(0, this.volume - 0.1)
|
|
|
|
}
|
|
|
|
},
|
2021-08-21 01:29:10 +02:00
|
|
|
mouseover() {
|
2022-02-17 22:19:50 +01:00
|
|
|
if (!this.isHovering) {
|
|
|
|
window.addEventListener('mousewheel', this.scroll)
|
|
|
|
}
|
2021-08-21 01:29:10 +02:00
|
|
|
this.isHovering = true
|
|
|
|
this.setOpen()
|
|
|
|
},
|
|
|
|
mouseleave() {
|
2022-02-17 22:19:50 +01:00
|
|
|
if (this.isHovering) {
|
|
|
|
window.removeEventListener('mousewheel', this.scroll)
|
|
|
|
}
|
2021-08-21 01:29:10 +02:00
|
|
|
this.isHovering = false
|
|
|
|
},
|
|
|
|
setOpen() {
|
|
|
|
this.isOpen = true
|
|
|
|
clearTimeout(this.openTimeout)
|
|
|
|
this.openTimeout = setTimeout(() => {
|
|
|
|
if (!this.isHovering && !this.isDragging) {
|
|
|
|
this.isOpen = false
|
|
|
|
} else {
|
|
|
|
this.setOpen()
|
|
|
|
}
|
|
|
|
}, 600)
|
|
|
|
},
|
2021-08-18 00:01:11 +02:00
|
|
|
mousemove(e) {
|
2021-08-21 01:29:10 +02:00
|
|
|
var diff = this.posX - e.x
|
|
|
|
this.posX = e.x
|
2021-08-18 00:01:11 +02:00
|
|
|
var volShift = 0
|
|
|
|
if (diff < 0) {
|
|
|
|
// Volume up
|
2021-08-21 01:29:10 +02:00
|
|
|
volShift = diff / this.trackWidth
|
2021-08-18 00:01:11 +02:00
|
|
|
} else {
|
|
|
|
// volume down
|
2021-08-21 01:29:10 +02:00
|
|
|
volShift = diff / this.trackWidth
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
var newVol = this.volume - volShift
|
|
|
|
newVol = Math.min(Math.max(0, newVol), 1)
|
|
|
|
this.volume = newVol
|
|
|
|
e.preventDefault()
|
|
|
|
},
|
|
|
|
mouseup(e) {
|
|
|
|
if (this.isDragging) {
|
|
|
|
this.isDragging = false
|
|
|
|
document.body.removeEventListener('mousemove', this.mousemove)
|
|
|
|
document.body.removeEventListener('mouseup', this.mouseup)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mousedownTrack(e) {
|
|
|
|
this.isDragging = true
|
2021-08-21 01:29:10 +02:00
|
|
|
this.posX = e.x
|
|
|
|
var vol = e.offsetX / this.trackWidth
|
2021-08-18 00:01:11 +02:00
|
|
|
vol = Math.min(Math.max(vol, 0), 1)
|
|
|
|
this.volume = vol
|
|
|
|
document.body.addEventListener('mousemove', this.mousemove)
|
|
|
|
document.body.addEventListener('mouseup', this.mouseup)
|
|
|
|
e.preventDefault()
|
|
|
|
},
|
|
|
|
clickOutside() {
|
|
|
|
this.isOpen = false
|
|
|
|
},
|
|
|
|
clickVolumeIcon() {
|
2021-08-21 01:29:10 +02:00
|
|
|
this.isMute = !this.isMute
|
|
|
|
if (this.isMute) {
|
|
|
|
this.lastValue = this.volume
|
|
|
|
this.volume = 0
|
|
|
|
} else {
|
|
|
|
this.volume = this.lastValue || 0.5
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
},
|
2021-10-23 03:08:02 +02:00
|
|
|
toggleMute() {
|
|
|
|
this.clickVolumeIcon()
|
|
|
|
},
|
2021-08-18 00:01:11 +02:00
|
|
|
clickVolumeTrack(e) {
|
2021-08-21 01:29:10 +02:00
|
|
|
var vol = e.offsetX / this.trackWidth
|
2021-08-18 00:01:11 +02:00
|
|
|
vol = Math.min(Math.max(vol, 0), 1)
|
|
|
|
this.volume = vol
|
|
|
|
}
|
|
|
|
},
|
2021-08-21 01:29:10 +02:00
|
|
|
mounted() {
|
|
|
|
if (this.value === 0) {
|
|
|
|
this.isMute = true
|
|
|
|
}
|
2022-02-17 22:19:50 +01:00
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
window.removeEventListener('mousewheel', this.scroll)
|
|
|
|
document.body.removeEventListener('mousemove', this.mousemove)
|
|
|
|
document.body.removeEventListener('mouseup', this.mouseup)
|
2021-08-21 01:29:10 +02:00
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
</script>
|