文本溢出截断省略
文本溢出截断省略是比较常见的业务场景,主要分为单行文本溢出截断省略与多行文本溢出截断省略,单行的截断方案比较简单,多行截断相对比较复杂。
单行溢出省略
单行文本溢出截断省略直接使用CSS
即可,其无兼容问题,文本溢出范围才显示省略号,否则不显示省略号,省略号位置显示刚好,但是只能作为单行文本溢出截断省略的解决方案。
1 2 3 4 5 6 7 8 9 10 11
| <section> <div class="t1">很长很长很长很长很长很长很长很长的文本</div> <div class="t1">不很长的文本</div> </section> <style type="text/css"> .t1{ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style>
|
多行溢出省略
按行计算 CSS方案
多行文本溢出截断省略按行计算使用CSS
,其文本溢出范围才显示省略号,否则不显示省略号,省略号位置显示刚好,但是兼容性一般,line-clamp
属性只有WebKit
内核的浏览器才支持,多适用于移动端页面,因为移动设备浏览器更多是基于WebKit
内核。
1 2 3 4 5 6 7 8 9 10 11 12 13
| <section> <div class="t2">不很长的文本</div> <div class="t2">很长很长很长很长很长很长很长很长的很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的文本</div> </section> <style type="text/css"> .t2{ -webkit-line-clamp: 3; display: -webkit-box; box-orient: vertical; overflow: hidden; text-overflow: ellipsis; } </style>
|
按行计算 Js方案
多行文本溢出截断省略按行计算使用Js
,其无兼容问题,文本溢出范围才显示省略号,否则不显示省略号,但是需要Js
实现,背离展示和行为相分离原则,文本为中英文混合时,省略号显示位置略有偏差。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <section> <div class="t3">不很长的文本</div> <div class="t3">很长很长很长很长很长很长很长很长的很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的文本</div> </section> <script type="text/javascript"> (function(){ var lineNum = 3; var elements = document.getElementsByClassName("t3"); Array.prototype.forEach.call(elements, element => { var text = element.innerText; var textLen = text.length; var baseWidth = window.getComputedStyle(element).width; var fontsize = window.getComputedStyle(element).fontSize; var lineWidth = baseWidth.slice(0, -2); var charCount = Math.floor(lineWidth / fontsize.slice(0, -2)); var content = ""; var totalStrNum = Math.floor(charCount * lineNum); var lastIndex = totalStrNum - textLen; if (textLen > totalStrNum) content = text.slice(0, lastIndex - 3).concat('...'); else content = text; element.innerText = content; }) })(); </script>
|
按高度计算 CSS方案
多行文本溢出截断省略按高度计算使用CSS
,利用Float
的浮动,通过::before
与::after
两个伪元素实现浮动操作,其无兼容问题,文本溢出范围才显示省略号,否则不显示省略号,但省略号显示可能不会刚刚好,有时会遮住一半文字。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <section> <div class="t4"> <div class="text">不很长的文本</div> </div> <div class="t4"> <div class="text">很长很长很长很长很长很长很长很长的很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的文本</div> </div> </section> <style type="text/css"> .t4 { max-height: 40px; line-height: 20px; overflow: hidden; } .t4::before{ float: left; content:""; width: 20px; height: 40px; } .t4 .text { float: right; width: 100%; margin-left: -20px; word-break: break-all; } .t4::after{ float:right; background-color: #fff; content:'...'; width: 20px; height: 20px; position: relative; left:100%; transform: translate(-100%,-100%); } </style>
|
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| <!DOCTYPE html> <html> <head> <title>文本溢出截断省略</title> <style type="text/css"> #container{ width: 300px; border: 1px solid #eee; position: relative; } section{ padding: 5px 0; margin: 5px; border-bottom: 1px solid #eee; } .t1{ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .t2{ -webkit-line-clamp: 3; display: -webkit-box; -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; } .t4 { max-height: 40px; line-height: 20px; overflow: hidden; } .t4::before{ float: left; content:""; width: 20px; height: 40px; } .t4 .text { float: right; width: 100%; margin-left: -20px; word-break: break-all; } .t4::after{ float:right; background-color: #fff; content:'...'; width: 20px; height: 20px; position: relative; left:100%; transform: translate(-100%,-100%); } </style> </head> <body>
<div id="container"> <section> <div class="t1">不很长的文本</div> <div class="t1">很长很长很长很长很长很长很长很长的文本</div> </section>
<section> <div class="t2">不很长的文本</div> <div class="t2">很长很长很长很长很长很长很长很长的很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的文本</div> </section>
<section> <div class="t3">不很长的文本</div> <div class="t3">很长很长很长很长很长很长很长很长的很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的文本</div> </section>
<section> <div class="t4"> <div class="text">不很长的文本</div> </div> <div class="t4"> <div class="text">很长很长很长很长很长很长很长很长的很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的文本</div> </div> </section> </div>
</body> <script type="text/javascript"> (function(){ var lineNum = 3; var elements = document.getElementsByClassName("t3"); Array.prototype.forEach.call(elements, element => { var text = element.innerText; var textLen = text.length; var baseWidth = window.getComputedStyle(element).width; var fontsize = window.getComputedStyle(element).fontSize; var lineWidth = baseWidth.slice(0, -2); var charCount = Math.floor(lineWidth / fontsize.slice(0, -2)); var content = ""; var totalStrNum = Math.floor(charCount * lineNum); var lastIndex = totalStrNum - textLen; if (textLen > totalStrNum) content = text.slice(0, lastIndex - 3).concat('...'); else content = text; element.innerText = content; }) })(); </script> </html>
|
参考
1 2 3
| https://www.jb51.net/css/718058.html https://zhuanlan.zhihu.com/p/92576904 https://www.jianshu.com/p/391702bd5b6b
|