vue實(shí)現(xiàn)固定位置顯示功能
在vue項(xiàng)目中實(shí)現(xiàn)吸頂效果.
比如說(shuō),我們要實(shí)現(xiàn)的功能是導(dǎo)航欄在頁(yè)面下滑到一定位置之后,便固定不定。
首先:要在mounted生命周期內(nèi)監(jiān)聽'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)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
vue項(xiàng)目實(shí)現(xiàn)圖形驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目實(shí)現(xiàn)圖形驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04SpringBoot+Vue開發(fā)之Login校驗(yàn)規(guī)則、實(shí)現(xiàn)登錄和重置事件
這篇文章主要介紹了SpringBoot+Vue開發(fā)之Login校驗(yàn)規(guī)則、實(shí)現(xiàn)登錄和重置事件,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Vue.JS項(xiàng)目中5個(gè)經(jīng)典Vuex插件
在本文中,將向你展示5個(gè)特性,你可以通過(guò) Vuex 插件輕松地添加到下一個(gè)項(xiàng)目中。一起來(lái)學(xué)習(xí)下。2017-11-11Vue Cli3 創(chuàng)建項(xiàng)目的方法步驟
Vue CLI是一個(gè)用于快速Vue.js開發(fā)的完整系統(tǒng)。這篇文章主要介紹了Vue Cli3 創(chuàng)建項(xiàng)目的方法步驟,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10vue報(bào)錯(cuò)Error:Cannot?find?module?'fs/promises'的解決方
最近的項(xiàng)目需要用到vue/cli,但是用cnpm安裝vue/cli的時(shí)候報(bào)錯(cuò)了,下面這篇文章主要給大家介紹了關(guān)于vue報(bào)錯(cuò)Error:Cannot?find?module?'fs/promises'的解決方式,需要的朋友可以參考下2022-11-11