1、文本末尾显示省略号
实现原理:先获取文本块的 offestWidth ,再创建一个节点,遍历文本块将文字添加到节点上,将节点的offestWidth和文本块的offestWidth比较。 当前节点的offestWidth 大于 文本块的offestWidth时,认为追加到节点的内容为一行可容纳的最大数量
注:文本内容在dom上占有空间,且不能设置属性为`visibility:hidden`,`display:none`
2、requestAnimationFrame实现定时器
大部分屏幕的刷新率为60Hz,即1s刷新60次,requestAnimationFrame函数在1秒内可以执行60次,每16.7ms执行一次。使用requestAnimationFrame的时候,只需反复调用它即可
例
/*+---- 以下函数执行完刚好需要1s ----+*/
let progress = 0;
function render() {
progress += 1;
if(progress < 60) window.requestAnimationFrame(render); // 反复调用
}
// 第一帧渲染
window.requestAnimationFrame(render);
