Skip to content

Commit 0aad8e2

Browse files
committed
详情页的文章目录支持定位
1 parent 1510832 commit 0aad8e2

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/views/Detail.vue

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
</div>
3131
<prev-and-next v-if="getResult" :prevInfo="prevInfo" :nextInfo="nextInfo"></prev-and-next>
3232
</div>
33-
<div class="detail-content">
34-
<div class="article-content markdown-body" v-html="handleDetail"></div>
33+
<div :class="{ 'fixed-toc-box': tocFixed }" class="detail-content">
34+
<div ref="articleContent" class="article-content markdown-body" v-html="handleDetail"></div>
3535
</div>
3636
<prev-and-next v-if="getResult" :prevInfo="prevInfo" :nextInfo="nextInfo" style="padding: 15px;"></prev-and-next>
3737
</el-main>
@@ -62,7 +62,8 @@ export default {
6262
nextInfo: {},
6363
authorInfo: {},
6464
getResult: false,
65-
clipboard: ''
65+
clipboard: '',
66+
tocFixed: false
6667
}
6768
},
6869
components: {
@@ -153,6 +154,10 @@ export default {
153154
this.$message.error('复制成功失败')
154155
})
155156
})
157+
window.addEventListener('scroll', this.pageScroll, false)
158+
},
159+
beforeDestroy () {
160+
window.removeEventListener('scroll', this.pageScroll, false)
156161
},
157162
methods: {
158163
async apiArticleDetailMethod () {
@@ -189,6 +194,16 @@ export default {
189194
})
190195
this.info.articleStart = result.data.articleStart
191196
}
197+
},
198+
pageScroll () {
199+
if (document.querySelector('.markdownIt-TOC')) {
200+
const pageScrollTop = document.documentElement.scrollTop
201+
if (pageScrollTop >= 200) {
202+
this.tocFixed = true
203+
} else {
204+
this.tocFixed = false
205+
}
206+
}
192207
}
193208
},
194209
watch: {
@@ -219,6 +234,16 @@ export default {
219234
line-height: 26px;
220235
}
221236
}
237+
.fixed-toc-box {
238+
.markdownIt-TOC {
239+
position: fixed;
240+
top: 80px;
241+
left: 50%;
242+
z-index: 100;
243+
margin-left: 575px;
244+
max-width: 200px;
245+
}
246+
}
222247
.article-content {
223248
font-size: 14px;
224249
color: #333;

0 commit comments

Comments
 (0)