♻️(component): 使用 setup script 重构 scroll 组件
This commit is contained in:
		
							parent
							
								
									844f3fd0cf
								
							
						
					
					
						commit
						3b1849fb70
					
				| @ -1,44 +1,34 @@ | |||||||
| .scrollbar-box { | .layui-scroll { | ||||||
|   height: 100%; |   height: 100%; | ||||||
|   overflow: hidden !important; |   overflow: hidden !important; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| .scrollbar-y { | .layui-scroll-y { | ||||||
|   position: relative; |   position: relative; | ||||||
|   height: 100%; |   height: 100%; | ||||||
|   .scroll-wrap { | 
 | ||||||
|  |   .layui-scroll-wrap { | ||||||
|     height: 100%; |     height: 100%; | ||||||
|     overflow-y: scroll; |     overflow-y: scroll; | ||||||
|     scrollbar-width: none; /* firefox */ |     scrollbar-width: none; | ||||||
|     -ms-overflow-style: none; |     -ms-overflow-style: none; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   .scrollbar-track { |   .layui-scroll-track { | ||||||
|     position: absolute; |     position: absolute; | ||||||
|     top: 0; |     top: 0; | ||||||
|     right: 0; |     right: 0; | ||||||
|     bottom: 0; |     bottom: 0; | ||||||
|     border-radius: 8px; |     border-radius: 8px; | ||||||
|     z-index: 20; |     z-index: 20; | ||||||
|     .scrollbar-thumb { |     .layui-scroll-thumb { | ||||||
|       margin: 0 auto; |       margin: 0 auto; | ||||||
|       border-radius: 6px; |       border-radius: 6px; | ||||||
|       cursor: default; |       cursor: default; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
| .scrollbar-y ::-webkit-scrollbar { | 
 | ||||||
|  | .layui-scroll-y ::-webkit-scrollbar { | ||||||
|   display: none; |   display: none; | ||||||
| } | } | ||||||
| //移动端隐藏自定义滚动条 |  | ||||||
| .hide.scrollbar-box .scrollbar-track { |  | ||||||
|   display: none; |  | ||||||
| } |  | ||||||
| //移动端显示原生滑块 |  | ||||||
| .hide.scrollbar-box .scrollbar-y ::-webkit-scrollbar { |  | ||||||
|   display: block; |  | ||||||
| } |  | ||||||
| .hide.scrollbar-box .scrollbar-y .scroll-wrap { |  | ||||||
|   scrollbar-width: auto; |  | ||||||
|   -ms-overflow-style: scrollbar; |  | ||||||
| } |  | ||||||
|  | |||||||
| @ -1,16 +1,16 @@ | |||||||
| <template> | <template> | ||||||
|   <div |   <div | ||||||
|     class="scrollbar-box" |     class="layui-scroll" | ||||||
|     :class="{ hide: winWidth < 500 }" |     :class="{ hide: data.winWidth < 500 }" | ||||||
|     :style="{ height: height }" |     :style="{ height: height }" | ||||||
|   > |   > | ||||||
|     <div class="scrollbar-y"> |     <div class="layui-scroll-y"> | ||||||
|       <div ref="scrollRef" class="scroll-wrap" @scroll="onMosewheel"> |       <div ref="scrollRef" class="layui-scroll-wrap" @scroll="onMosewheel"> | ||||||
|         <slot></slot> |         <slot></slot> | ||||||
|       </div> |       </div> | ||||||
|       <div |       <div | ||||||
|         ref="barRef" |         ref="barRef" | ||||||
|         class="scrollbar-track" |         class="layui-scroll-track" | ||||||
|         :style="{ |         :style="{ | ||||||
|           backgroundColor: heightPre == 1 ? 'rgba(0,0,0,0)' : slotColor, |           backgroundColor: heightPre == 1 ? 'rgba(0,0,0,0)' : slotColor, | ||||||
|         }" |         }" | ||||||
| @ -21,12 +21,12 @@ | |||||||
|        --> |        --> | ||||||
|         <div |         <div | ||||||
|           :style="{ |           :style="{ | ||||||
|             height: barHeight + 'px', |             height: data.barHeight + 'px', | ||||||
|             width: scrollWidth + 'px', |             width: scrollWidth + 'px', | ||||||
|             transform: 'translateY(' + translateY + 'px)', |             transform: 'translateY(' + data.translateY + 'px)', | ||||||
|             backgroundColor: heightPre == 1 ? 'rgba(0,0,0,0)' : scrollColor, |             backgroundColor: data.heightPre == 1 ? 'rgba(0,0,0,0)' : scrollColor, | ||||||
|           }" |           }" | ||||||
|           class="scrollbar-thumb" |           class="layui-scroll-thumb" | ||||||
|           @mousedown.stop.prevent="moveStart" |           @mousedown.stop.prevent="moveStart" | ||||||
|         ></div> |         ></div> | ||||||
|       </div> |       </div> | ||||||
| @ -34,7 +34,13 @@ | |||||||
|   </div> |   </div> | ||||||
| </template> | </template> | ||||||
| 
 | 
 | ||||||
| <script> | <script lang="ts"> | ||||||
|  | export default { | ||||||
|  |   name: "LayScroll" | ||||||
|  | } | ||||||
|  | </script> | ||||||
|  | 
 | ||||||
|  | <script lang="ts" setup> | ||||||
| import "./index.less"; | import "./index.less"; | ||||||
| import { | import { | ||||||
|   defineComponent, |   defineComponent, | ||||||
| @ -43,34 +49,29 @@ import { | |||||||
|   nextTick, |   nextTick, | ||||||
|   reactive, |   reactive, | ||||||
|   onUnmounted, |   onUnmounted, | ||||||
|  |   ref, | ||||||
| } from "vue"; | } from "vue"; | ||||||
| export default defineComponent({ | 
 | ||||||
|   name: "LayScroll", | export interface LayScrollProps { | ||||||
|   props: { |   height: string; | ||||||
|     height: { |   slotColor: string; | ||||||
|       type: String, |   scrollColor: string; | ||||||
|       default: "100%", |   scrollWidth: number; | ||||||
|     }, | } | ||||||
|     slotColor: { | 
 | ||||||
|       //滑块槽颜色 :slotColor="'red'" | const props = withDefaults(defineProps<LayScrollProps>(), { | ||||||
|       type: String, |   height: "100%", | ||||||
|       default: "rgba(0,0,0,0)", |   slotColor: "rgba(0,0,0,0)", | ||||||
|     }, |   scrollColor: "#eeeeee", | ||||||
|     scrollColor: { |   scrollWidth: 6, | ||||||
|       //滑块颜色  :scrollColor="'red'" | }); | ||||||
|       type: String, | 
 | ||||||
|       default: "#eeeeee", | const emit = defineEmits(["arrive"]); | ||||||
|     }, | 
 | ||||||
|     scrollWidth: { | const scrollRef = ref<HTMLElement | null>(null); | ||||||
|       //滑块宽度 :scrollWidth="7" | const barRef = ref<HTMLElement | null>(null); | ||||||
|       type: Number, | 
 | ||||||
|       default: 6, |  | ||||||
|     }, |  | ||||||
|   }, |  | ||||||
|   setup(props, ctx) { |  | ||||||
| const data = reactive({ | const data = reactive({ | ||||||
|       scrollRef: null, // 内容盒子 |  | ||||||
|       barRef: null, // 滚动条轨道 |  | ||||||
|   translateY: 0, // 滚动块平移的距离 |   translateY: 0, // 滚动块平移的距离 | ||||||
|   heightPre: 0, // 可视高度和内容高度比 |   heightPre: 0, // 可视高度和内容高度比 | ||||||
|   barHeight: 0, // 滑块高度 |   barHeight: 0, // 滑块高度 | ||||||
| @ -113,11 +114,13 @@ export default defineComponent({ | |||||||
| 
 | 
 | ||||||
| //监听内容元素尺寸变化 | //监听内容元素尺寸变化 | ||||||
| const monitorScrollBar = function () { | const monitorScrollBar = function () { | ||||||
|       var monitorUl = data.scrollRef.children[0]; |   var monitorUl = scrollRef.value.children[0]; | ||||||
|   // var monitorDiv= document;    // 监听document |   // var monitorDiv= document;    // 监听document | ||||||
|   let MutationObserver = |   let MutationObserver = | ||||||
|     window.MutationObserver || |     window.MutationObserver || | ||||||
|  |     // @ts-ignore | ||||||
|     window.WebKitMutationObserver || |     window.WebKitMutationObserver || | ||||||
|  |     // @ts-ignore | ||||||
|     window.MozMutationObserver; |     window.MozMutationObserver; | ||||||
|   let observer = new MutationObserver(function (mutations) { |   let observer = new MutationObserver(function (mutations) { | ||||||
|     // console.log("内容元素变化更新滚动条"); |     // console.log("内容元素变化更新滚动条"); | ||||||
| @ -149,8 +152,8 @@ export default defineComponent({ | |||||||
| }; | }; | ||||||
| // 计算滚动条高度 | // 计算滚动条高度 | ||||||
| const initScrollListner = function () { | const initScrollListner = function () { | ||||||
|       let scroll = data.scrollRef; |   let scroll = scrollRef.value; | ||||||
|       let bar = data.barRef; |   let bar = barRef.value; | ||||||
|   // scroll有时候拿不到元素,要判断一下 |   // scroll有时候拿不到元素,要判断一下 | ||||||
|   if (scroll) { |   if (scroll) { | ||||||
|     wrapContentHeight = scroll.scrollHeight; |     wrapContentHeight = scroll.scrollHeight; | ||||||
| @ -183,7 +186,7 @@ export default defineComponent({ | |||||||
| }; | }; | ||||||
| // 到达顶部或者底部通知父级元素 | // 到达顶部或者底部通知父级元素 | ||||||
| const arrive = function name(tb) { | const arrive = function name(tb) { | ||||||
|       ctx.emit("arrive", tb); |   emit("arrive", tb); | ||||||
| }; | }; | ||||||
| // 鼠标点击滑块时 | // 鼠标点击滑块时 | ||||||
| const moveStart = function (e) { | const moveStart = function (e) { | ||||||
| @ -212,7 +215,7 @@ export default defineComponent({ | |||||||
|         data.translateY = e.clientY - moveClientY; |         data.translateY = e.clientY - moveClientY; | ||||||
|       } |       } | ||||||
|       // 计算出内容盒子滚出顶部的距离 |       // 计算出内容盒子滚出顶部的距离 | ||||||
|           data.scrollRef.scrollTop = data.translateY / data.heightPre; |       scrollRef.value.scrollTop = data.translateY / data.heightPre; | ||||||
|     } |     } | ||||||
|   }; |   }; | ||||||
| }; | }; | ||||||
| @ -225,11 +228,5 @@ export default defineComponent({ | |||||||
|   }; |   }; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|     return { | let dataRef = toRefs(data); | ||||||
|       ...toRefs(data), |  | ||||||
|       onMosewheel, |  | ||||||
|       moveStart, |  | ||||||
|     }; |  | ||||||
|   }, |  | ||||||
| }); |  | ||||||
| </script> | </script> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user