Skip to content

Commit 2ae78eb

Browse files
committed
add float mode
1 parent 4ee3557 commit 2ae78eb

7 files changed

+167
-76
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Props are mostly the same as [APlayer's options](https://aplayer.js.org/#/home?i
4949
| music| Object | *required* | Music info for current playing music, see [Music info](https://github.com/SevenOutman/vue-aplayer#music-info) |
5050
| list | Array | `[]` | Music list to play and display. If list is not empty, music list panel will be shown, even if the only song in the list is identical to music prop. |
5151
| mini | Boolean | `false` | Mini mode |
52+
| float | Boolean | `false` | Float mode, in which you can drag the player around and leave it anywhere on your page |
5253
| autoplay | Boolean | `false` | Whether to autoplay. If more than one mutex player are set autoplay, only the first one will play. |
5354
| showlrc | Boolean | `false` | Whether to show lyrics or not |
5455
| mutex | Boolean | `false` | Pause other players when this player is playing |

src/components/aplayer-controller-progress.vue

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,9 @@
2626
</template>
2727

2828
<script>
29-
function getElementViewLeft (element) {
30-
let actualLeft = element.offsetLeft
31-
let current = element.offsetParent
32-
let elementScrollLeft
33-
while (current !== null) {
34-
actualLeft += current.offsetLeft
35-
current = current.offsetParent
36-
}
37-
elementScrollLeft = document.body.scrollLeft + document.documentElement.scrollLeft
38-
return actualLeft - elementScrollLeft
39-
}
29+
30+
31+
import {getElementViewLeft} from '../utils'
4032
4133
export default {
4234
props: ['loadProgress', 'playProgress', 'theme'],

src/components/aplayer-controller-volume.vue

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,10 @@
2626

2727
<script>
2828
import IconButton from './aplayer-iconbutton.vue'
29+
import {getElementViewTop} from '../utils'
2930
3031
const barHeight = 40
3132
32-
function getElementViewTop (element) {
33-
let actualTop = element.offsetTop
34-
let current = element.offsetParent
35-
let elementScrollTop
36-
while (current !== null) {
37-
actualTop += current.offsetTop
38-
current = current.offsetParent
39-
}
40-
elementScrollTop = document.body.scrollTop + document.documentElement.scrollTop
41-
return actualTop - elementScrollTop
42-
}
43-
4433
export default {
4534
components: {
4635
IconButton,

src/components/aplayer-thumbnail.vue

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<div
33
class="aplayer-pic"
44
:style="currentPicStyleObj"
5-
@click="$emit('toggleplay')"
5+
@mousedown="onDragBegin"
6+
@click="onClick"
67
>
78
<div class="aplayer-button" :class="playing ? 'aplayer-pause' : 'aplayer-play'">
89
<icon-button
@@ -14,6 +15,7 @@
1415
</template>
1516
<script>
1617
import IconButton from './aplayer-iconbutton.vue'
18+
import {getElementViewLeft, getElementViewTop} from '../utils'
1719
1820
export default {
1921
components: {
@@ -25,6 +27,17 @@
2527
type: Boolean,
2628
default: false,
2729
},
30+
enableDrag: {
31+
type: Boolean,
32+
default: false
33+
}
34+
},
35+
data () {
36+
return {
37+
hasMovedSinceMouseDown: false,
38+
dragStartX: 0,
39+
dragStartY: 0
40+
}
2841
},
2942
computed: {
3043
currentPicStyleObj () {
@@ -34,12 +47,45 @@
3447
}
3548
},
3649
},
50+
methods: {
51+
onDragBegin (e) {
52+
if (this.enableDrag) {
53+
this.hasMovedSinceMouseDown = false
54+
this.$emit('dragbegin')
55+
this.dragStartX = e.clientX
56+
this.dragStartY = e.clientY
57+
document.addEventListener('mousemove', this.onDocumentMouseMove)
58+
document.addEventListener('mouseup', this.onDocumentMouseUp)
59+
}
60+
},
61+
onDocumentMouseMove (e) {
62+
this.hasMovedSinceMouseDown = true
63+
this.$emit('dragging', {offsetLeft: e.clientX - this.dragStartX, offsetTop: e.clientY - this.dragStartY})
64+
},
65+
onDocumentMouseUp (e) {
66+
document.removeEventListener('mouseup', this.onDocumentMouseUp)
67+
document.removeEventListener('mousemove', this.onDocumentMouseMove)
68+
69+
this.$emit('dragend')
70+
},
71+
onClick() {
72+
if (!this.hasMovedSinceMouseDown) {
73+
this.$emit('toggleplay')
74+
}
75+
}
76+
}
3777
}
3878
</script>
3979

4080
<style lang="scss">
4181
@import "../scss/variables";
4282
83+
.aplayer-float {
84+
.aplayer-pic:active {
85+
cursor: move;
86+
}
87+
}
88+
4389
.aplayer-pic {
4490
position: relative;
4591
float: left;

src/demo/App.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@
6464
:list='list3'
6565
list-max-height="80px"
6666
/>
67-
<h3>Mini Mode</h3>
67+
<h3>Mini mode and float mode (try drag it around)</h3>
6868
<aplayer
6969
mini
70+
float
7071
mutex
7172
theme="#e6d0b2"
7273
mode="circulation"

src/utils.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,26 @@ export function warn (message) {
6363

6464
export function deprecatedProp(name, sinceVersion, alternative) {
6565
return warn(`'${name}' is deprecated since v${sinceVersion}, and will be removed in future releases, use '${alternative}' instead`)
66-
}
66+
}
67+
export function getElementViewLeft (element) {
68+
let actualLeft = element.offsetLeft
69+
let current = element.offsetParent
70+
let elementScrollLeft
71+
while (current !== null) {
72+
actualLeft += current.offsetLeft
73+
current = current.offsetParent
74+
}
75+
elementScrollLeft = document.body.scrollLeft + document.documentElement.scrollLeft
76+
return actualLeft - elementScrollLeft
77+
}
78+
export function getElementViewTop (element) {
79+
let actualTop = element.offsetTop
80+
let current = element.offsetParent
81+
let elementScrollTop
82+
while (current !== null) {
83+
actualTop += current.offsetTop
84+
current = current.offsetParent
85+
}
86+
elementScrollTop = document.body.scrollTop + document.documentElement.scrollTop
87+
return actualTop - elementScrollTop
88+
}

0 commit comments

Comments
 (0)