This repository has been archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTrunquee.vue
105 lines (86 loc) · 1.7 KB
/
Trunquee.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
<template>
<div
ref="text"
class="truncate"
:style="styles"
@mouseenter="onEnter"
@mouseleave="onLeave"
v-html="displayText"
></div>
</template>
<script>
export default {
props: {
'text': {
type: String,
required: true,
},
'speed': {
type: Number,
default: 85,
},
'cursor': {
type: String,
default: 'help',
},
},
data: () => ({
displayText: "",
interval: null,
}),
computed: {
styles() {
return {
cursor: this.interval ? this.cursor : 'default',
};
}
},
watch: {
text: {
immediate: true,
handler: function () {
if (!this.interval) this.resetText();
},
},
},
mounted() {
this.$text = this.$refs.text;
},
methods: {
onEnter() {
if (this.interval || !this.isTruncated()) return;
this.interval = setInterval(() => {
if (!this.isTruncated()) return clearInterval(this.interval);
this.displayText = this.displayText.slice(1);
while (this.displayText.charAt() === ' ') {
this.displayText = this.displayText.slice(1);
}
}, this.speed);
},
onLeave() {
if (this.interval) {
clearInterval(this.interval);
this.interval = false;
}
this.resetText();
},
isTruncated() {
return this.getTruncatedDistance() > 0;
},
getTruncatedDistance() {
return this.$text.scrollWidth - this.$text.clientWidth;
},
resetText() {
this.displayText = this.text;
},
},
}
</script>
<style scoped>
.truncate {
width: 100%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>