Element Plus實現(xiàn)Affix 固釘
更新時間:2021年07月19日 10:16:19 作者:LittleWorker本尊
本文主要介紹了Element Plus實現(xiàn)Affix 固釘,Affix組件用于將頁面元素固定在特定可視區(qū)域,文中通過示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下
一、組件介紹
Affix組件用于將頁面元素固定在特定可視區(qū)域。
1.1 屬性
- position:指定固釘?shù)奈恢?,可設(shè)置為top或bottom,默認(rèn)為top
- offset: 設(shè)置偏移距離,默認(rèn)為0
- target:指定容器(CSS 選擇器),讓固釘始終保持在容器內(nèi),超過范圍則隱藏,默認(rèn)的容器是document.documentElement。
- z-index: 固釘?shù)膶蛹?,默認(rèn)100
1.2 事件
- scroll: 容器滾動時觸發(fā)事件,參數(shù)是:固釘?shù)膕crollTop值和狀態(tài)(是否fixed)
- change: 固釘狀態(tài)改變時觸發(fā),參數(shù)是固釘當(dāng)前是否處于fixed狀態(tài)
二、源碼分析
2.1 template
<template> <div ref="root" class="el-affix" :style="rootStyle"> <div :class="{'el-affix--fixed': state.fixed}" :style="affixStyle"> <slot></slot> </div> </div> </template>
template部分很簡單,通過slot接收內(nèi)容
2.2 script
// 部分核心代碼,代碼順序有所調(diào)整 setup(props, { emit }) { // target容器 ref const target = ref(null) // 固釘ref,與template中的ref屬性配合,得到HTML元素 const root = ref(null) // 滾動容器ref const scrollContainer = ref(null) // 固釘狀態(tài) const state = reactive({ fixed: false, height: 0, // height of root width: 0, // width of root scrollTop: 0, // scrollTop of documentElement clientHeight: 0, // clientHeight of documentElement transform: 0, }) onMounted(() => { // 根據(jù)傳入的target確定 target容器 if (props.target) { target.value = document.querySelector(props.target) if (!target.value) { throw new Error(`target is not existed: ${props.target}`) } } else { target.value = document.documentElement } // 根據(jù)固釘元素,向上尋找滾動容器 scrollContainer.value = getScrollContainer(root.value) // 監(jiān)聽滾動容器的scroll事件 on(scrollContainer.value, 'scroll', onScroll) // 監(jiān)聽固釘元素的resize事件 addResizeListener(root.value, updateState) }) // 滾動容器的scroll事件的響應(yīng)函數(shù) const onScroll = () => { // 更新固釘狀態(tài) updateState() emit('scroll', { scrollTop: state.scrollTop, fixed: state.fixed, }) } // 更新固釘狀態(tài)函數(shù) const updateState = () => { const rootRect = root.value.getBoundingClientRect() const targetRect = target.value.getBoundingClientRect() state.height = rootRect.height state.width = rootRect.width state.scrollTop = scrollContainer.value === window ? document.documentElement.scrollTop : scrollContainer.value.scrollTop state.clientHeight = document.documentElement.clientHeight if (props.position === 'top') { if (props.target) { const difference = targetRect.bottom - props.offset - state.height // targetRect.bottom > 0 對應(yīng)的是讓固釘始終保持在容器內(nèi),超過范圍則隱藏 state.fixed = props.offset > rootRect.top && targetRect.bottom > 0 // 用于處理場景:滾動過程中,target容器可視區(qū)域不足以顯示整個固釘,則固釘應(yīng)相應(yīng)偏移,只展示部分 state.transform = difference < 0 ? difference : 0 } else { state.fixed = props.offset > rootRect.top } } else { if (props.target) { const difference = state.clientHeight - targetRect.top - props.offset - state.height state.fixed = state.clientHeight - props.offset < rootRect.bottom && state.clientHeight > targetRect.top state.transform = difference < 0 ? -difference : 0 } else { state.fixed = state.clientHeight - props.offset < rootRect.bottom } } } // 監(jiān)測固釘fixed狀態(tài)變化,并對外emit change事件 watch(() => state.fixed, () => { emit('change', state.fixed) }) // 計算屬性,通過固釘?shù)臓顟B(tài)自動更新固釘?shù)臉邮? const affixStyle = computed(() => { if (!state.fixed) { return } const offset = props.offset ? `${props.offset}px` : 0 const transform = state.transform ? `translateY(${state.transform}px)` : '' return { height: `${state.height}px`, width: `${state.width}px`, top: props.position === 'top' ? offset : '', bottom: props.position === 'bottom' ? offset : '', transform: transform, zIndex: props.zIndex, } }) }
2.3 實現(xiàn)總結(jié):
- 通過監(jiān)聽滾動容器的scroll事件(及固釘自身的resize事件);
- 事件響應(yīng)函數(shù)中動態(tài)獲取固釘及target容器的DOM屬性并以此計算固釘?shù)臓顟B(tài);
- 利用計算屬性自動更新固釘?shù)臉邮剑?/li>
到此這篇關(guān)于Element Plus實現(xiàn)Affix 固釘?shù)奈恼戮徒榻B到這了,更多相關(guān)Element Affix 固釘內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2 中使用 render 函數(shù)編寫組件的方式
vue提供了聲明式編寫UI的方式,即vue提供了對DOM進(jìn)行描述的方式,有兩種描述DOM的方式即模板和render 函數(shù),本文通過示例代碼介紹vue2 中使用 render 函數(shù)編寫組件的方式,感興趣的朋友跟隨小編一起看看吧2024-06-06Vue實現(xiàn)下拉滾動加載數(shù)據(jù)的示例
這篇文章主要介紹了Vue實現(xiàn)下拉滾動加載數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-04-04