vue中錨點(diǎn)的三種方法
第一種:
router.js中添加
mode: 'history', srcollBehavior(to,from,savedPosition){ if(to.hash){ return { selector:to.hash } } }
組件:
<template> <div> <ul class="list"> <li><a href="#1" rel="external nofollow" >星期1</a></li> <li><a href="#2" rel="external nofollow" >星期2</a></li> <li><a href="#3" rel="external nofollow" >星期3</a></li> <li><a href="#4" rel="external nofollow" >星期4</a></li> <li><a href="#5" rel="external nofollow" >星期5</a></li> <li><a href="#6" rel="external nofollow" >星期6</a></li> <li><a href="#7" rel="external nofollow" >星期7</a></li> </ul> <div class="main_con" id="1">11111111111111111111111111111111</div> <div class="main_con" id="2">22222222222222222222222222222222222</div> <div class="main_con" id="3">33333333333333333333333333333333333333</div> <div class="main_con" id="4">444444444444444444444444444444444444444</div> <div class="main_con" id="5">555555555555555555555555555555555555555</div> <div class="main_con" id="6">666666666666666666666666666666666666666</div> <div class="main_con" id="7">7777777777777777777777777777777777777777</div> </div> </template> <script> export default { data(){ return { } } } </script> <style> .list{ width: 100%; height: 50px; } li{ width: 11%; height: 50px; line-height: 50px; text-align: center; border: 1px solid #ccc; color: #ff6c00; float: left; list-style: none!important; } .main_con{ width: 100%; height: 200px; border: 1px solid #ccc; line-height: 200px; text-align: center; color: blue; } </style>
第二種:
寫一個(gè)方法 組件
<template> <div> <div><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" @click="goAnchor('#anchor-'+index)" v-for="index in 20"> {{index}} </a></div> <div :id="'anchor-'+index" class="item" v-for="index in 20">{{index}}</div> </div> </template> <script> export default{ data(){ return { } }, methods: { goAnchor(selector) { var anchor = this.$el.querySelector(selector) document.documentElement.scrollTop = anchor.offsetTop } } } </script> <style> .item{ width: 100%; height: 200px; line-height: 200px; text-align: center; } </style>
第三種: 自定義指令
<template> <div> <div><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" v-anchor="index" v-for="index in 20"> {{index}} </a></div> <div :id="'anchor-'+index" class="item" v-for="index in 20" >{{index}}</div> </div> </template> <script> export default{ data(){ return { } } } </script> <style> .item{ width: 100%; height: 200px; line-height: 200px; text-align: center; } </style>
main.js 定義全局指令 方便其他地方復(fù)用
Vue.directive('anchor',{ inserted : function(el,binding){ el.onclick = function(){ document.documentElement.scrollTop = $('#anchor-'+binding.value).offset().top } } })
總結(jié)
以上所述是小編給大家介紹的vue中錨點(diǎn)的三種方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue.js 中的 v-model 指令及綁定表單元素的方法
這篇文章主要介紹了Vue.js 中的 v-model 指令及綁定表單元素的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12vue-router實(shí)現(xiàn)組件間的跳轉(zhuǎn)(參數(shù)傳遞)
這篇文章主要為大家詳細(xì)介紹了vue-router實(shí)現(xiàn)組件間的跳轉(zhuǎn),參數(shù)傳遞方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11element-ui table行點(diǎn)擊獲取行索引(index)并利用索引更換行順序
這篇文章主要介紹了element-ui table行點(diǎn)擊獲取行索引(index)并利用索引更換行順序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02vue+canvas實(shí)現(xiàn)炫酷時(shí)鐘效果的倒計(jì)時(shí)插件(已發(fā)布到npm的vue2插件,開箱即用)
這篇文章主要介紹了vue+canvas實(shí)現(xiàn)炫酷時(shí)鐘效果的倒計(jì)時(shí)插件(已發(fā)布到npm的vue2插件,開箱即用) ,需要的朋友可以參考下2018-11-11Vue網(wǎng)頁(yè)html轉(zhuǎn)換PDF(最低兼容ie10)的思路詳解
這篇文章主要介紹了Vue網(wǎng)頁(yè)html轉(zhuǎn)換PDF(最低兼容ie10)的思路詳解,實(shí)現(xiàn)此功能需要引入兩個(gè)插件,需要的朋友可以參考下2017-08-08如何用vue-pdf包實(shí)現(xiàn)pdf文件預(yù)覽,支持分頁(yè)
這篇文章主要介紹了如何用vue-pdf包實(shí)現(xiàn)pdf文件預(yù)覽,支持分頁(yè)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vuex通過getters訪問數(shù)據(jù)為undefined問題及解決
這篇文章主要介紹了vuex通過getters訪問數(shù)據(jù)為undefined問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08