简体   繁体   English

逐行截断段落而不是字符

[英]Truncate paragraphs by line not characters

I have multiple items in a div with paragraphs and I would like to truncate them to 2 lines. 我在一个带有段落的div中有多个项目,我想将它们截断为2行。 I have tried to truncate using the height but it results in cut off words. 我试图使用高度截断,但结果是单词断断续续。 I can't use characters because in some cases the words are long and get pushed to a new line. 我不能使用字符,因为在某些情况下,单词很长并且会被推到新的一行。

I am trying to work with getClientRects() as you'll see in the fiddle below. 我正在尝试使用getClientRects(),如您在下面的小提琴中所见。 Also note that I can't use any plugins for the project I am working on. 还要注意,我不能为正在处理的项目使用任何插件。

I found this example on another post: Working Truncate from stackoverflow post 我在另一篇文章中找到了这个示例:从stackoverflow文章中 截断工作

My Fiddle: JS Fiddle 我的小提琴: JS小提琴

  var lines = $(".truncate")[0].getClientRects();

  var divHeight = 0;

  for (var i=0, max = 2; i < max; i++)
        divHeight += lines[i].bottom - lines[i].top;

  divHeight += i;
  $(".truncate").height(divHeight);

There's a number of issues. 有很多问题。

  • The code you're trying to work from takes advantage of a quirk related to display: inline but you don't set display: inline , instead leaving .truncate at the browser default of display: block . 您尝试使用的代码利用了与display: inline相关的怪癖,但未设置display: inline ,而是将.truncate为浏览器默认的display: block
  • ready isn't a real event and jQuery no longer fakes it when using .on('ready', ...) so your code never runs. ready不是一个真正的事件,使用.on('ready', ...)时jQuery不再伪造它.on('ready', ...)因此您的代码永远不会运行。
  • jQuery's .height() requires that the argument be in the form of a CSS height value. jQuery的.height()要求参数采用CSS高度值的形式。 This means you need to use something that results in, for example, '50px' rather than just 50 . 这意味着您需要使用会导致'50px'而不是50
  • height is ignored on inline elements so it'll have to be set on the outer element. 内联元素上的height被忽略,因此必须在外部元素上进行设置。 The code you were working from did this but you didn't follow it. 您正在使用的代码做到了这一点,但是您没有遵循它。
  • Your code assumes that the number of lines will always be two or more. 您的代码假定行数将始终为两个或更多。
  • overflow: hidden isn't set so the text itself will push outside its container even if the container was shortened. overflow: hidden未设置“ overflow: hidden因此即使缩短了容器,文本本身也将被推入容器之外。

All together, your code should look something like this instead: 总之,您的代码应该看起来像这样:

.item {
  width: 400px;
  margin: 20px;
  display: inline-block;
  overflow: hidden;
  box-sizing: content-box;
}
.truncate {
  display:inline;
}
$(document).ready( function(){
    var lines = $(".truncate")[0].getClientRects();
    var divHeight = 0;
    var max = lines.length >= 2 ? 2 : lines.length;

    for (var i=0; i < max; i++) {
        divHeight += lines[i].bottom - lines[i].top;
    }

    divHeight += i;

    $(".item").height(divHeight + 'px');
});

JSFiddle 的jsfiddle

Using the css answer from css-tricks ( https://css-tricks.com/line-clampin/ ) assuming you know the line-height. 假设您知道行高, 使用css-tricks( https://css-tricks.com/line-clampin/ )中的css答案。

 .item { width: 400px; margin: 20px; overflow: hidden; } .fade { position: relative; height: 2.4em; /* exactly two lines */ } 
  <div class="item fade"> Lorem ipsum dolor sit amet, consectetur adipiscing 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. </div> <div class="item fade"> Lorem ipsum dolor sit amet, consectetur adipiscing 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. </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM