简体   繁体   English

隐藏溢出y时如何找到隐藏的第一个元素?

[英]How to find the first element hidden when overflow-y is hidden?

I have a container div and multiple child elements. 我有一个容器div和多个子元素。 And have set overflow-y hidden for the container. 并为容器设置了溢出y。 Now I would like to find the first element which is hidden through vanilla javascript 现在,我想找到通过香草javascript隐藏的第一个元素

I have tried getting computed property of the child element to see whether they have display -> none. 我尝试获取子元素的计算属性,以查看它们是否具有显示->无。 all of them are block. 他们都是被封锁的。


calView() {
    const wrapper = document.querySelector(".chunks-bar-wrapper");
    const fourthElement = wrapper.querySelector('div[data-id="4"]');
    console.log(fourthElement);
    console.log(window.getComputedStyle(fourthElement));
  }
  componentDidMount() {
    window.addEventListener("resize", this.calView);
  }
  componentWillMount() {
    window.removeEventListener("resize", this.calView);
  }
  render() {
    const { labelList } = this.props;
    return (
      <div className="wrapper">
        {labelList.map((label, index) => (
          <div className="tab" key={index} data-id={index}>
            {label}
          </div>
        ))}
      </div>
    );
  }

You should call getBoundingClientRect on your element, you can also use refs to get control of your rects: 您应该在元素上调用getBoundingClientRect,也可以使用refs来控制自己的rect:

Quick fix: 快速解决:

calView() {
    const wrapper = document.querySelector(".chunks-bar-wrapper");
    const fourthElement = wrapper.querySelector('div[data-id="4"]');
    console.log(fourthElement);
    console.log(fourthElement.getBoundingClientRect());
}

Using refs: 使用参考:

constructor() {
    ...
    this.elRefs = {};
    ...
}

calView() {
    const fourthElement = this.refs[4];
    console.log(fourthElement);
    console.log(fourthElement.getBoundingClientRect());
}

render() {
    const { labelList } = this.props;
    return (
      <div className="wrapper">
        {labelList.map((label, index) => (
          <div key={index} ref={(el) => { this.refs[index] = el; }} className="tab">
            {label}
          </div>
        ))}
      </div>
    );
}

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

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