Vue組件設(shè)計(jì)-Sticky布局效果示例
Vue組件設(shè)計(jì)-Sticky布局
Sticky布局即為粘性定位,常見于一些重要的頁(yè)面區(qū)域在向上滾動(dòng)時(shí)不會(huì)被卷起來,在CSS中可以通過設(shè)置position:sticky來實(shí)現(xiàn)這一功能,但是如果出于兼容性考慮或是一些復(fù)雜的場(chǎng)景,就需要我們用傳統(tǒng)的方法來實(shí)現(xiàn),以下基于Vue封裝一個(gè)組件給大家參考。
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);
},
// 組件激活時(shí)調(diào)用
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() {
// 粘性定位區(qū)域的寬度
const width = this.$el.getBoundingClientRect().width;
this.width = width || "auto";
// 粘性定位距屏幕頂部的高度
const offsetTop = this.$el.getBoundingClientRect().top;
// 如果滾動(dòng)高度小于設(shè)定的定位高度
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;">
這是定位區(qū)域
</div>
</Sticky>
</div>
</template>
<script>
import Sticky from "@/components/Sticky";
export default {
components:{
Sticky:Sticky
}
};
</script>到此這篇關(guān)于Vue組件設(shè)計(jì)-Sticky布局的文章就介紹到這了,更多相關(guān)Vue Sticky布局內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VueCli生產(chǎn)環(huán)境打包部署跨域失敗的解決
這篇文章主要介紹了VueCli生產(chǎn)環(huán)境打包部署跨域失敗的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue3項(xiàng)目如何使用樣式穿透修改elementUI默認(rèn)樣式
這篇文章主要介紹了vue3項(xiàng)目使用樣式穿透修改elementUI默認(rèn)樣式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
vue3+vite項(xiàng)目中顯示SVG圖片的實(shí)現(xiàn)
vite-plugin-svg-icons是一個(gè)Vite插件,其作用是將SVG圖標(biāo)文件轉(zhuǎn)換為Vue組件,本文主要介紹了vue3+vite項(xiàng)目中顯示SVG圖片的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
解決vue項(xiàng)目中type=”file“ change事件只執(zhí)行一次的問題
這篇文章主要介紹了vue項(xiàng)目中解決type=”file“ change事件只執(zhí)行一次的問題,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05
VUE項(xiàng)目中引入vue-router的詳細(xì)過程
vue-router 也叫 vue 路由,根據(jù)不同的路徑,來執(zhí)行不同的組件,這篇文章主要介紹了VUE項(xiàng)目中引入vue-router,需要的朋友可以參考下2023-05-05
vue3+ts+Vuex中使用websocket協(xié)議方式
這篇文章主要介紹了vue3+ts+Vuex中使用websocket協(xié)議方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

