Vue路由與a標(biāo)簽鏈接錨點(diǎn)發(fā)生沖突問(wèn)題及解決
Vue路由與a標(biāo)簽鏈接錨點(diǎn)發(fā)生沖突
近期在vue項(xiàng)目中,使用了a標(biāo)簽錨點(diǎn)定位對(duì)應(yīng)內(nèi)容的時(shí)候發(fā)現(xiàn)路由也發(fā)生了變化,此時(shí)如果去刷新頁(yè)面則會(huì)出現(xiàn)找不到頁(yè)面的情況。
如果直接使用下面的方法進(jìn)行錨鏈接,會(huì)導(dǎo)致路由變成xxx,這樣顯然不是我們需要的
<a href="#xxx" rel="external nofollow" ></a> <div id="xxx"></div>
采用下面方法解決
<a @click.prevent="anchor('comment')">點(diǎn)擊我跳轉(zhuǎn)至comment</a> <div id="comment">我是comment區(qū)域</div>
methods: { /*錨鏈接跳轉(zhuǎn)*/ anchor(anchorName) { /*找到錨點(diǎn)*/ let anchorElement = document.getElementById(anchorName); /*如果對(duì)應(yīng)id的錨點(diǎn)存在,就跳轉(zhuǎn)到錨點(diǎn)*/ if(anchorElement) { anchorElement.scrollIntoView(); } } }
這樣,路由就不會(huì)發(fā)生變化了。
錨點(diǎn)跳轉(zhuǎn)方法二
1、先在需要跳轉(zhuǎn)的對(duì)應(yīng)板塊上添加 id
<!-- 第一塊對(duì)比表 --> <table1 :abnormalData="abnormalData1" id="table1"></table1> <!-- 第二塊對(duì)比表 --> <table2 :abnormalData="abnormalData2" id="table2"></table2> <!-- 第三塊對(duì)比表 --> <table3 :abnormalData="abnormalData3" id="table3"></table3> <!-- 第四塊對(duì)比表 --> <table4 :abnormalData="abnormalData4" :abnormalData2="abnormalData41" id="table4"></table4> <!-- 第五塊對(duì)比表 --> <table5 :abnormalData="abnormalData5" id="table5"></table5> <!-- 第六塊對(duì)比表 --> <table6 :abnormalData="abnormalData6" id="table6"></table6> <!-- 第七塊對(duì)比表 --> <table7 :abnormalData="abnormalData7" id="table7"></table7>
2、在導(dǎo)航的 a 標(biāo)簽上添加事件
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="goAnchor('#table1')">導(dǎo)航1</a> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="goAnchor('#table2')">導(dǎo)航2</a> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="goAnchor('#table3')">導(dǎo)航3</a> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="goAnchor('#table4')">導(dǎo)航4</a> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="goAnchor('#table5')">導(dǎo)航5</a> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="goAnchor('#table6')">導(dǎo)航6</a> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="goAnchor('#table7')">導(dǎo)航7</a>
注意每一塊的 id 一一對(duì)應(yīng)
3、在 methods 中添加跳轉(zhuǎn)的方法:
methods: { //模擬錨點(diǎn)跳轉(zhuǎn) goAnchor(selector) { document.querySelector(selector).scrollIntoView({ behavior: "smooth" }); }, },
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用Vue.js實(shí)現(xiàn)求職在線之職位查詢(xún)功能
Vue.js是當(dāng)下很火的一個(gè)JavaScript MVVM庫(kù),它是以數(shù)據(jù)驅(qū)動(dòng)和組件化的思想構(gòu)建的。下面這篇文章主要給大家介紹了關(guān)于利用Vue.js實(shí)現(xiàn)求職在線之職位查詢(xún)功能的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07vue3 setup點(diǎn)擊跳轉(zhuǎn)頁(yè)面的實(shí)現(xiàn)示例
本文主要介紹了vue3 setup點(diǎn)擊跳轉(zhuǎn)頁(yè)面的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10實(shí)現(xiàn)elementUI表單的全局驗(yàn)證的方法步驟
這篇文章主要介紹了實(shí)現(xiàn)elementUI表單的全局驗(yàn)證的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04atom-design(Vue.js移動(dòng)端組件庫(kù))手勢(shì)組件使用教程
這篇文章主要介紹了atom-design(Vue.js移動(dòng)端組件庫(kù))手勢(shì)組件使用教程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05vue組件開(kāi)發(fā)之用戶(hù)無(wú)限添加自定義填寫(xiě)表單的方法
今天小編就為大家分享一篇vue組件開(kāi)發(fā)之用戶(hù)無(wú)限添加自定義填寫(xiě)表單的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08