vue添加錨點(diǎn),實(shí)現(xiàn)滾動(dòng)頁(yè)面時(shí)錨點(diǎn)添加相應(yīng)的class操作
第一步,給vue頁(yè)面添加錨點(diǎn)
.orange{
color: #f97910;
}
<template>
<div class="productDetail" ref="content">
<div class="tabbar">
<div @click.prevent="tabclick(index)" v-for="(item,index) in productTile" :key="index" :class="{orange:index==current}">{{item}}</div>
</div>
<div id="0">...</div>
<div id="1">...</div>
<div id="2">...</div>
</div>
<template>
tabclick(index){
this.current=index;
let anchorElement = document.getElementById(index);
if(anchorElement) { anchorElement.scrollIntoView(); }
},
第二步:給class為productDetail的<div>部分加height:100%;overflow-y: scroll;
.productDetail {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
overflow-y: scroll;
}
第三步,添加監(jiān)聽事件
document.getElementsByClassName('productDetail')[0]; vue中同理于:this.$refs.content
methods:{
handleScroll(el) {
this.scrollTop = this.$refs.content.scrollTop;
if (this.scrollTop >= 460) {
this.current = 2
} else if (this.scrollTop < 460 && this.scrollTop >= 360) {
this.current = 1
} else {
this.current = 0
}
},
},
mounted() {
//scoll滾動(dòng)事件監(jiān)聽
var pro_detail_page = document.getElementsByClassName('productDetail')[0];
pro_detail_page.addEventListener('scroll', this.handleScroll);
},
注:給最外層div添加height:100%后,mint-ui的輪播圖就會(huì)展示不出來。我們可以修改mint-ui的默認(rèn)overflow屬性,改為:overflow:visible
補(bǔ)充知識(shí):使用Vuepress自動(dòng)生成markdown的目錄時(shí),一旦標(biāo)題有數(shù)字時(shí)便無法跳轉(zhuǎn)的問題解決
問題描述
最近在用vuepress寫網(wǎng)頁(yè)文檔的時(shí)候發(fā)現(xiàn)了一個(gè)問題,就是我用markdown書寫的標(biāo)題中如果有類似 1.2 XXX 的標(biāo)題時(shí),當(dāng)使用官方文檔給出的:
[[toc]]
自動(dòng)生成目錄時(shí),最終生成的網(wǎng)頁(yè),含有數(shù)字的標(biāo)題是無法跳轉(zhuǎn)到相應(yīng)位置的。
問題分析
查看官方開發(fā)文檔后發(fā)現(xiàn),這跟vuepress的默認(rèn)配置有關(guān),從如圖1所示markdown.slugify函數(shù)可以看到,我們需要修改其配置。
markdown.slugify函數(shù)

圖1 markdown.slugify函數(shù)
點(diǎn)擊圖中的source,跳轉(zhuǎn)到GitHub的工程頁(yè)面,可以看到如下的代碼段:
// string.js slugify drops non ascii chars so we have to
// use a custom implementation here
// @ts-ignore
import { remove as removeDiacritics } from 'diacritics'
// eslint-disable-next-line no-control-regex
const rControl = /[\u0000-\u001f]/g
const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'<>,.?/]+/g
export = function slugify (str: string): string {
return removeDiacritics(str)
// Remove control characters
.replace(rControl, '')
// Replace special characters
.replace(rSpecial, '-')
// Remove continous separators
.replace(/\-{2,}/g, '-')
// Remove prefixing and trailing separtors
.replace(/^\-+|\-+$/g, '')
// ensure it doesn't start with a number (#121)
.replace(/^(\d)/, '_$1')
// lowercase
.toLowerCase()
}
看到了其中有一句ensure it doesn't start with a number (#121),可以知道這就是問題所在:
// ensure it doesn't start with a number (#121)
.replace(/^(\d)/, '_$1')
我們的標(biāo)題數(shù)字被這句代碼替換掉了,導(dǎo)致最終的鏈接根本沒有指向標(biāo)題,故無法跳轉(zhuǎn)。
問題解決
根據(jù)GitHub頁(yè)面上的配置路徑,找到自己安裝的vuepress模塊的配置路徑,我的路徑是:
D:\my_program\nodejs\node_global\node_modules\vuepress\node_modules\@vuepress\shared-utils\lib\slugify.js
打開 slugify.js 文件,并將上述的代碼段注釋掉,問題即可解決。
以上這篇vue添加錨點(diǎn),實(shí)現(xiàn)滾動(dòng)頁(yè)面時(shí)錨點(diǎn)添加相應(yīng)的class操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用自定義指令打開dialog的實(shí)現(xiàn)方法
在web后臺(tái)管理項(xiàng)目中,經(jīng)常要用到dialog,就vue來說,使用方式則是引入組件,注冊(cè),在template中使用,試想一下,如果我們需要在項(xiàng)目中的不同.vue文件中使用該dialog,但是又不想每次都在template中寫入組件該如何實(shí)現(xiàn)呢?本文我們介紹用指令控制dialog,需要的朋友可以參考下2024-07-07
vue?beforeDestroy?clearInterval清除定時(shí)器失效的解決
這篇文章主要介紹了vue?beforeDestroy?clearInterval清除定時(shí)器失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
解決vue單頁(yè)使用keep-alive頁(yè)面返回不刷新的問題
下面小編就為大家分享一篇解決vue單頁(yè)使用keep-alive頁(yè)面返回不刷新的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
VUE實(shí)時(shí)監(jiān)聽元素距離頂部高度的操作
這篇文章主要介紹了VUE實(shí)時(shí)監(jiān)聽元素距離頂部高度的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue-photo-preview圖片預(yù)覽失效的問題及解決
這篇文章主要介紹了vue-photo-preview圖片預(yù)覽失效的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
vue項(xiàng)目中運(yùn)用webpack動(dòng)態(tài)配置打包多種環(huán)境域名的方法
本人分享一個(gè)vue項(xiàng)目里,根據(jù)命令行輸入不同的命令,打包出不同環(huán)境域名的方法。需要的朋友跟隨小編一起看看吧2019-06-06

