ScrollAndFetchHandler.vue 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. position: 1,
  6. maxPosition: 1,
  7. isGettingSet: false,
  8. loadAllSongs: false,
  9. interval: null
  10. };
  11. },
  12. computed: {
  13. setsLoaded() {
  14. return this.position - 1;
  15. },
  16. maxSets() {
  17. return this.maxPosition - 1;
  18. }
  19. },
  20. mounted() {
  21. window.addEventListener("scroll", this.handleScroll);
  22. },
  23. unmounted() {
  24. clearInterval(this.interval);
  25. window.removeEventListener("scroll", this.handleScroll);
  26. },
  27. methods: {
  28. handleScroll() {
  29. const scrollPosition = document.body.clientHeight + window.scrollY;
  30. const bottomPosition = document.body.scrollHeight;
  31. if (this.loadAllSongs) return false;
  32. if (scrollPosition + 400 >= bottomPosition) this.getSet();
  33. return this.maxPosition === this.position;
  34. },
  35. loadAll() {
  36. this.loadAllSongs = true;
  37. this.interval = setInterval(() => {
  38. if (this.loadAllSongs && this.maxPosition > this.position)
  39. this.getSet();
  40. else {
  41. clearInterval(this.interval);
  42. this.loadAllSongs = false;
  43. }
  44. }, 500);
  45. }
  46. }
  47. };
  48. </script>