vue實現(xiàn)錨點跳轉(zhuǎn)及滾動監(jiān)聽的方法
vue中實現(xiàn)錨點跳轉(zhuǎn)以及滾動監(jiān)聽跳轉(zhuǎn)到相應(yīng)標簽的方法,供大家參考,具體內(nèi)容如下
*注意·如果scroll-item的最后一個元素高度必須大于等于滾動區(qū)域的高度,不然最后一個元素就滾動不上去,
實際效果圖

代碼結(jié)構(gòu)
// Html結(jié)構(gòu)
<template>
? <div>
? ? <div class="list">
? ? ? <ul>
? ? ? ? <li v-for="(item,index) in title_list" :key="index">
? ? ? ? <span ref="spans" :style="{color: activeStep === index ? '#1987e1' : '#000000'}"
? ? ? ? @click="jump(index)">
? ? ? ? {{item.title}}
? ? ? ? </span>
? ? ? ? </li>
? ? ? </ul>
? ? </div>
? ? <div class="result" @scroll="onScroll" >
? ? ? <div class="scroll-item"><span>第一</span></div>
? ? ? <div class="scroll-item"><span>第二</span></div>
? ? ? <div class="scroll-item"><span>第三</span></div>
? ? ? <div class="scroll-item"><span>第四</span></div>
? ? </div>
? </div>
</template>//功能實現(xiàn)代碼
<script>
export default {
? methods:{
? ? jump(index) {
? ? ? var items = document.querySelectorAll(".scroll-item");
? ? ? for (var i = 0; i < items.length; i++) {
? ? ? ? if (index === i) {
? ? ? ? ? items[i].scrollIntoView({
? ? ? ? ? ? block: "start",//默認跳轉(zhuǎn)到頂部
? ? ? ? ? ? behavior: "smooth"http://滾動的速度
? ? ? ? ? });
? ? ? ? }
? ? ? }
? ? },
? ? onScroll(e) {
? ? ? let scrollItems = document.querySelectorAll(".scroll-item");
? ? ? for (let i = scrollItems.length - 1; i >= 0; i--) {
? ? ? ? // 判斷滾動條滾動距離是否大于當前滾動項可滾動距離
? ? ? ? let judge =
? ? ? ? ? e.target.scrollTop >=
? ? ? ? ? scrollItems[i].offsetTop - scrollItems[0].offsetTop;
? ? ? ? if (judge) {
? ? ? ? ? this.activeStep = i;
? ? ? ? ? break;
? ? ? ? }
? ? ? }
? ? }, ? ?
? },
? data() {
? ? return {
? ? ? activeStep :0,
? ? ? title_list:[
? ? ? ? {title:'第一'},
? ? ? ? {title:'第二'}, ? ? ? ?
? ? ? ? {title:'第三'},
? ? ? ? {title:'第四'},
? ? ? ? ]
? ? }
? }
}
</script>//樣式
<style>
.list {
? width: 100%;
? height: 40px;
? margin-bottom: 20px;
? background: pink;
}
ul {
? width: 100%;
? height: 40px;
? line-height: 40px;
? list-style: none;
}
li {
? float: left;
? width: 20%;
? font-size: 30px;
}
li>span {
? cursor:pointer;
}
.result {
? width: 100%;
? height: 500px;
? overflow: scroll;
}
.scroll-item {
? width: 100%;
? height: 500px;
? margin-top:20px;
? background: yellow;
}
.scroll-item>span {
? font-size: 40px;
}
.scroll-item:first-child {
? margin-top: 0;
}
.scroll-item:last-child {
? height: 500px;
}/ * 最后一個元素的最小高度要大于等于父元素的高度,如果scroll-item為高度自適應(yīng)的話,那么最后一個scroll-item就得設(shè)置min-height:100%* /
</style>以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue elementUI Plus實現(xiàn)拖拽流程圖的詳細代碼(不引入插件)
文章介紹了如何使用Vue和elementUI Plus實現(xiàn)一個簡單的拖拽流程圖功能,不引入任何插件,完全手寫,設(shè)計思路,感興趣的朋友跟隨小編一起看看吧2025-01-01
關(guān)于@click.native中?.native?的含義與使用方式
這篇文章主要介紹了關(guān)于@click.native中?.native?的含義與使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue以組件或者插件的形式實現(xiàn)throttle或者debounce
這篇文章主要介紹了vue以組件或者插件的形式實現(xiàn)throttle或者debounce,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
基于Electron+Vite快速構(gòu)建Vue3桌面應(yīng)用
這篇文章主要介紹了如何基于Electron和Vite快速構(gòu)建Vue3桌面應(yīng)用,本文主要技術(shù)棧就是Vue3、vite、Electron,文中有詳細的代碼示例,需要的朋友可以參考下2023-07-07
如何在vue3.0+中使用tinymce及實現(xiàn)多圖上傳文件上傳公式編輯功能
本文給大家分享tinymce編輯器如何在vue3.0+中使用tinymce及實現(xiàn)多圖上傳文件上傳公式編輯功能,tinymce安裝方法文中也給大家詳細介紹了,對vue tinymce實現(xiàn)上傳公式編輯相關(guān)知識感興趣的朋友跟隨小編一起學習下吧2021-05-05

