A
A
account-62017-04-13 04:03:04
JavaScript
account-6, 2017-04-13 04:03:04

How to make a universal selector?

There is code like this (Vue method):

handleScroll() {
      let obj = document.querySelector('h2');
      let {top,bottom} = obj.getBoundingClientRect();
      let height = document.documentElement.clientHeight;
      this.scrolled = top < height && bottom >0;
}

The problem is that if I want such elements, say, 5 on the page, then I have to repeat the same thing again, changing only one selector (element, tag, id).
How do I refactor this so that this code applies to whatever elements I already select thanks to Vue?
We need something that makes sense
let obj = document.querySelector('любой элемент которому задам этот метод');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kulakov, 2017-04-13
@account-6

As an option, create a separate vue component.
Or just animate all elements that satisfy the condition:

handleScroll() {
      let objList = document.querySelectorAll('h2');
      objList.forEach(function(el) {
        let {top,bottom} = el.getBoundingClientRect();
        let height = document.documentElement.clientHeight;
        if(top < height && bottom > 0) {
          el.classList.add('bounceInLeft');
        } else {
          el.classList.remove('bounceInLeft');
        }
      });
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question