const overflowhidden = (rows: number = 1, str: string) => {
let text = document.getElementById('**');
let style = window.getComputedStyle(text, null);
let { lineHeight } = style;
let at = rows * parseInt(lineHeight, 10);
let tempstr = str;
text.innerHTML = tempstr;
let len = tempstr.length;
let i = 0;
if (text.offsetHeight <= at) {
// 如果所有文本在写入html后文本没有溢出,那不需要做溢出处理
} else {
let temp = '';
text.innerHTML = temp;
while (text.offsetHeight <= at) {
temp = tempstr.substring(0, i + 1);
i += 1;
text.innerHTML = temp;
}
let slen = temp.length;
tempstr = temp.substring(0, slen - 1);
len = tempstr.length;
temp = `${tempstr.substring(0, len - 3)}...`;
text.innerHTML = temp;
text.height = `${at}px`;
}
};
文本溢出处理
最新推荐文章于 2024-09-15 23:59:26 发布