-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.vue
164 lines (156 loc) · 3.43 KB
/
index.vue
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<template>
<div
class="marquee-container"
:class="{ vertical: direction === 'vertical' }"
@mouseenter="handlePause"
@mouseleave="handleStart"
>
<div class="inner" :style="animateTime">
<slot />
</div>
<div v-if="showCopy" class="inner" :style="animateTime">
<slot />
</div>
</div>
</template>
<script>
import { getConfig } from '../../config'
const TAG = 'PiMarquee'
const { marquee } = getConfig()
export default {
name: TAG,
props: {
// 控制方向
direction: {
// vertical or horizontal
type: String,
default: marquee.direction
},
// 动画移动速度:每毫秒移动多少像素
speed: {
type: Number,
// 0.2s
default: marquee.speed
},
// 是否支持鼠标移上去暂停滚动
hoverPause: {
type: Boolean,
// true
default: marquee.hoverPause
}
},
data() {
return {
children: [],
animateTime: '',
showCopy: false
}
},
watch: {
direction() {
this.children.forEach(i => (i.direction = this.direction))
this.updateTime()
},
duration: {
immediate: true,
handler() {
this.handlePause()
this.updateTime()
}
}
},
mounted() {
window.addEventListener('resize', this.updateTime)
},
beforeDestroy() {
window.removeEventListener('resize', this.updateTime)
},
methods: {
handlePause() {
if (!this.hoverPause) {
return
}
this.animateTime = 'animation: none'
},
handleStart() {
if (!this.hoverPause) {
return
}
this.updateTime()
},
updateTime() {
this.$nextTick(() => {
const containerWidth = this.$el.clientWidth
const containerHeight = this.$el.clientHeight
const inner = this.$el.children[0]
if (!inner) {
// 没有marquee-item 则直接停止动画 隐藏拷贝
this.handlePause()
this.showCopy = false
return
}
if (this.direction !== 'vertical') {
if (inner.offsetWidth <= containerWidth) {
this.handlePause()
this.showCopy = false
} else {
const time = inner.offsetWidth / this.speed / 1000
this.animateTime = `animation-duration: ${time}s`
this.showCopy = true
}
} else {
if (inner.offsetHeight <= containerHeight) {
this.animateTime = 'animation: none'
this.showCopy = false
} else {
const time = inner.offsetHeight / this.speed / 1000
this.animateTime = `animation-duration: ${time}s`
this.showCopy = true
}
}
})
}
}
}
</script>
<style lang="scss" scoped>
.marquee-container {
width: 100%;
white-space: nowrap;
overflow: auto hidden;
background: #ffffff;
}
.marquee-container > .inner {
display: inline-block;
height: 100%;
white-space: nowrap;
animation: roll 0s linear infinite;
}
.marquee-container.vertical {
height: 100%;
white-space: normal;
overflow: hidden auto;
}
.marquee-container.vertical > .inner {
height: auto;
width: 100%;
white-space: normal;
animation: roll-vertical 0s linear infinite;
}
@keyframes roll {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-100%);
}
}
@keyframes roll-vertical {
0% {
transform: translateY(0);
}
100% {
transform: translateY(-100%);
}
}
</style>