Element Plus實(shí)現(xiàn)Affix 固釘
一、組件介紹
Affix組件用于將頁(yè)面元素固定在特定可視區(qū)域。
1.1 屬性
- position:指定固釘?shù)奈恢?,可設(shè)置為top或bottom,默認(rèn)為top
- offset: 設(shè)置偏移距離,默認(rèn)為0
- target:指定容器(CSS 選擇器),讓固釘始終保持在容器內(nèi),超過(guò)范圍則隱藏,默認(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部分很簡(jiǎn)單,通過(guò)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)聽(tīng)滾動(dòng)容器的scroll事件
on(scrollContainer.value, 'scroll', onScroll)
// 監(jiān)聽(tīng)固釘元素的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),超過(guò)范圍則隱藏
state.fixed = props.offset > rootRect.top && targetRect.bottom > 0
// 用于處理場(chǎng)景:滾動(dòng)過(guò)程中,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)測(cè)固釘fixed狀態(tài)變化,并對(duì)外emit change事件
watch(() => state.fixed, () => {
emit('change', state.fixed)
})
// 計(jì)算屬性,通過(guò)固釘?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é):
- 通過(guò)監(jiān)聽(tīng)滾動(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)文章
vue2 中使用 render 函數(shù)編寫組件的方式
vue提供了聲明式編寫UI的方式,即vue提供了對(duì)DOM進(jìn)行描述的方式,有兩種描述DOM的方式即模板和render 函數(shù),本文通過(guò)示例代碼介紹vue2 中使用 render 函數(shù)編寫組件的方式,感興趣的朋友跟隨小編一起看看吧2024-06-06
vue+vant移動(dòng)端顯示table表格加橫向滾動(dòng)條效果
vant移動(dòng)端顯示table效果,增加復(fù)選框,可以進(jìn)行多選和全選,加橫向滾動(dòng)條,可以看全部?jī)?nèi)容,下面通過(guò)本文給大家分享vue+vant移動(dòng)端顯示table表格加橫向滾動(dòng)條效果,感興趣的朋友跟隨小編一起看看吧2024-06-06
VUE項(xiàng)目初建和常見(jiàn)問(wèn)題總結(jié)
在本篇文章里小編給大家整理的是關(guān)于VUE 項(xiàng)目初建和常見(jiàn)問(wèn)題以及相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們學(xué)習(xí)下。2019-09-09
Vue渲染器如何對(duì)節(jié)點(diǎn)進(jìn)行掛載和更新
這篇文章主要介紹了Vue 的渲染器是如何對(duì)節(jié)點(diǎn)進(jìn)行掛載和更新的,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-05-05
詳解vue中v-on事件監(jiān)聽(tīng)指令的基本用法
這篇文章主要介紹了詳解vue中v-on事件監(jiān)聽(tīng)指令的基本用法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
Vue實(shí)現(xiàn)下拉滾動(dòng)加載數(shù)據(jù)的示例
這篇文章主要介紹了Vue實(shí)現(xiàn)下拉滾動(dòng)加載數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-04-04

