国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Element Plus實(shí)現Affix 固釘

發(fā)布時(shí)間:2021-08-17 12:16 來(lái)源: 閱讀:0 作者:LittleWorker本尊 欄目: JavaScript 歡迎投稿:712375056

目錄

一、組件介紹

Affix組件用于將頁(yè)面元素固定在特定可視區域。

1.1 屬性

  • position:指定固釘的位置,可設置為top或bottom,默認為top
  • offset: 設置偏移距離,默認為0
  • target:指定容器(CSS 選擇器),讓固釘始終保持在容器內,超過(guò)范圍則隱藏,默認的容器是document.documentElement。
  • z-index: 固釘的層級,默認100

1.2 事件

  • scroll: 容器滾動(dòng)時(shí)觸發(fā)事件,參數是:固釘的scrollTop值和狀態(tài)(是否fixed)
  • change: 固釘狀態(tài)改變時(shí)觸發(fā),參數是固釘當前是否處于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接收內容

2.2 script

// 部分核心代碼,代碼順序有所調整
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,
      }
    })
}

2.3 實(shí)現總結:

  • 通過(guò)監聽(tīng)滾動(dòng)容器的scroll事件(及固釘自身的resize事件);
  • 事件響應函數中動(dòng)態(tài)獲取固釘及target容器的DOM屬性并以此計算固釘的狀態(tài);
  • 利用計算屬性自動(dòng)更新固釘的樣式;

到此這篇關(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í)歡迎投稿傳遞力量。

欧美变态另类牲交ZOZO| 久久精品九九热无码免贵| 中文字幕AV一区乱码| 国产免费不卡午夜福利在线| 亚洲色偷偷综合亚洲AV78| 久久亚洲欧美国产精品|