0

I have a part of the page that looks like this (i use pug view engine)

<div class="product">
    <div class="name">#{product.product_name}</div>
    <div class="category">#{product.category}</div>
    <div class="description">
       | #{product.product_description}
    </div>
    <div class="amount">#{product.product_amount}</div>
    <div class="count-stock">#{product.product_count_stock}</div>
    <div>
       <a href="/editProductProperties?productId=#{product.product_id}">
           <img src="images/pencil-white.svg" class="pencil-icon icon" data-product_id="#{product.product_id}"/>
       </a>
       <img src="images/trash.svg" class="trash-icon icon"/>
    </div>
</div>

And style for this part:

.product {
    display: grid;
    grid-template-columns: [name] 207px [category] 150px [description] 1fr [amount] 100px [count-stock] 100px [action] 54px;
}

.product > div:not(:last-child) {
    padding-right: 5px;
}

.product {
    padding: 20px 0;
    border-top: 1px solid #f2f2f2;
    position: relative;
}

.product::after {
    width: 0;
    height: 0;
    border-top: 100px solid red; 
    border-right: 100px solid transparent;
    position: absolute; 
}

.product .description {
    position: relative;
}

.product .description .showButton {
    position: absolute;
    top: 10px;
    right: 0;
    text-decoration: underline;
    cursor: pointer;
}

.product:last-child {
    border-bottom: 1px solid #f2f2f2;
}

.product:hover {
    background: #555454;
}

And js:

window.onresize = addShowButton;

function addShowButton() {
  productDescriptionList.forEach(el => {
    if(el.offsetHeight > 40) {
      if(el.children.length === 0) {
        let showButton = document.createElement('span');
        showButton.className = 'showButton';
        showButton.innerText = 'Показать полностью';
        el.append(showButton);
      }
      el.style["max-height"] = '40px';
      el.style.overflow = 'hidden';
    } else if(el.offsetHeight <= 40) {
      el.style["max-height"] = 'initial';
      el.style.overflow = 'visible';
      if(el.children.length > 0) {
        el.children[0].remove();
      }
    }
  });
}

addShowButton();

The problem is that el.offsetHeight takes either the old element height values or the values before all styles are applied.

That is, I look at the height value of the element in the inspector and it is 80, and offsetHeight = 36 in console and I can not understand why this is

enter image description here

enter image description here

Sean
  • 6,873
  • 4
  • 21
  • 46
  • 1
    `resize` fires several times per second, you need a [debouncer](https://stackoverflow.com/a/4298672/1169519) to execute the resize handler only once after the resizing has been finished. – Teemu Apr 17 '20 at 09:13
  • but why does it give different height data in offsetHeight and html inspector? – Святослав Марюха Apr 17 '20 at 09:18
  • I'm not sure, but if there's a ton of these descriptions, iterating all of them would take so much time, that the events will cumulate, and maybe they all are not executed. Try with the debouncer, if it helps. – Teemu Apr 17 '20 at 09:20
  • I can remove window. resize and the result with height data will be the same. It is important for me now to understand why there is such a difference in height (offsetHeight and in the element inspector) – Святослав Марюха Apr 17 '20 at 09:24
  • the problem now is not in window.resize, but in the difference between the height data that element.offsetHeight gets and the element inspector – Святослав Марюха Apr 17 '20 at 09:25

0 Answers0