" />

最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)

    這篇文章主要介紹了利用Dectorator分模塊存儲Vuex狀態(tài)的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • vue2從數(shù)據(jù)到視圖渲染之模板渲染詳解

    vue2從數(shù)據(jù)到視圖渲染之模板渲染詳解

    這篇文章主要為大家介紹了vue2從數(shù)據(jù)到視圖渲染之模板渲染詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • vue添加錨點,實現(xiàn)滾動頁面時錨點添加相應(yīng)的class操作

    vue添加錨點,實現(xiàn)滾動頁面時錨點添加相應(yīng)的class操作

    這篇文章主要介紹了vue添加錨點,實現(xiàn)滾動頁面時錨點添加相應(yīng)的class操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • vue 通過綁定事件獲取當(dāng)前行的id操作

    vue 通過綁定事件獲取當(dāng)前行的id操作

    這篇文章主要介紹了vue 通過綁定事件獲取當(dāng)前行的id操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue中正確使用jsx語法的姿勢分享

    vue中正確使用jsx語法的姿勢分享

    這篇文章主要給大家介紹了關(guān)于vue中正確使用jsx的相關(guān)資料,JSX就是Javascript和XML結(jié)合的一種格式,React發(fā)明了JSX,利用HTML語法來創(chuàng)建虛擬DOM,當(dāng)遇到<,JSX就當(dāng)HTML解析,遇到{就當(dāng)JavaScript解析,需要的朋友可以參考下
    2021-07-07
  • Vue.js中輕松解決v-for執(zhí)行出錯的三個方案

    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專屬狀態(tài)管理庫Pinia的使用與實踐分享

    Vue專屬狀態(tài)管理庫Pinia的使用與實踐分享

    在 Vue 的開發(fā)中,狀態(tài)管理是一個不可或缺的部分,尤其是在復(fù)雜的應(yīng)用中,組件之間的狀態(tài)共享和管理變得至關(guān)重要,Pinia 作為 Vue 的專屬狀態(tài)管理庫,本文將深入介紹 Pinia 的基礎(chǔ)知識、核心功能以及實際使用場景,需要的朋友可以參考下
    2024-11-11
  • vue?parseHTML函數(shù)解析器遇到結(jié)束標(biāo)簽

    vue?parseHTML函數(shù)解析器遇到結(jié)束標(biāo)簽

    這篇文章主要介紹了vue?parseHTML函數(shù)源碼解析之析器遇到結(jié)束標(biāo)簽的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • vue3中form表單層級嵌套問題的解決詳解

    vue3中form表單層級嵌套問題的解決詳解

    這篇文章主要為大家詳細(xì)介紹了vue3中form表單層級嵌套問題的相關(guān)解決方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-05-05
  • 解決vue中使用swiper插件問題及swiper在vue中的用法

    解決vue中使用swiper插件問題及swiper在vue中的用法

    Swiper是純javascript打造的滑動特效插件,面向手機(jī)、平板電腦等移動終端。這篇文章主要介紹了解決vue中使用swiper插件及swiper在vue中的用法,需要的朋友可以參考下
    2018-04-04

最新評論

巫溪县| 电白县| 随州市| 柘荣县| 本溪市| 诏安县| 凤冈县| 平凉市| 秦皇岛市| 迁安市| 阿拉尔市| 旬邑县| 彭水| 霸州市| 黄平县| 盐津县| 泸定县| 潍坊市| 山阳县| 长寿区| 林州市| 广灵县| 重庆市| 个旧市| 勐海县| 启东市| 临夏市| 建德市| 湟中县| 长海县| 鱼台县| 屏东县| 大新县| 武冈市| 昌乐县| 安仁县| 玛纳斯县| 亳州市| 博客| 光山县| 新营市|