Implemented scroll to top using stimulus.

This commit is contained in:
Jan Böhmer 2022-07-30 00:47:51 +02:00
parent 8ccf5652ab
commit 1f890efc97
3 changed files with 30 additions and 27 deletions

View file

@ -0,0 +1,27 @@
import {Controller} from "@hotwired/stimulus";
import {Tooltip} from "bootstrap";
export default class extends Controller {
connect() {
window.addEventListener('scroll', this._onscroll.bind(this));
}
_onscroll() {
const button = this.element;
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
button.style.display = "block";
} else {
button.style.display = "none";
}
}
backToTop() {
//Hide button tooltip to prevent ugly tooltip on scroll
Tooltip.getInstance(this.element)?.hide();
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
}