Guest User

Untitled

a guest
Feb 24th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. </head>
  6. <body>
  7.  
  8.  
  9.  
  10. <h2>假设目标是是 3行 溢出则结尾 ...</h2>
  11. <h5>那么在元素中写刚好是3行的文本,审查它是多高, 比如是67px, 那么4行的文本肯定是大于68px,那么我们设置元素的max-height为68px,用于js判断</h5>
  12. <br>
  13.  
  14. <div class="box">
  15. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  16. </div>
  17.  
  18. <style>
  19. *{margin:0;padding:0;word-break:break-all;}
  20. .box{
  21. border: 1px solid red;
  22. width: 200px;
  23. max-height: 68px;
  24. overflow: hidden;
  25. }
  26.  
  27.  
  28. .ellipsis{
  29. position: relative;
  30. max-height: 67px;
  31. }
  32. .ellipsis::after{
  33. content: "...";
  34. position: absolute;
  35. width: 16px;
  36. bottom: 3px;
  37. right: 0;
  38. background: #fff;
  39. }
  40. </style>
  41.  
  42.  
  43. <script src="./jquery-1.9.1.min.js"></script>
  44. <script>
  45.  
  46. // 如果div.box高度大于等于max-height: 67px;的67说明溢出了,需要添加一个伪类模拟文本 '...'
  47. if( $('.box').height()>=68 ){
  48. $('.box').addClass('ellipsis');
  49. }
  50.  
  51. </script>
  52.  
  53.  
  54. <!--
  55. bug:
  56. 有种可能是,文本刚好是3行,但是没有溢出,这样尾部也会添加一个'...'
  57.  
  58. 改进:
  59. 同样假设是3行
  60. 那么我们去设置比3行文本高个1px的max-height
  61. 比如那么3行的高度大概是67px
  62. 那么我们设置max-height为68px,js中就判断大于等于68px,说明是4行文本,肯定超过3行,说明溢出
  63. 然后在添加的类中设置回max-height又为67px
  64.  
  65. -->
  66.  
  67. </body>
  68. </html>
Add Comment
Please, Sign In to add comment