当前位置:网站首页>判断一段文字的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
}))
边栏推荐
猜你喜欢
随机推荐
抛出一个问题? Mysql环境下进行Count操作执行的时候速度很慢_需手动给主键添加索引---MySql优化001
OSCS开源软件安全周报,一分钟了解本周开源软件安全大事
《刷题日记》2
Loop nesting and basic operations on lists
如何快速打通镜像发布流程?
electron 应用开发优秀实践
时间复杂度和空间复杂度
Arrays类、冒泡排序、选择排序、插入排序、稀疏数组!
函数二
EndNote使用指南
conditional control statement
Secretive present: in modern times
5.Set interface and implementation class
8.Properties property collection
【八大排序①】插入排序(直接插入排序、希尔排序)
[Personal study summary] CRC verification principle and implementation
3D printed this DuPont cable management artifact, and the desktop is no longer messy
[Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)
ArrayList和LinkedList
Source GBase database, oracle quote "ORA - 01000: beyond the shop open the cursor"