Vue組件設計-Sticky佈局效果示例

Vue組件設計-Sticky佈局

Sticky佈局即為粘性定位,常見於一些重要的頁面區域在向上滾動時不會被卷起來,在CSS中可以通過設置position:sticky來實現這一功能,但是如果出於兼容性考慮或是一些復雜的場景,就需要我們用傳統的方法來實現,以下基於Vue封裝一個組件給大傢參考。

1. 效果示例

 2. 組件封裝

<template>
    <div :style="{height:height + 'px', zIndex: zIndex }">
        <div
            :class="className"
            :style="{
                width: width,
                zIndex: zIndex,
                position: position,
                height: height + 'px',
                top: isSticky ? stickyTop + 'px' : '',
            }">
            <slot>
            </slot>
        </div>
    </div>
</template>
<script>
export default {
    name: "Sticky",
    props: {
        stickyTop: {
            type: Number,
            default: 0,
        },
        zIndex: {
            type: Number,
            default: 1,
        },
        className: {
            type: String,
            default: "",
        },
    },
    data() {
        return {
            position: "",
            active: false,
            isSticky: false,
            width: undefined,
            height: undefined,
        };
    },
    mounted() {
        this.height = this.$el.getBoundingClientRect().height;
        window.addEventListener("scroll", this.handleScroll);
        window.addEventListener("resize", this.handleResize);
    },
    // 組件激活時調用
    activated() {
        this.handleScroll();
    },
    destroyed() {
        window.removeEventListener("scroll", this.handleScroll);
        window.removeEventListener("resize", this.handleResize);
    },
    methods: {
        sticky() {
            if (this.active) {
                return;
            }
            this.active = true;
            this.isSticky = true;
            this.position = "fixed";
            this.width = this.width + "px";
        },
        handleReset() {
            if (!this.active) {
                return;
            }
            this.reset();
        },
        reset() {
            this.position = "";
            this.width = "auto";
            this.active = false;
            this.isSticky = false;
        },
        handleScroll() {
            // 粘性定位區域的寬度
            const width = this.$el.getBoundingClientRect().width;
            this.width = width || "auto";
            // 粘性定位距屏幕頂部的高度
            const offsetTop = this.$el.getBoundingClientRect().top;
            // 如果滾動高度小於設定的定位高度
            if (offsetTop < this.stickyTop) {
                this.sticky();
                return;
            };
            this.handleReset();
        },
        handleResize() {
            if (this.isSticky) {
                this.width = this.$el.getBoundingClientRect().width + "px";
            }
        },
    },
};
</script>

3. 組件使用

<template>
    <div style="height:2000px;">
        <div style="height:200px;background-color:green"></div>
        <Sticky>
            <div style="height:300px;background-color:red;line-height:300px;text-align:center;font-size:30px;">
                這是定位區域
            </div>
        </Sticky>
    </div>
</template>
<script>
import Sticky from "@/components/Sticky";
export default {
    components:{
        Sticky:Sticky
    }
};
</script>

到此這篇關於Vue組件設計-Sticky佈局的文章就介紹到這瞭,更多相關Vue Sticky佈局內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: