diff --git a/examples/routers/anchor.vue b/examples/routers/anchor.vue index 15e59b1..e53f294 100644 --- a/examples/routers/anchor.vue +++ b/examples/routers/anchor.vue @@ -2,10 +2,12 @@
@@ -30,6 +32,8 @@ +
+

这是新添加的哦哦哦哦哦 哦

+

这是信息司大是大非胜多负少的{{i}}

@@ -63,7 +69,8 @@ export default { container: null, value1: '1', scrollCon: '', - con: 'div' + con: 'div', + showNewLink: false } }, methods: { @@ -76,6 +83,9 @@ export default { }, handleSelect (href) { console.log(`select ${href}`) + }, + andLink () { + this.showNewLink = true; } }, mounted () { diff --git a/src/components/anchor/anchor-link.vue b/src/components/anchor/anchor-link.vue index 445a3f0..a009e1f 100644 --- a/src/components/anchor/anchor-link.vue +++ b/src/components/anchor/anchor-link.vue @@ -40,6 +40,11 @@ export default { goAnchor () { this.parentAnchor.turnTo(this.href); } + }, + mounted () { + this.$nextTick(() => { + this.parentAnchor.init(); + }); } }; diff --git a/src/components/anchor/anchor.vue b/src/components/anchor/anchor.vue index 7b38ee0..278e2ec 100644 --- a/src/components/anchor/anchor.vue +++ b/src/components/anchor/anchor.vue @@ -112,13 +112,16 @@ export default { const links = findComponentsDownward(this, 'AnchorLink').map(link => { return link.href; }); - const offsetArr = links.map(link => { - const id = link.split('#')[1]; + const idArr = links.map(link => { + return link.split('#')[1]; + }); + let offsetArr = []; + idArr.forEach(id => { const titleEle = document.getElementById(id); - return { - link: link, + if (titleEle) offsetArr.push({ + link: `#${id}`, offset: titleEle.offsetTop - this.scrollElement.offsetTop - }; + }); }); this.titlesOffsetArr = offsetArr; }, -- libgit2 0.21.4