多行内容超出...显示的JS解决方案
web前端开发
共 3673字,需浏览 8分钟
·
2021-11-19 20:54
来源 | https://juejin.cn/post/7026655351004987400
作者 | 梦想敲木鱼
function bitCompute(content) {
var total = 0,
len = arguments[0].length || 0
for (var i = 0; i < len; i++) {
if (content[i].charCodeAt() > 255) {
total += 2;
} else {
total += 1;
}
}
return total
}
function complate() {
var offsetWidth = el.offsetWidth;
var scrollWidth = el.scrollWidth;
var gap = scrollWidth - offsetWidth;
var percent = Math.floor(offsetWidth / scrollWidth * 1e3) / 1e3;
return {
gap: gap,
percent: percent
}
}
function cut(content) {
el.innerhtml = content;
var info = complate(),
percent = info.percent;
var total = bitCompute(content).total;
var showLen = +(total * percent).toFixed(0) - cfg.placeholder;
content = bitCompute(content, showLen).content;
return content + cfg.padding;
}
function bitCompute(content, maxLen) {
var total = 0,
len = arguments[0].length || 0,
outContent = '';
for (var i = 0; i < len; i++) {
if (content[i].charCodeAt() > 255) {
total += 2;
} else {
total += 1;
}
if (maxLen && total > maxLen) {
break;
}
outContent += content[i];
}
return {
total: total,
content: outContent
}
}
function cutFactory(opt) {
var cfg = {
padding: opt.padding || "...",
classList: opt.classList || [],
style: opt.style || {},
debug: opt.debug
};
cfg.placeholder = bitCompute(cfg.padding).total;
var el = doc.createElement("span");
el.className = cfg.classList.join(" ");
var customStyles = [];
for (var styleKey in cfg.style) {
if (cfg.style.hasOwnProperty(styleKey)) {
customStyles.push(styleKey + ":" + cfg.style[styleKey]);
}
}
el.style.cssText = "position:absolute;left:0;top:0;background:transparent;color:transparent;height:100%;white-space:nowrap;overflow:visible;border:0;" + (cfg.debug ? "background:white;color:red;" : "") + customStyles.join(";");
var div = doc.createElement("div");
div.appendChild(el);
div.style.cssText = "width:99%;min-height:50px;line-height:50px;position:absolute;left:3px;top:3px;overflow:hidden;outline:0;background:transparent;" + (cfg.debug ? "outline:1px solid red;background:black;" : "");
doc.body.appendChild(div);
var css = win.getComputedStyle(el);
cfg.fontSize = parseFloat(css.fontSize) || 16;
return function (content) {
el.innerhtml = content;
var out = {
flag: false,
cut: '',
all: content,
last: content
}
if (complate().gap > 0) {
out.flag = true,
out.last = out.cut = cut(content)
}
return out
}
}
function subStringEL(name, fontSize, width) {
this.subStringELFns || (this.subStringELFns = {});
var key = 'key_' + fontSize + '_' + width;
var fn = this.subStringELFns[key];
if (!fn) {
fn = this.subStringELFns[key] = cutFactory({
style: {
'font-size': fontSize,
'width': width
}
})
}
return fn(name);
}
这样就完美的解决了多行超出...显示的问题了,兼容性很好,而且也能准确截取,灵活方便。希望能帮助到受该问题困扰的同仁,内容中如有不正确之处,烦请指正,不胜感激!
另外附注源码地址:github.com/18822600748/MyJSFactory/blob/main/src/subStrByWidth.js
学习更多技能
请点击下方公众号
评论