Skip to content

Commit d7e6d16

Browse files
authored
fix($SidebarLinks): fix full path link jump and other questions
1 parent bbdddcc commit d7e6d16

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

packages/vuepress-theme-reco/components/SidebarLinks.vue

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,60 @@ export default {
5353
}
5454
},
5555
56-
mounted () {
56+
mounted() {
57+
this.activationLink()
5758
this.isInViewPortOfOne()
5859
},
59-
60+
6061
updated: function () {
6162
this.isInViewPortOfOne()
6263
},
6364
6465
methods: {
66+
activationLink () {
67+
let subtitleName = decodeURIComponent(this.$route.fullPath)
68+
if (!subtitleName || subtitleName=='') return
69+
const subtitles = [].slice.call(document.querySelectorAll(AHL_SIDEBAR_LINK_SELECTOR))
70+
for(let i = 0; i < subtitles.length; i++) {
71+
if(decodeURIComponent(subtitles[i].getAttribute('href')).indexOf(subtitleName) != -1){
72+
subtitles[i].click()
73+
this.activationAnchor()
74+
return
75+
}
76+
}
77+
},
78+
79+
activationAnchor() {
80+
let anchors = [].slice.call(document.querySelectorAll(AHL_HEADER_ANCHOR_SELECTOR))
81+
.filter(anchor => decodeURIComponent(this.$route.fullPath).indexOf(decodeURIComponent(anchor.hash)) != -1)
82+
if(anchors == null || anchors.length<1 || anchors[0].offsetTop == undefined) return
83+
setTimeout(function(){
84+
window.scrollTo(0, anchors[0].offsetTop + 160)
85+
},100)
86+
},
87+
6588
isInViewPortOfOne () {
66-
const siderbarScroll = document.getElementsByClassName('sidebar')[0]
67-
let el = document.getElementsByClassName('active sidebar-link')[1]
68-
if (el === null || el.offsetTop == undefined) {
69-
el = document.getElementsByClassName('active sidebar-link')[0]
89+
let siderbarScroll = document.getElementsByClassName("sidebar")[0]
90+
let el = document.getElementsByClassName("active sidebar-link")[1]
91+
if (el ==null || el == undefined || el.offsetTop == undefined) {
92+
el = document.getElementsByClassName("active sidebar-link")[0]
7093
}
71-
if (el === null || el.offsetTop == undefined) return
72-
94+
if (el ==null || el == undefined || el.offsetTop == undefined) return
95+
7396
const viewPortHeight = siderbarScroll.clientHeight || window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
74-
const offsetBottom = el.offsetTop + el.offsetHeight
75-
const scrollTop = siderbarScroll.scrollTop
76-
const isView = (offsetBottom <= viewPortHeight + scrollTop)
77-
if (!isView) {
97+
let offsetTop = el.offsetTop
98+
let offsetBottom = el.offsetTop + el.offsetHeight
99+
let scrollTop = siderbarScroll.scrollTop
100+
let bottomVisible = (offsetBottom <= viewPortHeight + scrollTop)
101+
if (!bottomVisible) {
78102
siderbarScroll.scrollTop = (offsetBottom + 5 - viewPortHeight)
79103
}
104+
let topVisible = (offsetTop >= scrollTop)
105+
if (!topVisible) {
106+
siderbarScroll.scrollTop = (offsetTop - 5)
107+
}
80108
},
81-
109+
82110
refreshIndex () {
83111
const index = resolveOpenGroupIndex(
84112
this.$route,

0 commit comments

Comments
 (0)