简体   繁体   中英

Typescript active element focus

I have a section with a class name 'concert-landing-synopsis' and on a scroll, I want to check if the section is on focus and add a class name to a different element. I have looked through some of the solutions here the focused variable is always false

let activateElement = document.getElementsByClassName("concert-landing-synopsis");

if (activateElement.length > 0) {
  window.onscroll = _ => {
    let isFocused = (document.activeElement === activateElement[0]);

    if (isFocused) {
      console.log("On focus");
    }
  };
}

The view is something like this

<section class="concert-landing-details">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</section>

You need to loop through all the elements returned by document.getElementsByClassName('concert-landing-synopsis') , as in

document.getElementsByClassName('concert-landing-synopsis').forEach((el) => {
   const isFocused = (document.activeElement === el);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM