iview的Affix組件滾動時沒有按照預期固定


業務場景

新建任務的頁面,創建和重置按鈕,頁面沒有滾動時,直接跟在內容下面;頁面滾動時,固定於頁面下方,不隨內容進行滾動,以方便按鈕的操作。效果如下:

問題以及解決辦法

直接使用<Affix :offset-bottom="40"></Affix> 圖釘跟隨頁面滾動,並不能固定於底部
目前原因未知,估測是此組件的bug。發現手動觸發一次resize可解決此問題,代碼如下:

<template>
  <div>
    <Form>
      ···
      <FormItem>
        <Affix :offset-bottom="40">
          <Button type="primary">執行導出</Button>
          <Button>重置</Button>
        </Affix>
      </FormItem>
    </Form>
    ···
  </div>
</template>

<script>
export default {
  name: "Config",
  methods: {
    initAffix() {
      if (!this.affixInit) {
        //affix組件有bug,需要觸發一次resize事件才能正常
        this.affixInit = true;
        if (document.createEvent) {
          let event = document.createEvent("HTMLEvents");
          event.initEvent("resize", true, true);
          window.dispatchEvent(event);
        } else if (document.createEventObject) {
          window.fireEvent("onresize");
        }
      }
    }
  },
  mounted() {
    window.addEventListener("scroll", this.initAffix, true);
  },
  destroyed() {
    window.removeEventListener("scroll", this.initAffix, true);
  }
};
</script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM