From ae02d37385f013ae626d4d0286e7f5769ca1a2b1 Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Thu, 4 May 2017 22:55:49 +0200 Subject: [PATCH] Beautiful Go to Top button :) --- dashboard/src/app/album.component.css | 32 ++++++++++++++++++++++++++ dashboard/src/app/album.component.html | 12 ++++++---- dashboard/src/app/album.component.ts | 30 +++++++++++++++++++----- 3 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 dashboard/src/app/album.component.css diff --git a/dashboard/src/app/album.component.css b/dashboard/src/app/album.component.css new file mode 100644 index 0000000..ed5f6e0 --- /dev/null +++ b/dashboard/src/app/album.component.css @@ -0,0 +1,32 @@ +/* Thank to https://codyhouse.co/gem/back-to-top/ */ + +.btn-top { + display: inline-block; + height: 40px; + width: 40px; + position: fixed; + bottom: 20px; + right: 20px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.05); + /* image replacement properties */ + /*overflow: hidden;*/ + /*text-indent: 100%;*/ + /*white-space: nowrap;*/ + /*background: rgba(232, 98, 86, 0.8);*/ + /*background: #ff9000;*/ + visibility: hidden; + opacity: 0; + -webkit-transition: opacity .3s 0s, visibility 0s .3s; + -moz-transition: opacity .3s 0s, visibility 0s .3s; + transition: opacity .3s 0s, visibility 0s .3s; +} +.btn-top.btn-top-is-visible, .no-touch .btn-top:hover { + -webkit-transition: opacity .3s 0s, visibility 0s 0s; + -moz-transition: opacity .3s 0s, visibility 0s 0s; + transition: opacity .3s 0s, visibility 0s 0s; +} +.btn-top.btn-top-is-visible { + /* the button becomes visible */ + visibility: visible; + opacity: 1; +} \ No newline at end of file diff --git a/dashboard/src/app/album.component.html b/dashboard/src/app/album.component.html index 97a1ee9..47c8d89 100644 --- a/dashboard/src/app/album.component.html +++ b/dashboard/src/app/album.component.html @@ -8,13 +8,15 @@ + + - +
@@ -37,10 +39,12 @@
- - \ No newline at end of file diff --git a/dashboard/src/app/album.component.ts b/dashboard/src/app/album.component.ts index d3b4380..82ae3e4 100644 --- a/dashboard/src/app/album.component.ts +++ b/dashboard/src/app/album.component.ts @@ -8,8 +8,8 @@ import { Album } from './object/album'; @Component({ selector: 'album-component', - templateUrl: './album.component.html' -// styleUrls: [ './album.component.css' ] + templateUrl: './album.component.html', + styleUrls: [ './album.component.css' ] }) export class AlbumComponent implements OnInit { @@ -22,7 +22,8 @@ export class AlbumComponent implements OnInit { albumName = ""; songs: Array = []; album: Album = new Album(); // If album not found, will be replaced by 'undefined' - more: boolean = false; + moreDataAvailable: boolean = false; + atBottom: boolean = false; ngOnInit(): void { this.route.params @@ -40,8 +41,9 @@ export class AlbumComponent implements OnInit { loadSongs(): void { this.elsService.getAlbumSongs(this.albumName, this.songs.length).subscribe( data => { - this.more = data.length == ElsService.DEFAULT_SIZE - // Init dashboard for first load, then add elements + this.moreDataAvailable = data.length == ElsService.DEFAULT_SIZE + + // Erase song array with result for first load, then add elements one by one // instead use concat => concat will sort table at each load, very consuming! and not user friendly if (this.songs.length == 0) { this.songs = data; @@ -58,9 +60,25 @@ export class AlbumComponent implements OnInit { window.scrollTo(0,0); } + /** + * Handle scroll: + * - load data if at bottom of screen (if needed) + * - hide/show "go to top" button + * + * @param event scroll event + */ onScroll(event: any) { - if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) { + if (this.moreDataAvailable && + (window.innerHeight + window.scrollY) >= document.body.offsetHeight) { this.loadSongs(); } + + if (window.scrollY > window.innerHeight) { + this.atBottom = true; + } + + if (window.scrollY == 0) { + this.atBottom = false; + } } } \ No newline at end of file