scroll_up.js 848 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. export class scroll_up {
  2. #button;
  3. constructor(button) {
  4. this.#button = button;
  5. this.#update();
  6. document.addEventListener("scroll", () => {
  7. this.#update()
  8. });
  9. this.#button.addEventListener("click", () => {
  10. this.scroll();
  11. });
  12. }
  13. scroll() {
  14. this.#position = 0;
  15. }
  16. get #position() {
  17. return document.scrollingElement.scrollTop;
  18. }
  19. set #position(target) {
  20. document.scrollingElement.scrollTop = target;
  21. }
  22. get #visible() {
  23. return Number(this.#button.style.opacity) === 1;
  24. }
  25. set #visible(target) {
  26. this.#button.style.opacity = (target) ? "1" : "0";
  27. }
  28. get #margin() {
  29. return 20;
  30. }
  31. #update() {
  32. this.#visible = (this.#position > this.#margin);
  33. }
  34. }