vue點擊導(dǎo)航頁面實現(xiàn)自動滾動到特定位置
更新時間:2024年03月01日 09:07:49 作者:前端深造中
這篇文章主要介紹了vue點擊導(dǎo)航頁面實現(xiàn)自動滾動到特定位置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
效果預(yù)覽

npm i element-ui -S
下載安裝element組件庫,導(dǎo)航我們使用element組件庫中的樣式
type="primary"剛好作為我們導(dǎo)航激活后的樣式
省去了我們寫樣式的時間
到 main.js文件中全局引入element組件
import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI);
代碼實現(xiàn)
<!-- 為每一個按鈕 添加點擊事件 用來作為按鈕的切換 我們將所有的按鈕導(dǎo)航裝到
一個div中,給這個div添加點擊事件就可以了(不知道為什么的小伙伴可以去看一下事件冒泡)
由于進(jìn)入頁面要有默認(rèn)激活項,我們將data屬性中的active賦值為1,就可以了,每次點擊,只需要通過
訪問dataset中的屬性動態(tài)的賦值給active就可以實現(xiàn)切換啦
-->
<div @click="dbclick">
<el-button :type="active=='1'?'primary':''" data-index="1">新建</el-button>
<el-button :type="active=='2'?'primary':''" data-index="2">銷毀</el-button>
<el-button :type="active=='3'?'primary':''" data-index="3">轉(zhuǎn)辦</el-button>
<el-button :type="active=='4'?'primary':''" data-index="4">發(fā)送</el-button>
</div>
<script>
export default{
data() {
return {
active: "1"
}
},
}
</script>
<!-- 接下來我們定義內(nèi)容展示部分,用一個大的盒子將其包裹,然后給與每一個子div不同的id,到后期我們會
使用到,本人比較懶散,內(nèi)容呢直接在這里循環(huán)了50次,以此來撐開盒子高度 -->
<div class="height">
<div id="newview" ref="newview">
<span v-for="(item,index) in 50" :key="index" style="background:whitesmoke;">這是新建內(nèi)容</span>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="distory">
<span v-for="(item,index) in 50" :key="index" style="background:yellowgreen;">這是銷毀內(nèi)容</span>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="turn">
<span v-for="(item,index) in 50" :key="index" style="background:pink;">這是轉(zhuǎn)辦內(nèi)容</span>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="contantsend">
<span v-for="(item,index) in 50" :key="index" style="background:yellow;">這是發(fā)送內(nèi)容</span>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>
<script>
export default {
methods: {
dbclick(e) {
this.active=e.target.dataset.index
// scrollIntoView 方法為滾動到指定元素位置 所以為了得到滾動元素的位置
// 為每一個元素添加了id 當(dāng)然初了id還可以使用ref為元素綁定值,通過this.$refs獲取
// 看您的喜好,想用那種都行
if(e.target.dataset.index==1){
// document.getElementById("newview").scrollIntoView({
// block: 'start',
// behavior: 'smooth'
//})
this.$refs.newview.scrollIntoView({
block: 'start',
behavior: 'smooth'// 代表平滑滾動
})
}
if(e.target.dataset.index==2){
document.getElementById("distory").scrollIntoView({
block: 'start',
behavior: 'smooth'
})
}
if(e.target.dataset.index==3){
document.getElementById("turn").scrollIntoView({
block: 'start',
behavior: 'smooth'
})
}
if(e.target.dataset.index==4){
document.getElementById("contantsend").scrollIntoView({
block: 'start',
behavior: 'smooth'
})
}
console.log("觸發(fā)事件",e.target.dataset.index);
}
}
}
</script>
整體代碼
<template>
<div>
<div @click="dbclick">
<el-button :type="active=='1'?'primary':''" data-index="1">新建</el-button>
<el-button :type="active=='2'?'primary':''" data-index="2">銷毀</el-button>
<el-button :type="active=='3'?'primary':''" data-index="3">轉(zhuǎn)辦</el-button>
<el-button :type="active=='4'?'primary':''" data-index="4">發(fā)送</el-button>
</div>
<div class="height">
<div id="newview" ref="newview">
<span v-for="(item,index) in 50" :key="index" style="background:whitesmoke;">這是新建內(nèi)容</span>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="distory">
<span v-for="(item,index) in 50" :key="index" style="background:yellowgreen;">這是銷毀內(nèi)容</span>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="turn">
<span v-for="(item,index) in 50" :key="index" style="background:pink;">這是轉(zhuǎn)辦內(nèi)容</span>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="contantsend">
<span v-for="(item,index) in 50" :key="index" style="background:yellow;">這是發(fā)送內(nèi)容</span>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>
</div>
</template><script>
export default {
data() {
return {
active: "1"
}
},
methods: {
dbclick(e) {
this.active=e.target.dataset.index
if(e.target.dataset.index==1){
this.$refs.newview.scrollIntoView({
block: 'start',
behavior: 'smooth'
})
}
if(e.target.dataset.index==2){
document.getElementById("distory").scrollIntoView({
block: 'start',
behavior: 'smooth'
})
}
if(e.target.dataset.index==3){
document.getElementById("turn").scrollIntoView({
block: 'start',
behavior: 'smooth'
})
}
if(e.target.dataset.index==4){
document.getElementById("contantsend").scrollIntoView({
block: 'start',
behavior: 'smooth'
})
}
console.log("觸發(fā)事件",e.target.dataset.index);
}
}
}
</script><style lang="scss" scoped>
.height{
width: 30%;
height:500px;
margin: auto;
background-color: bisque;
word-break: break-all;
overflow-y: scroll;
// 垂直方向滾動條
}
</style>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用Dectorator分模塊存儲Vuex狀態(tài)的實現(xiàn)
這篇文章主要介紹了利用Dectorator分模塊存儲Vuex狀態(tài)的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
vue添加錨點,實現(xiàn)滾動頁面時錨點添加相應(yīng)的class操作
這篇文章主要介紹了vue添加錨點,實現(xiàn)滾動頁面時錨點添加相應(yīng)的class操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Vue.js中輕松解決v-for執(zhí)行出錯的三個方案
v-for標(biāo)簽可以用來遍歷數(shù)組,將數(shù)組的每一個值綁定到相應(yīng)的視圖元素中去,下面這篇文章主要給大家介紹了關(guān)于在Vue.js中輕松解決v-for執(zhí)行出錯的三個方案,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-06-06
vue?parseHTML函數(shù)解析器遇到結(jié)束標(biāo)簽
這篇文章主要介紹了vue?parseHTML函數(shù)源碼解析之析器遇到結(jié)束標(biāo)簽的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
解決vue中使用swiper插件問題及swiper在vue中的用法
Swiper是純javascript打造的滑動特效插件,面向手機(jī)、平板電腦等移動終端。這篇文章主要介紹了解決vue中使用swiper插件及swiper在vue中的用法,需要的朋友可以參考下2018-04-04

