43513f70
zhigang.li
add anchor component
|
1
2
3
4
5
|
<template>
<component :is="wrapperComponent" :offset-top="offsetTop" :offset-bottom="offsetBottom" @on-change="handleAffixStateChange">
<div :class="`${prefix}-wrapper`" :style="wrapperStyle">
<div :class="`${prefix}`">
<div :class="`${prefix}-ink`">
|
cf9a399e
zhigang.li
修改showInkInFixed为...
|
6
|
<span v-show="showInk" :class="`${prefix}-ink-ball`" :style="{top: `${inkTop}px`}"></span>
|
43513f70
zhigang.li
add anchor component
|
7
8
9
10
11
12
13
|
</div>
<slot></slot>
</div>
</div>
</component>
</template>
<script>
|
9d553a2b
zhigang.li
update
|
14
|
import { scrollTop, findComponentsDownward, sharpMatcherRegx } from '../../utils/assist';
|
43513f70
zhigang.li
add anchor component
|
15
16
17
|
import { on, off } from '../../utils/dom';
export default {
name: 'Anchor',
|
c69f8ff5
梁灏
update Anchor
|
18
19
20
21
22
|
provide () {
return {
anchorCom: this
};
},
|
43513f70
zhigang.li
add anchor component
|
23
24
25
26
27
|
data () {
return {
prefix: 'ivu-anchor',
isAffixed: false, // current affixed state
inkTop: 0,
|
43513f70
zhigang.li
add anchor component
|
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
animating: false, // if is scrolling now
currentLink: '', // current show link => #href -> currentLink = #href
currentId: '', // current show title id => #href -> currentId = href
scrollContainer: null,
scrollElement: null,
titlesOffsetArr: [],
wrapperTop: 0,
upperFirstTitle: true
};
},
props: {
affix: {
type: Boolean,
default: true
},
offsetTop: {
type: Number,
default: 0
},
offsetBottom: Number,
bounds: {
type: Number,
default: 5
},
|
a8e7c029
梁灏
update Anchor to ...
|
52
53
|
// container: [String, HTMLElement], // HTMLElement 在 SSR 下不支持
container: null,
|
cf9a399e
zhigang.li
修改showInkInFixed为...
|
54
|
showInk: {
|
43513f70
zhigang.li
add anchor component
|
55
56
|
type: Boolean,
default: false
|
e0f71ebf
zhigang.li
增加scroll-offset属性...
|
57
58
59
60
|
},
scrollOffset: {
type: Number,
default: 0
|
43513f70
zhigang.li
add anchor component
|
61
62
63
64
65
66
67
68
69
70
71
72
73
|
}
},
computed: {
wrapperComponent () {
return this.affix ? 'Affix' : 'div';
},
wrapperStyle () {
return {
maxHeight: this.offsetTop ? `calc(100vh - ${this.offsetTop}px)` : '100vh'
};
},
containerIsWindow () {
return this.scrollContainer === window;
|
43513f70
zhigang.li
add anchor component
|
74
75
76
77
78
79
80
81
82
83
84
85
86
|
}
},
methods: {
handleAffixStateChange (state) {
this.isAffixed = this.affix && state;
},
handleScroll (e) {
this.upperFirstTitle = e.target.scrollTop < this.titlesOffsetArr[0].offset;
if (this.animating) return;
this.updateTitleOffset();
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop || e.target.scrollTop;
this.getCurrentScrollAtTitleId(scrollTop);
},
|
43513f70
zhigang.li
add anchor component
|
87
88
89
|
handleHashChange () {
const url = window.location.href;
const sharpLinkMatch = sharpMatcherRegx.exec(url);
|
25257337
zhigang.li
解决anchor组件在页面没有点击...
|
90
|
if (!sharpLinkMatch) return;
|
43513f70
zhigang.li
add anchor component
|
91
92
93
94
95
|
this.currentLink = sharpLinkMatch[0];
this.currentId = sharpLinkMatch[1];
},
handleScrollTo () {
const anchor = document.getElementById(this.currentId);
|
145be7a6
zhigang.li
anchor-link组件添加sc...
|
96
97
98
99
100
101
|
const currentLinkElementA = document.querySelector(`a[data-href="${this.currentLink}"]`);
let offset = this.scrollOffset;
if (currentLinkElementA) {
offset = parseFloat(currentLinkElementA.getAttribute('data-scroll-offset'));
}
|
43513f70
zhigang.li
add anchor component
|
102
|
if (!anchor) return;
|
145be7a6
zhigang.li
anchor-link组件添加sc...
|
103
|
const offsetTop = anchor.offsetTop - this.wrapperTop - offset;
|
43513f70
zhigang.li
add anchor component
|
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
this.animating = true;
scrollTop(this.scrollContainer, this.scrollElement.scrollTop, offsetTop, 600, () => {
this.animating = false;
});
this.handleSetInkTop();
},
handleSetInkTop () {
const currentLinkElementA = document.querySelector(`a[data-href="${this.currentLink}"]`);
if (!currentLinkElementA) return;
const elementATop = currentLinkElementA.offsetTop;
const top = (elementATop < 0 ? this.offsetTop : elementATop);
this.inkTop = top;
},
updateTitleOffset () {
const links = findComponentsDownward(this, 'AnchorLink').map(link => {
return link.href;
});
|
4556cfa8
zhigang.li
fixed bug of anch...
|
121
122
123
124
125
|
const idArr = links.map(link => {
return link.split('#')[1];
});
let offsetArr = [];
idArr.forEach(id => {
|
43513f70
zhigang.li
add anchor component
|
126
|
const titleEle = document.getElementById(id);
|
4556cfa8
zhigang.li
fixed bug of anch...
|
127
128
|
if (titleEle) offsetArr.push({
link: `#${id}`,
|
43513f70
zhigang.li
add anchor component
|
129
|
offset: titleEle.offsetTop - this.scrollElement.offsetTop
|
4556cfa8
zhigang.li
fixed bug of anch...
|
130
|
});
|
43513f70
zhigang.li
add anchor component
|
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
});
this.titlesOffsetArr = offsetArr;
},
getCurrentScrollAtTitleId (scrollTop) {
let i = -1;
let len = this.titlesOffsetArr.length;
let titleItem = {
link: '#',
offset: 0
};
scrollTop += this.bounds;
while (++i < len) {
let currentEle = this.titlesOffsetArr[i];
let nextEle = this.titlesOffsetArr[i + 1];
if (scrollTop >= currentEle.offset && scrollTop < ((nextEle && nextEle.offset) || Infinity)) {
titleItem = this.titlesOffsetArr[i];
break;
}
}
this.currentLink = titleItem.link;
this.handleSetInkTop();
},
getContainer () {
this.scrollContainer = this.container ? (typeof this.container === 'string' ? document.querySelector(this.container) : this.container) : window;
this.scrollElement = this.container ? this.scrollContainer : (document.documentElement || document.body);
},
removeListener () {
off(this.scrollContainer, 'scroll', this.handleScroll);
off(window, 'hashchange', this.handleHashChange);
},
init () {
|
576329cc
zhigang.li
use link.js for a...
|
162
|
// const anchorLink = findComponentDownward(this, 'AnchorLink');
|
43513f70
zhigang.li
add anchor component
|
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
this.handleHashChange();
this.$nextTick(() => {
this.removeListener();
this.getContainer();
this.wrapperTop = this.containerIsWindow ? 0 : this.scrollElement.offsetTop;
this.handleScrollTo();
this.handleSetInkTop();
this.updateTitleOffset();
this.upperFirstTitle = this.scrollElement.scrollTop < this.titlesOffsetArr[0].offset;
on(this.scrollContainer, 'scroll', this.handleScroll);
on(window, 'hashchange', this.handleHashChange);
});
}
},
watch: {
'$route' () {
this.handleHashChange();
|
576329cc
zhigang.li
use link.js for a...
|
180
181
|
this.$nextTick(() => {
this.handleScrollTo();
|
9d553a2b
zhigang.li
update
|
182
|
});
|
43513f70
zhigang.li
add anchor component
|
183
184
185
186
187
188
189
190
191
|
},
container () {
this.init();
},
currentLink (newHref, oldHref) {
this.$emit('on-change', newHref, oldHref);
}
},
mounted () {
|
a1fb4790
zhigang.li
update
|
192
|
this.init();
|
43513f70
zhigang.li
add anchor component
|
193
194
195
196
197
198
|
},
beforeDestroy () {
this.removeListener();
}
};
</script>
|