How do you find the dimensions of a “display: none” element?

后端 未结 6 855
轻奢々
轻奢々 2021-02-13 18:58

I have some child elements inside a div that gets the CSS display: none applied to it and I want to find out what the child element dimensions are. How can I do thi

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-13 19:17

    I use a method that for some reasons work but, funny thing, I can't properly explain why. Basically, in my use case, I need to know the height of tabs in order to toggle them smoothly in vanilla js.

    To calculate the height, I do the following steps in javascript:

    1. Element is displayed but with "height:0px" and "overflow:hidden" (alternative to "display: none")
    2. Set the element to "height:auto"
    3. Calculate the height of the element with offsetHeight property
    4. Set back the element to "height:0px"
    5. Set the calculated height to the element inside a setTimeout, in combination with a css transition for smoother transition

    I expected the animation to flicker but it does not. It blows my mind to be able to do this since it's probably the most simple solution here, but feel free to share your ideas if you understand why it works and if you see any drawbacks doing this.

      function toggleTabs() {
        let tabs = document.querySelectorAll('.accordionX a span')
        tabs.forEach(tab => {
          tab.addEventListener('click', function(e) {
            tab.classList.toggle('active')
            if (tab.classList.contains('active')) {
              //hide other tabs
              tabs.forEach(tab => {
                if (tab != e.target) {
                  tab.classList.remove('active');
                  tab.parentElement.nextElementSibling.style.height = '0px';
                }
              })
              var tabContent = tab.parentElement.nextElementSibling;
              tabContent.style.height = 'auto';
              var tabHeight = tabContent.offsetHeight + 'px'
              tabContent.style.height = '0px';
              setTimeout(function() {tabContent.style.height = tabHeight}, 15)
            } else {
              tab.classList.remove('active');
              tab.parentElement.nextElementSibling.style.height = '0px';
            }
          })
        })
      } toggleTabs();
    .accordionX{margin:0px 20px}
    .accordionX > li{border-bottom:1px solid #e7e7e7;position:relative;list-style-type:none}
    .accordionX > li:first-child{border-top:1px solid #e7e7e7}
    .accordionX > li a::after{display:none}
    .accordionX > li p{color:#3c3c3b;line-height:1.8;text-align:left}
    .accordionX > li > a span{position:relative;color:#3c3c3b;padding-right:5%;display:block;cursor:pointer;font-weight:600;line-height:3;text-indent:15px;user-select:none;-webkit-tap-highlight-color:transparent;border:none!important}
    .accordionX > li > a span:after{width:8px;height:8px;border-right:1px solid #3c3c3b;border-bottom:1px solid #3c3c3b;position:absolute;right:18px;top:25px;content:" ";top:50%;transform:translate(0,-50%) rotate(-45deg);-webkit-transition:all .2s ease-in-out;-moz-transition:all .2s ease-in-out;transition:all .2s ease-in-out}
    .accordionX > li > a span > img {vertical-align: middle;margin-right: 10px;}
    .accordionX p{font-size:1em;padding:10px 15px 0}
    .accordionX > li > a span.active:after{transform:translate(0,-75%) rotate(45deg);-webkit-transition:all .2s ease-in-out;-moz-transition:all .2s ease-in-out;transition:all .2s ease-in-out}
    .accordionX .in-accordion{box-sizing:content-box;overflow:hidden;height:0px;transition:height .4s ease 0.1s}
    .accordionX .in-accordion li {list-style: disc;list-style-position: inside;}
    .accordionX .in-accordion p:last-child{padding-bottom:20px}
    • TAB 1

      Lorem ipsum dolor sit amet consectetur adipiscing elit placerat vestibulum at, leo torquent arcu tortor lectus gravida commodo neque elementum, semper posuere libero tincidunt velit vulputate morbi iaculis lacinia.

    • TAB 2

      Lorem ipsum dolor sit amet consectetur adipiscing elit placerat vestibulum at, leo torquent arcu tortor lectus gravida commodo neque elementum, semper posuere libero tincidunt velit vulputate morbi iaculis lacinia. Lorem ipsum dolor sit amet consectetur adipiscing elit placerat vestibulum at, leo torquent arcu tortor lectus gravida commodo neque elementum, semper posuere libero tincidunt velit vulputate morbi iaculis lacinia. Lorem ipsum dolor sit amet consectetur adipiscing elit placerat vestibulum at, leo torquent arcu tortor lectus gravida commodo neque elementum, semper posuere libero tincidunt velit vulputate morbi iaculis lacinia.

提交回复
热议问题