From 5069a4d1df46b7866b25a7f8ca2bdc0e41731cdd Mon Sep 17 00:00:00 2001 From: dongsuo Date: Mon, 17 Jul 2017 17:47:16 +0800 Subject: [PATCH] =?UTF-8?q?'=E4=BF=AE=E6=94=B9=E8=BF=94=E5=9B=9E=E9=A1=B6?= =?UTF-8?q?=E9=83=A8=E5=8A=9F=E8=83=BD'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/BackToTop/index.vue | 65 +++++++++++++++++++----------- src/views/components/backToTop.vue | 29 +++++++++---- 2 files changed, 64 insertions(+), 30 deletions(-) diff --git a/src/components/BackToTop/index.vue b/src/components/BackToTop/index.vue index 10aaef1b..12cda74f 100644 --- a/src/components/BackToTop/index.vue +++ b/src/components/BackToTop/index.vue @@ -1,9 +1,12 @@ @@ -18,18 +21,39 @@ export default { backPosition: { type: Number, default: 0 + }, + customStyle: { + type: Object, + default: { + right: '50px', + bottom: '50px', + width: '40px', + height: '40px', + 'border-radius': '4px', + 'line-height': '40px', + background: '#e7eaf1' + } + }, + transitionName: { + type: String, + default: 'fade' } + }, data() { return { - visible: false + visible: false, + interval: null } }, mounted() { - window.addEventListener('scroll', this.handleScroll) + window.addEventListener('scroll', this.handleScroll); }, beforeDestroy() { - window.removeEventListener('scroll', this.handleScroll) + window.removeEventListener('scroll', this.handleScroll); + if (this.interval) { + clearInterval(this.interval); + } }, methods: { handleScroll() { @@ -38,11 +62,11 @@ export default { backToTop() { const start = window.pageYOffset; let i = 0; - const t = setInterval(() => { + this.interval = setInterval(() => { const next = Math.floor(this.easeInOutQuad(10 * i, start, -start, 500)); if (next <= this.backPosition) { window.scrollTo(0, this.backPosition); - clearInterval(t) + clearInterval(this.interval) } else { window.scrollTo(0, next); } @@ -59,21 +83,18 @@ export default { diff --git a/src/views/components/backToTop.vue b/src/views/components/backToTop.vue index b20bbcfe..ecf39575 100644 --- a/src/views/components/backToTop.vue +++ b/src/views/components/backToTop.vue @@ -1,6 +1,8 @@