ScrollXView.vue
3.31 KB
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
<template>
<div class="timeline-content">
<div
class="left-mask mask"
v-show="!isAtLeftEnd"
>
<div
class="btn flex"
@click="scrollTo(-400)"
>
<i class="el-icon-arrow-left"></i>
</div>
</div>
<div
class="right-mask mask"
v-show="!isAtRightEnd"
>
<div
class="btn flex"
@click="scrollTo(400)"
>
<i class="el-icon-arrow-left"></i>
</div>
</div>
<el-scrollbar
class="timeline-scrollbar"
ref="timelineScrollbar"
>
<slot></slot>
</el-scrollbar>
</div>
</template>
<script type='text/ecmascript-6'>
export default {
name: 'ScrollXView',
data () {
return {
isAtLeftEnd: true, // 初始在最左侧
isAtRightEnd: false // 初始不在最右侧
}
},
mounted () {
this.$nextTick(() => {
this.checkScrollPosition();
const scrollbar = this.$refs.timelineScrollbar;
if (scrollbar && scrollbar.wrap) {
scrollbar.wrap.addEventListener('scroll', this.handleScroll);
}
})
},
beforeDestroy () {
const scrollbar = this.$refs.timelineScrollbar;
if (scrollbar && scrollbar.wrap) {
scrollbar.wrap.removeEventListener('scroll', this.handleScroll);
}
},
methods: {
// 检查当前滚动位置
checkScrollPosition () {
const scrollbar = this.$refs.timelineScrollbar;
if (!scrollbar || !scrollbar.wrap) return;
const { scrollLeft, scrollWidth, clientWidth } = scrollbar.wrap;
const maxScroll = scrollWidth - clientWidth;
this.isAtLeftEnd = scrollLeft <= 0;
this.isAtRightEnd = scrollLeft >= maxScroll - 1; // 允许1px误差
},
// 滚动事件处理
handleScroll () {
this.checkScrollPosition();
},
scrollTo (x) {
const scrollbar = this.$refs.timelineScrollbar;
if (scrollbar && scrollbar.wrap) {
scrollbar.wrap.scrollBy({ left: x, behavior: 'smooth' });
}
this.$nextTick(this.checkScrollPosition);
}
}
}
</script>
<style scoped lang='scss'>
.flex {
display: flex;
align-items: center;
}
.timeline-content {
width: 100%;
position: relative;
.mask {
// opacity: 0;
width: 72px;
height: 100%;
background: linear-gradient(
90deg,
#ffffff 0%,
#ffffffe6 51%,
#ffffff00 100%
);
position: absolute;
z-index: 3;
transition: all 0.15s;
.btn {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 32px;
height: 32px;
background: #ffffff;
box-shadow: 0 0 4px 0 #0000001a;
border-radius: 50%;
cursor: pointer;
justify-content: center;
.el-icon-arrow-left {
color: #222;
font-weight: 700;
font-size: 14px;
}
}
}
.left-mask {
top: 0;
left: 0;
.btn {
left: 20px;
}
}
.right-mask {
top: 0;
right: -1px;
transform: scaleX(-1);
.btn {
right: 20px;
}
}
&:hover {
.mask {
opacity: 1;
}
}
}
.timeline-scrollbar {
width: 100%;
::v-deep .el-scrollbar__wrap {
// overflow:auto hidden !important;
// margin-bottom: 0 !important;
}
}
</style>