当前位置:网站首页>判断一段文字的width
判断一段文字的width
2022-08-09 10:03:00 【高桥靓仔】
判断一段文字的width
将一段文字字符串传入函数动态创建一个div元素,然后返回获取div的宽度即可:(思想就是和图片上传预判断图片的宽高一致)
export const getTextWidth = ({
str,
overWidth,
document,
}: {
str: string;//需要判断的一段字符串
overWidth: number;//需要判断的宽度阈值(单位px)
document: any;
}) => {
var width = 0;
var html = document.createElement('span');
html.innerText = str;
html.className = 'getTextWidth';
document.querySelector('body').appendChild(html);
width = document.querySelector('.getTextWidth').offsetWidth;
document.querySelector('.getTextWidth').remove();
return width >= overWidth;
};
console.log(getTextWidth({
str:'哈哈哈,春意盎然!',
overWidth:200,
document
}))
边栏推荐
猜你喜欢
随机推荐
Technology Sharing | How to simulate real usage scenarios?mock technology to help you
在anaconda环境中配置cuda和cudnn
KeyBERT和labse提取字符串中的关键词
拿下跨界C1轮投资,本土Tier 1高阶智能驾驶系统迅速“出圈”
LeetCode179:最大数(C语言)代码简洁!
【ASM】字节码操作 MethodVisitor 案例实战 生成对象
EndNote使用指南
【Linux】宝塔面板设置MySQL慢查询日志,未走索引日志
1: bubble sort
排序1:冒泡排序
自定义类型:结构体,枚举,联合
MySQL备份与恢复
Go-指针的那些事
通过程序发送 Gmail 邮件
喜迎排名18
【八大排序①】插入排序(直接插入排序、希尔排序)
搭建Tigase进行二次开发
Technology Sharing | Sending Requests Using cURL
Collections and Functions
MySQL执行过程及执行顺序