getTextWidth(str, fontSize) {
let result = 0;
let ele = document.createElement("div");
ele.style.position = "absolute";
ele.style.whiteSpace = "nowrap";
ele.style.fontSize = fontSize;
ele.style.opacity = 0;
ele.innerText = str;
document.body.append(ele);
result = ele.getBoundingClientRect().width;
document.body.removeChild(ele);
return result;
}
【js】计算一行字符串的像素长度
最新推荐文章于 2025-04-21 18:21:46 发布