2017-07-17 07:33:21 +00:00
|
|
|
<template>
|
2017-07-17 09:47:16 +00:00
|
|
|
<transition :name="transitionName">
|
2018-08-19 08:55:24 +00:00
|
|
|
<div v-show="visible" :style="customStyle" class="back-to-ceiling" @click="backToTop">
|
2017-07-17 09:47:16 +00:00
|
|
|
<svg width="16" height="16" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg" class="Icon Icon--backToTopArrow" aria-hidden="true" style="height: 16px; width: 16px;">
|
|
|
|
<title>回到顶部</title>
|
|
|
|
<g>
|
2019-03-17 09:40:51 +00:00
|
|
|
<path d="M12.036 15.59c0 .55-.453.995-.997.995H5.032c-.55 0-.997-.445-.997-.996V8.584H1.03c-1.1 0-1.36-.633-.578-1.416L7.33.29c.39-.39 1.026-.385 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.004z" fill-rule="evenodd" />
|
2017-07-17 09:47:16 +00:00
|
|
|
</g>
|
|
|
|
</svg>
|
2017-07-17 07:33:21 +00:00
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</template>
|
2017-07-17 10:00:28 +00:00
|
|
|
|
2017-07-17 07:33:21 +00:00
|
|
|
<script>
|
2017-08-22 07:43:34 +00:00
|
|
|
export default {
|
|
|
|
name: 'BackToTop',
|
|
|
|
props: {
|
|
|
|
visibilityHeight: {
|
|
|
|
type: Number,
|
|
|
|
default: 400
|
|
|
|
},
|
|
|
|
backPosition: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
2017-07-17 09:47:16 +00:00
|
|
|
},
|
2017-08-22 07:43:34 +00:00
|
|
|
customStyle: {
|
|
|
|
type: Object,
|
2018-04-06 04:40:58 +00:00
|
|
|
default: function() {
|
|
|
|
return {
|
|
|
|
right: '50px',
|
|
|
|
bottom: '50px',
|
|
|
|
width: '40px',
|
|
|
|
height: '40px',
|
|
|
|
'border-radius': '4px',
|
|
|
|
'line-height': '45px',
|
|
|
|
background: '#e7eaf1'
|
|
|
|
}
|
2017-07-17 09:47:16 +00:00
|
|
|
}
|
|
|
|
},
|
2017-08-22 07:43:34 +00:00
|
|
|
transitionName: {
|
|
|
|
type: String,
|
|
|
|
default: 'fade'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
visible: false,
|
2018-08-24 05:45:03 +00:00
|
|
|
interval: null,
|
|
|
|
isMoving: false
|
2017-08-22 07:43:34 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
window.addEventListener('scroll', this.handleScroll)
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
window.removeEventListener('scroll', this.handleScroll)
|
|
|
|
if (this.interval) {
|
|
|
|
clearInterval(this.interval)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
handleScroll() {
|
|
|
|
this.visible = window.pageYOffset > this.visibilityHeight
|
2017-07-17 07:33:21 +00:00
|
|
|
},
|
2017-08-22 07:43:34 +00:00
|
|
|
backToTop() {
|
2018-08-24 05:45:03 +00:00
|
|
|
if (this.isMoving) return
|
2017-08-22 07:43:34 +00:00
|
|
|
const start = window.pageYOffset
|
|
|
|
let i = 0
|
2018-08-24 05:45:03 +00:00
|
|
|
this.isMoving = true
|
2017-08-22 07:43:34 +00:00
|
|
|
this.interval = setInterval(() => {
|
|
|
|
const next = Math.floor(this.easeInOutQuad(10 * i, start, -start, 500))
|
|
|
|
if (next <= this.backPosition) {
|
|
|
|
window.scrollTo(0, this.backPosition)
|
|
|
|
clearInterval(this.interval)
|
2018-08-24 05:45:03 +00:00
|
|
|
this.isMoving = false
|
2017-08-22 07:43:34 +00:00
|
|
|
} else {
|
|
|
|
window.scrollTo(0, next)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}, 16.7)
|
2017-07-17 07:33:21 +00:00
|
|
|
},
|
2017-08-22 07:43:34 +00:00
|
|
|
easeInOutQuad(t, b, c, d) {
|
|
|
|
if ((t /= d / 2) < 1) return c / 2 * t * t + b
|
|
|
|
return -c / 2 * (--t * (t - 2) - 1) + b
|
2017-07-17 07:33:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-22 07:43:34 +00:00
|
|
|
}
|
2017-07-17 07:33:21 +00:00
|
|
|
</script>
|
2017-07-17 10:00:28 +00:00
|
|
|
|
2017-07-17 07:33:21 +00:00
|
|
|
<style scoped>
|
2019-04-01 09:07:16 +00:00
|
|
|
.back-to-ceiling {
|
|
|
|
position: fixed;
|
|
|
|
display: inline-block;
|
|
|
|
text-align: center;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
2017-07-17 09:47:16 +00:00
|
|
|
|
2019-04-01 09:07:16 +00:00
|
|
|
.back-to-ceiling:hover {
|
|
|
|
background: #d5dbe7;
|
|
|
|
}
|
2017-07-17 07:33:21 +00:00
|
|
|
|
2019-04-01 09:07:16 +00:00
|
|
|
.fade-enter-active,
|
|
|
|
.fade-leave-active {
|
|
|
|
transition: opacity .5s;
|
|
|
|
}
|
2017-07-17 07:33:21 +00:00
|
|
|
|
2019-04-01 09:07:16 +00:00
|
|
|
.fade-enter,
|
|
|
|
.fade-leave-to {
|
|
|
|
opacity: 0
|
|
|
|
}
|
2017-07-17 07:33:21 +00:00
|
|
|
|
2019-04-01 09:07:16 +00:00
|
|
|
.back-to-ceiling .Icon {
|
|
|
|
fill: #9aaabf;
|
|
|
|
background: none;
|
|
|
|
}
|
2017-07-17 07:33:21 +00:00
|
|
|
</style>
|