Skip to content

Commit

Permalink
fix: init catalog position after router change
Browse files Browse the repository at this point in the history
  • Loading branch information
Renovamen committed Feb 8, 2021
1 parent 0435bf6 commit b5bbf17
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions packages/theme-gungnir/layouts/Post.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ const catalogTopFixed = 80
export default {
name: 'Post',
components: {
ArticleHeader,
Common,
Catalog,
Page
},
data () {
return {
isFixed: false,
catalogTop: 0,
headerHeight: 0,
screenWidth: 0
}
},
computed: {
pageStyle () {
return this.$showCatalog ? {} : {
Expand All @@ -61,28 +65,43 @@ export default {
return style
}
},
watch: {
'$route' () {
this.$nextTick(() => {
this.initCatalog()
})
}
},
mounted () {
this.headerHeight = document.querySelector('.post-header').offsetHeight
this.screenWidth = document.body.clientWidth
if (this.screenWidth <= 719) this.catalogTop = -15 // $MQMobile
else this.catalogTop = this.headerHeight + catalogTopAbsolute
this.initCatalog()
const that = this
window.onresize = () => {
return (() => {
that.headerHeight = document.querySelector('.post-header').offsetHeight
that.screenWidth = document.body.clientWidth
that.initCatalog()
that.handleScroll()
})()
}
window.addEventListener('scroll', throttle(this.handleScroll, 50))
},
beforeDestroy () {
window.removeEventListener('scroll', throttle(this.handleScroll, 50))
},
methods: {
initCatalog() {
this.headerHeight = document.querySelector('.post-header').offsetHeight
this.screenWidth = document.body.clientWidth
if (this.screenWidth <= 719) this.catalogTop = -15 // $MQMobile
else this.catalogTop = this.headerHeight + catalogTopAbsolute
},
handleScroll () {
var currentTop = window.pageYOffset
const currentTop = window.pageYOffset
if (currentTop > (this.headerHeight + catalogTopAbsolute - catalogTopFixed)) {
this.isFixed = true
this.catalogTop = catalogTopFixed
Expand Down

1 comment on commit b5bbf17

@vercel
Copy link

@vercel vercel bot commented on b5bbf17 Feb 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.