Element Plus實(shí)現(xiàn)Affix 固釘
一、組件介紹
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ù)膶蛹?jí),默認(rèn)100
1.2 事件
- scroll: 容器滾動(dòng)時(shí)觸發(fā)事件,參數(shù)是:固釘?shù)膕crollTop值和狀態(tài)(是否fixed)
- change: 固釘狀態(tài)改變時(shí)觸發(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)
// 滾動(dòng)容器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ù)固釘元素,向上尋找滾動(dòng)容器
scrollContainer.value = getScrollContainer(root.value)
// 監(jiān)聽滾動(dòng)容器的scroll事件
on(scrollContainer.value, 'scroll', onScroll)
// 監(jiān)聽固釘元素的resize事件
addResizeListener(root.value, updateState)
})
// 滾動(dòng)容器的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 對(duì)應(yīng)的是讓固釘始終保持在容器內(nèi),超過范圍則隱藏
state.fixed = props.offset > rootRect.top && targetRect.bottom > 0
// 用于處理場景:滾動(dòng)過程中,target容器可視區(qū)域不足以顯示整個(gè)固釘,則固釘應(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)變化,并對(duì)外emit change事件
watch(() => state.fixed, () => {
emit('change', state.fixed)
})
// 計(jì)算屬性,通過固釘?shù)臓顟B(tài)自動(dòng)更新固釘?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 實(shí)現(xiàn)總結(jié):
- 通過監(jiān)聽滾動(dòng)容器的scroll事件(及固釘自身的resize事件);
- 事件響應(yīng)函數(shù)中動(dòng)態(tài)獲取固釘及target容器的DOM屬性并以此計(jì)算固釘?shù)臓顟B(tài);
- 利用計(jì)算屬性自動(dòng)更新固釘?shù)臉邮剑?/li>
到此這篇關(guān)于Element Plus實(shí)現(xiàn)Affix 固釘?shù)奈恼戮徒榻B到這了,更多相關(guān)Element Affix 固釘內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解vue-meta如何讓你更優(yōu)雅的管理頭部標(biāo)簽
這篇文章主要介紹了詳解vue-meta如何讓你更優(yōu)雅的管理頭部標(biāo)簽,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
在vue項(xiàng)目中集成graphql(vue-ApolloClient)
這篇文章主要介紹了在vue項(xiàng)目中集成graphql(vue-ApolloClient),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
vue中循環(huán)請(qǐng)求接口參數(shù)問題及解決
這篇文章主要介紹了vue中循環(huán)請(qǐng)求接口參數(shù)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue使用echarts實(shí)現(xiàn)立體柱形圖
這篇文章主要為大家詳細(xì)介紹了vue使用echarts實(shí)現(xiàn)立體柱形圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

