From 936dae09ecf37723577d20ad9ab8640fdbfbe315 Mon Sep 17 00:00:00 2001 From: An Phan Date: Thu, 19 Jan 2017 16:35:20 +0800 Subject: [PATCH] Refactor the "Back to Top" button component --- .../main-wrapper/main-content/albums.vue | 4 +- .../main-wrapper/main-content/artists.vue | 2 +- .../main-wrapper/main-content/home.vue | 2 +- .../js/components/shared/to-top-button.vue | 42 +++++++++++++------ resources/assets/js/mixins/infinite-scroll.js | 14 +------ resources/assets/js/utils/$.js | 3 +- 6 files changed, 37 insertions(+), 30 deletions(-) diff --git a/resources/assets/js/components/main-wrapper/main-content/albums.vue b/resources/assets/js/components/main-wrapper/main-content/albums.vue index 0afa3636..ac3276f0 100644 --- a/resources/assets/js/components/main-wrapper/main-content/albums.vue +++ b/resources/assets/js/components/main-wrapper/main-content/albums.vue @@ -5,10 +5,10 @@ -
+
- +
diff --git a/resources/assets/js/components/main-wrapper/main-content/artists.vue b/resources/assets/js/components/main-wrapper/main-content/artists.vue index 25b11295..6911598e 100644 --- a/resources/assets/js/components/main-wrapper/main-content/artists.vue +++ b/resources/assets/js/components/main-wrapper/main-content/artists.vue @@ -8,7 +8,7 @@
- +
diff --git a/resources/assets/js/components/main-wrapper/main-content/home.vue b/resources/assets/js/components/main-wrapper/main-content/home.vue index a10aec76..29df8845 100644 --- a/resources/assets/js/components/main-wrapper/main-content/home.vue +++ b/resources/assets/js/components/main-wrapper/main-content/home.vue @@ -68,7 +68,7 @@
- + diff --git a/resources/assets/js/components/shared/to-top-button.vue b/resources/assets/js/components/shared/to-top-button.vue index 9cb4a18d..aea6e6d0 100644 --- a/resources/assets/js/components/shared/to-top-button.vue +++ b/resources/assets/js/components/shared/to-top-button.vue @@ -1,15 +1,37 @@ @@ -27,11 +49,7 @@ export default { opacity: 1; transition: opacity .5s; - &.fade-enter { - opacity: 0; - } - - &.fade-leave { + &.fade-enter, &.fade-leave-to { opacity: 0; } diff --git a/resources/assets/js/mixins/infinite-scroll.js b/resources/assets/js/mixins/infinite-scroll.js index 445f0757..cb68e8fc 100644 --- a/resources/assets/js/mixins/infinite-scroll.js +++ b/resources/assets/js/mixins/infinite-scroll.js @@ -1,4 +1,3 @@ -import { $ } from '../utils' import toTopButton from '../components/shared/to-top-button.vue' /** @@ -12,8 +11,7 @@ export default { data () { return { numOfItems: 30, // Number of currently loaded and displayed items - perPage: 30, // Number of items to be loaded per "page" - showBackToTop: false + perPage: 30 // Number of items to be loaded per "page" } }, @@ -24,8 +22,6 @@ export default { if (e.target.scrollTop + e.target.clientHeight >= e.target.scrollHeight - 32) { this.displayMore() } - - this.showBackToTop = e.target.scrollTop > 64 }, /** @@ -33,14 +29,6 @@ export default { */ displayMore () { this.numOfItems += this.perPage - }, - - /** - * Scroll to top of the wrapper. - */ - scrollToTop () { - $.scrollTo(this.$refs.wrapper, 0, 500) - this.showBackToTop = false } } } diff --git a/resources/assets/js/utils/$.js b/resources/assets/js/utils/$.js index 777ed838..c147120d 100644 --- a/resources/assets/js/utils/$.js +++ b/resources/assets/js/utils/$.js @@ -36,7 +36,7 @@ export const $ = { } }, - scrollTo (el, to, duration) { + scrollTo (el, to, duration, cb = null) { if (duration <= 0 || !el) { return } @@ -45,6 +45,7 @@ export const $ = { window.setTimeout(() => { el.scrollTop = el.scrollTop + perTick if (el.scrollTop === to) { + cb && cb() return } this.scrollTo(el, to, duration - 10)