- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- Element Plus實(shí)現Affix 固釘
Affix組件用于將頁(yè)面元素固定在特定可視區域。
1.1 屬性
1.2 事件
<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接收內容
// 部分核心代碼,代碼順序有所調整 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(() => { // 根據傳入的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 } // 根據固釘元素,向上尋找滾動(dòng)容器 scrollContainer.value = getScrollContainer(root.value) // 監聽(tīng)滾動(dòng)容器的scroll事件 on(scrollContainer.value, 'scroll', onScroll) // 監聽(tīng)固釘元素的resize事件 addResizeListener(root.value, updateState) }) // 滾動(dòng)容器的scroll事件的響應函數 const onScroll = () => { // 更新固釘狀態(tài) updateState() emit('scroll', { scrollTop: state.scrollTop, fixed: state.fixed, }) } // 更新固釘狀態(tài)函數 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 對應的是讓固釘始終保持在容器內,超過(guò)范圍則隱藏 state.fixed = props.offset > rootRect.top && targetRect.bottom > 0 // 用于處理場(chǎng)景:滾動(dòng)過(guò)程中,target容器可視區域不足以顯示整個(gè)固釘,則固釘應相應偏移,只展示部分 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 } } } // 監測固釘fixed狀態(tài)變化,并對外emit change事件 watch(() => state.fixed, () => { emit('change', state.fixed) }) // 計算屬性,通過(guò)固釘的狀態(tài)自動(dòng)更新固釘的樣式 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, } }) }
到此這篇關(guān)于Element Plus實(shí)現Affix 固釘的文章就介紹到這了,更多相關(guān)Element Affix 固釘內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站