55 <div @mousedown.stop =" onTrackMousedown($event,1)"
66 @mouseenter.stop =" hoverH = true"
77 @mouseleave.stop =" hoverH = false"
8+ :style =" `background-color:${trackColor};`"
89 :class =" ['ohyeah-scroll-vertical-track-h',{'disabled': !isShowH},{'show': barHDown }]" >
910 <div @mousedown.stop =" onBarMousedown($event, 1)"
1011 ref =" ohyeahbarh"
11- :style =" `width:${(hoverH || barHDown )? breadth + 4 : breadth}px;height: ${barHTall}px;transform: translateY(${barHScrollTop}px);border-radius:${breadth}px`" ></div >
12+ :style =" `background-color:${thumbColor}; width:${(hoverH || barHDown )? breadth + 4 : breadth}px;height: ${barHTall}px;transform: translateY(${barHScrollTop}px);border-radius:${breadth}px`" ></div >
1213 </div >
1314 <!-- 横向滚动条 -->
1415 <div @mousedown.stop =" onTrackMousedown($event,2)"
1516 @mouseenter.stop =" hoverW = true"
1617 @mouseleave.stop =" hoverW = false"
18+ :style =" `background-color:${trackColor};`"
1719 :class =" ['ohyeah-scroll-vertical-track-w',{'disabled': !isShowW},{'show': barWDown }]" >
1820 <div @mousedown.stop =" onBarMousedown($event,2)"
1921 ref =" ohyeahbarw"
20- :style =" `height:${(hoverW || barWDown) ? breadth + 4 : breadth}px;width: ${barWTall}px;transform: translateX(${barWScrollLeft}px)`" ></div >
22+ :style =" `background-color:${thumbColor}; height:${(hoverW || barWDown) ? breadth + 4 : breadth}px;width: ${barWTall}px;transform: translateX(${barWScrollLeft}px)`" ></div >
2123 </div >
2224 <!-- 默认内容 -->
2325 <div ref =" ohyeahbody"
2931 </div >
3032</template >
3133<script >
32- // todo: 各种自定义:颜色,模式(固定小圆球),监听方法兼容性,超大数组测试,外层容器大小改变时监听
34+ // todo: 各种自定义:颜色V,监听方法兼容性V,外层容器大小改变时监听V,事件(到顶,到底,滚动的值),方法(设置滚动条位置),边缘检测,数量减少时滚动条位置处理
35+ import ElementResizeDetectorMaker from " element-resize-detector" ;
36+
3337export default {
3438 name: " ohyeah-scroll" ,
3539 data () {
@@ -51,17 +55,19 @@ export default {
5155 scaleH: 1 , // 比例尺
5256 scaleW: 1 ,
5357 hoverH: false , // H悬浮
54- hoverW: false // W悬浮
58+ hoverW: false , // W悬浮
59+ timer: null
5560 };
5661 },
5762 props: {
58- breadth: { type: Number , default: 6 } // bar宽窄
63+ breadth: { type: Number , default: 6 }, // bar宽窄
64+ trackColor: { type: String , default: " rgba(255, 255, 255, 0.5)" }, // 轨道背景色
65+ thumbColor: { type: String , default: " #7f7f7f" } // 滑块背景色
5966 },
6067 mounted () {
6168 console .log (" slot" , this .$slots , this .$refs );
6269 // 监听内部宽高变化,用于调整滚动条大小和位置
63- this .observer = new ResizeObserver (this .callback );
64- this .observer .observe (this .$refs .ohyeahbody );
70+ this .listenResize ();
6571 // 监听鼠标拖动事件
6672 document .addEventListener (" mousemove" , this .onBarDragMove );
6773 document .addEventListener (" mouseup" , this .onMouseUp );
@@ -70,12 +76,31 @@ export default {
7076 beforeDestroy () {
7177 // 卸载鼠标拖动事件
7278 document .removeEventListener (" mousemove" , this .onBarDragMove );
73- this .observer .disconnect ();
79+ if (window .ResizeObserver ) {
80+ this .observer .disconnect ();
81+ } else {
82+ this .observer .uninstall ();
83+ }
7484 this .observer = null ;
7585 },
7686
7787 computed: {},
7888 methods: {
89+ // 监听容器变化
90+ listenResize () {
91+ // 如果支持ResizeObserver就用这个
92+ if (window .ResizeObserver ) {
93+ this .observer = new ResizeObserver (this .callback );
94+ this .observer .observe (this .$refs .ohyeahbody );
95+ this .observer .observe (this .$refs .ohyeahbox );
96+ } else {
97+ this .observer = ElementResizeDetectorMaker ({
98+ strategy: " scroll"
99+ });
100+ this .observer .listenTo (this .$refs .ohyeahbody , this .callback );
101+ this .observer .listenTo (this .$refs .ohyeahbox , this .callback );
102+ }
103+ },
79104 callback (msg ) {
80105 const a = this .$refs .ohyeahbox .getBoundingClientRect (); // 外壳大小
81106 const b = this .$refs .ohyeahbody .getBoundingClientRect (); // body大小
@@ -168,19 +193,26 @@ export default {
168193 e .preventDefault ();
169194 e .stopPropagation ();
170195 e .stopImmediatePropagation ();
171- const a = this .$refs .ohyeahbox .getBoundingClientRect (); // 外壳大小
172- if (this .isShowH ) {
173- this .barHScrollTop = Math .min (
174- Math .max (this .barHScrollTop - e .wheelDeltaY / 10 , 0 ),
175- a .height - this .barHTall - 4
176- );
177- }
178- if (this .isShowW ) {
179- this .barWScrollLeft = Math .min (
180- Math .max (this .barWScrollLeft - e .wheelDeltaX / 10 , 0 ),
181- a .width - this .barWTall - 4
182- );
196+ // 节流
197+ if (this .timer ) {
198+ return ;
183199 }
200+ this .timer = setTimeout (() => {
201+ const a = this .$refs .ohyeahbox .getBoundingClientRect (); // 外壳大小
202+ if (this .isShowH ) {
203+ this .barHScrollTop = Math .min (
204+ Math .max (this .barHScrollTop + e .deltaY / 5 , 0 ),
205+ a .height - this .barHTall - 4
206+ );
207+ }
208+ if (this .isShowW ) {
209+ this .barWScrollLeft = Math .min (
210+ Math .max (this .barWScrollLeft + e .deltaX / 5 , 0 ),
211+ a .width - this .barWTall - 4
212+ );
213+ }
214+ this .timer = null ;
215+ });
184216 }
185217 }
186218};
@@ -213,14 +245,11 @@ export default {
213245 z-index : 10 ;
214246 cursor : pointer ;
215247 opacity : 0 ;
216-
217- background-color : rgba (255 , 255 , 255 , 0.5 );
218248 transition : opacity 200ms , width 200ms ;
219249 & .show {
220250 opacity : 1 ;
221251 }
222252 & > div {
223- background-color : #7f7f7f ;
224253 border-radius : 999px ;
225254 transition : height 200ms , width 200ms ;
226255 }
@@ -235,13 +264,11 @@ export default {
235264 z-index : 9 ;
236265 cursor : pointer ;
237266 opacity : 0 ;
238- background-color : rgba (255 , 255 , 255 , 0.5 );
239267 transition : opacity 200ms , width 200ms ;
240268 & .show {
241269 opacity : 1 ;
242270 }
243271 & > div {
244- background-color : #7f7f7f ;
245272 border-radius : 999px ;
246273 transition : height 200ms , width 200ms ;
247274 }
0 commit comments