In the case of not displaying the text, obtain the display height that the text needs to occupy in advance, which is mostly used to preset the display content of a certain area (such as: waterfall)

// 获取文本片段高度----------------------------------------
let labelEl = document.createElement('label');
labelEl.innerText = v.label;
labelEl.style.cssText = `
    width:${itemWidth}px;                
    display: block;
    box-sizing: border-box;
    padding-top: 10px;
    line-height: 1.6;
    font-size: 14px;
    word-wrap: break-word;
    word-break: break-all;
    white-space: break-spaces;
`;//这里的样式一定要和实际显示的label保持一致,否者计算出来的高度不准确
document.body.appendChild(labelEl);
这个就是你想要的文本显示高度 = labelEl.getBoundingClientRect().height;
document.body.removeChild(labelEl);
// ----------------------------------------

Guess you like

Origin blog.csdn.net/qq_37860634/article/details/132301059