Commit 145be7a6ad7ca5a20862bf0034342fc7c4125e49
1 parent
e0f71ebf
anchor-link组件添加scroll-offset属性,会优先使用anchor-link的这个值
Showing
3 changed files
with
17 additions
and
5 deletions
Show diff stats
examples/routers/anchor.vue
... | ... | @@ -5,9 +5,9 @@ |
5 | 5 | <Button @click="andLink">添加一个连接</Button> |
6 | 6 | <Anchor :bounds="100" @on-change="handleChange" @on-select="handleSelect" :style="{right: '100px'}" :affix="true" :offset-top="30" :scroll-offset="100" :container="scrollCon" show-ink-in-fixed> |
7 | 7 | <AnchorLink v-if="(link - 1) % 30 === 0" v-for="link in 300" :key="`link${link}`" :href="`#title-${link}`" :title="`title-${link}`"> |
8 | - <AnchorLink v-if="link === 61" href="#title-child-69" title="title-child-69"/> | |
8 | + <AnchorLink :scroll-offset="20" v-if="link === 61" href="#title-child-69" title="title-child-69"/> | |
9 | 9 | </AnchorLink> |
10 | - <AnchorLink href="#lishi" title="李氏专跳"/> | |
10 | + <AnchorLink :scroll-offset="200" href="#lishi" title="李氏专跳"/> | |
11 | 11 | <AnchorLink v-if="showNewLink" href="#new-link" title="这是动态添加的连接"/> |
12 | 12 | </Anchor> |
13 | 13 | </div> | ... | ... |
src/components/anchor/anchor-link.vue
1 | 1 | <template> |
2 | 2 | <div :class="anchorLinkClasses"> |
3 | - <a :class="linkTitleClasses" :href="href" :data-href="href" @click.prevent="goAnchor" :title="title">{{ title }}</a> | |
3 | + <a :class="linkTitleClasses" :href="href" :data-scroll-offset="scrollOffset" :data-href="href" @click.prevent="goAnchor" :title="title">{{ title }}</a> | |
4 | 4 | <slot></slot> |
5 | 5 | </div> |
6 | 6 | </template> |
... | ... | @@ -10,7 +10,13 @@ export default { |
10 | 10 | inject: ['anchorCom'], |
11 | 11 | props: { |
12 | 12 | href: String, |
13 | - title: String | |
13 | + title: String, | |
14 | + scrollOffset: { | |
15 | + type: Number, | |
16 | + default () { | |
17 | + return this.anchorCom.scrollOffset | |
18 | + } | |
19 | + } | |
14 | 20 | }, |
15 | 21 | data () { |
16 | 22 | return { | ... | ... |
src/components/anchor/anchor.vue
... | ... | @@ -95,8 +95,14 @@ export default { |
95 | 95 | }, |
96 | 96 | handleScrollTo () { |
97 | 97 | const anchor = document.getElementById(this.currentId); |
98 | + const currentLinkElementA = document.querySelector(`a[data-href="${this.currentLink}"]`); | |
99 | + let offset = this.scrollOffset; | |
100 | + if (currentLinkElementA) { | |
101 | + offset = parseFloat(currentLinkElementA.getAttribute('data-scroll-offset')); | |
102 | + } | |
103 | + | |
98 | 104 | if (!anchor) return; |
99 | - const offsetTop = anchor.offsetTop - this.wrapperTop - this.scrollOffset; | |
105 | + const offsetTop = anchor.offsetTop - this.wrapperTop - offset; | |
100 | 106 | this.animating = true; |
101 | 107 | scrollTop(this.scrollContainer, this.scrollElement.scrollTop, offsetTop, 600, () => { |
102 | 108 | this.animating = false; | ... | ... |