D
D
deathsoul2020-04-22 22:16:10
JavaScript
deathsoul, 2020-04-22 22:16:10

QuerySelector for multiple elements?

Hello, please tell me how to make the script work for st_img_1, st_img_2, st_img_3, st_img_4. Each one needs a slightly different style. Tried through

<script type="text/javascript">
let section = document.querySelectorAll ('.stall');

  window.onscroll = magic;

  function magic(){
    if (window.pageYOffset > 400) {
      section.style.width= '20%'
      section.style.margin= '-100px 0 0 -200px'
      section.style.transition= '1s ease-in-out 0s'
    } else {
      section.style.width= '10%'
      section.style.margin= '0'
    }
  }</script>


doesn't work (when querySelector is 1 element first it works, when all is useless). The scripts are a complete layman, but you need to do it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JP_Cool, 2020-04-22
@JP_Cool

In the section variable, you have an array of nodes that you need to iterate over in a loop, for example, using forEach. And then you will already have an instance of one specific section, and you can already execute your code with it

P
profesor08, 2020-04-23
@profesor08

const elements = [...document.querySelectorAll('.stall')];
for (const section of elements) {
  console.log(section);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question