共计 2969 个字符,预计需要花费 8 分钟才能阅读完成。
最近遇到一个貌似很简略,但写起来也不简略的问题。对于多行文字,超出 … 显示。通过 css 能够实现,但受限于浏览器兼容问题,有时候还须要依赖 JS 来实现。通过 js 实现,就须要思考到文字大小,中英文、数子、标点符号所对应的字节长度不统一,如果思考的不全面,对于不同的文字内容,总会有点差距。
首先,咱们须要理解,中文汉字,英文字母,数字以及特殊符号所占的字节长度是不一样的,如果须要计算精确,就不能依照字符串的元素个数去截取,把它们换算成字节数来截取,准确度更高。所以,咱们须要一个获取字符串字节长度的办法:
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 | |
} |
复制代码
对于要截取多少内容的字节数,咱们须要知悉能放入容器内的字节数与总字节数的比例,展现字节数 / 总字节数 = offsetWidth / scrollWidth:
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 | |
} | |
} |
复制代码
当然文字展现的多少,也是和字体大小相干的,所以咱们也须要把字体大小的因素思考到,而且作为一个工作办法,自身就不应该页面中的元素有关联,所以咱们应该在办法中本人创立元素,放入内容,计算 offsetWidth 和 scrollWidth
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 | |
} | |
} |
复制代码
最初,再裸露一个办法,不便使用者调用。为了性能思考,不创立过多 dom 元素,咱们能够缓存一下字体大小和容器宽度雷同的截取办法
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); | |
} |
最初
如果你感觉此文对你有一丁点帮忙,点个赞。或者能够退出我的开发交换群:1025263163 互相学习,咱们会有业余的技术答疑解惑
如果你感觉这篇文章对你有点用的话,麻烦请给咱们的开源我的项目点点 star: https://gitee.com/ZhongBangKe… 不胜感激!
PHP 学习手册:https://doc.crmeb.com
技术交换论坛:https://q.crmeb.com