vue實(shí)現(xiàn)固定位置顯示功能
在vue項(xiàng)目中實(shí)現(xiàn)吸頂效果.
比如說(shuō),我們要實(shí)現(xiàn)的功能是導(dǎo)航欄在頁(yè)面下滑到一定位置之后,便固定不定。
首先:要在mounted生命周期內(nèi)監(jiān)聽(tīng)'scroll'事件,事件觸發(fā)后,執(zhí)行一個(gè)處理滾動(dòng)的函數(shù)。
window.addEventListener('scroll', this.handleScroll)
methods:{
handleScroll () {
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
var offsetTop = document.querySelector('#searchBar').offsetTop
if (scrollTop > offsetTop) { //判斷是否到達(dá)了頂部
this.searchBarFixed = true
} else {
this.searchBarFixed = false
}
}
}
完整源代碼如下:
<template>
<div>
<div class="nav"></div>
<div class="searchBar" id="searchBar">
<ul :class="searchBarFixed == true ? 'isFixed' :''"> //用v-bind綁定樣式
<li>區(qū)域<i class="iconfont icon-jiantouxia"></i></li>
<li>價(jià)格<i class="iconfont icon-jiantouxia"></i></li>
<li>房型<i class="iconfont icon-jiantouxia"></i></li>
<li>更多<i class="iconfont icon-jiantouxia"></i></li>
</ul>
</div>
<div class="content">
</div>
</div>
</template>
<script>
export default {
components:{
},
mounted () {
window.addEventListener('scroll', this.handleScroll)
},
data(){
return {
searchBarFixed:false
}
},
methods:{
handleScroll () {
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
var offsetTop = document.querySelector('#searchBar').offsetTop
if (scrollTop > offsetTop) {
this.searchBarFixed = true
} else {
this.searchBarFixed = false
}
},
}
}
</script>
<style lang="less" scope>
.nav{
height: 250px;
}
.content{
height: 1900px;
}
.searchBar{
.isFixed{
position:fixed;
background-color:#Fff;
top:0;
z-index:999;
}
ul {
width:100%;
height: 40px;
line-height: 40px;
display: flex;
li {
list-style: none;
font-size: 0.8rem;
text-align: center;
flex: 1;
i {
font-size: 0.9rem;
padding-left: 5px;
color: #ccc;
}
}
border-bottom: 1px solid #ddd;
}
}
</style>
總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)固定位置顯示功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
vue使用element-ui實(shí)現(xiàn)表單驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了vue使用element-ui實(shí)現(xiàn)表單驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
VUE搭建分布式醫(yī)療掛號(hào)系統(tǒng)的前臺(tái)預(yù)約掛號(hào)步驟詳情
這篇文章主要介紹了VUE搭建分布式醫(yī)療掛號(hào)系統(tǒng)的前臺(tái)預(yù)約掛號(hào)步驟詳情,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
vue-cli創(chuàng)建的項(xiàng)目,配置多頁(yè)面的實(shí)現(xiàn)方法
下面小編就為大家分享一篇vue-cli創(chuàng)建的項(xiàng)目,配置多頁(yè)面的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue系列之動(dòng)態(tài)路由詳解【原創(chuàng)】
下面小編就為大家?guī)?lái)一篇vue系列之動(dòng)態(tài)路由詳解【原創(chuàng)】。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就想給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
在VUE中使用lodash的debounce和throttle操作
這篇文章主要介紹了在VUE中使用lodash的debounce和throttle操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Vue使用Less與Scss實(shí)現(xiàn)主題切換方法詳細(xì)講解
目前,在眾多的后臺(tái)管理系統(tǒng)中,換膚功能已是一個(gè)很常見(jiàn)的功能。用戶(hù)可以根據(jù)自己的喜好,設(shè)置頁(yè)面的主題,從而實(shí)現(xiàn)個(gè)性化定制。目前,我所了解到的換膚方式,也是我目前所掌握的兩種換膚方式,想同大家一起分享2023-02-02

