简体   繁体   English

打字稿活动元素焦点

[英]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. 我有一个类名为“ concert-landing-synopsis”的部分,并且在滚动条上,我想检查该部分是否聚焦,并将类名添加到其他元素。 I have looked through some of the solutions here the focused variable is always false 我在这里查看了一些解决方案,重点变量始终为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')返回的所有元素,如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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