G
G
grabbee2020-09-13 14:17:46
Vue.js
grabbee, 2020-09-13 14:17:46

How to request data only if the component is visible on the screen?

Page in the form of a tape of photos and likes to them. There are 2 photos on the screen, and there are more than 20 on the page. Information for each like is loaded separately. Perhaps there are ready-made components that monitor whether the component is visible on the screen. I want to request data from the server only when the user scrolls the page to each photo.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-09-13
@grabbee

mounted() {
  const observer = new IntersectionObserver(entries => {
    if (entries[0].isIntersecting) {
      stopObservation();
      // здесь запрашивайте свои данные
    }
  }, {
    threshold: 1,
  });
  observer.observe(this.$el);

  const stopObservation = () => observer.disconnect();
  this.$on('hook:beforeDestroy', stopObservation);
},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question