简体   繁体   English

多行文本溢出(dotdotdot):仅包装单词

[英]Multiline text overflow (dotdotdot): wrap only word

I have such example of my code: 我有我的代码的例子:

  <div class="container">
    <div class="item n1">Proe Schugaienz</div>
    <div class="item n2">Proe Schugaienz</div>
  </div>

and i use such jQuery code: 我使用这样的jQuery代码:

$('.item').dotdotdot({
  wrap: 'word',
  fallbackToLetter: false
})

and css: 和css:

.item {
  margin: 5px;
  background: red;
  padding: 2px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.n1 {
  width: 8px;
}

.n2 {
  width: 80px;
}

but as result i get: 但结果我得到:

在此输入图像描述

as result i want achieve this: 结果我想实现这个:

在此输入图像描述

is it possible with pure css or with dotdotdot.js? 是纯粹的CSS还是dotdotdot.js? if word (sentence) cannot match it's parent: then show only default one-line-text-overflow if word (sentence) is able to fit parent word-by-word - then match it, no letter-hyphenation! 如果单词(句子)不匹配它的父级:那么只显示默认的单行文本溢出,如果单词(句子)能够逐字适合父 - 然后匹配它,没有字母连字符!

so i don't wanna my container to be extented by height (i have a lot of data, it's only an example, i could not hardcode some blocks) 所以我不希望我的容器被高度扩展(我有很多数据,它只是一个例子,我不能硬编码一些块)

https://plnkr.co/edit/IxS0CReJicRfDdeGpoPo?p=preview https://plnkr.co/edit/IxS0CReJicRfDdeGpoPo?p=preview

you can use flex 你可以使用flex

<div class="container">
 <span></span>
 <em></em>
</div>
.container {
   display: flex;
   justify-content: center; /* centers content horizontally*/
   align-items: center /* centers content vertically*/
}

Here is a proposal that prevents the word break - there are changes in the markup as well: 这是一个阻止单词中断的提议 - 标记也有变化:

  1. I'm using flexbox container item for the div inner that has dotdotdot applied - this ensures two things: 我正在使用flexbox容器item为div inner应用了dotdotdot - 这确保了两件事:

    a. 一种。 inner takes the width of the content inner取内容的宽度

    b. word-wrap: break-word applied by dotdotdot will not break the word word-wrap: break-word dotdotdot应用的word-wrap: break-word不会破坏这个词

  2. Now it is possible to detect when word break or overflow can happen - this would be when inner width exceeds item width. 现在可以检测到单词中断溢出何时发生 - 这可能是inner宽度超过item宽度时。

    So we can detect this in the callback of dotdotdot ! 所以我们可以在dotdotdot回调中检测到这一点 When words starts breaking, you need ellipsis - so replace the text with ... just like dotdotdot does. 当单词开始破坏时,你需要省略号 - 所以用...替换文本就像dotdotdot那样。

See demo below: 见下面的演示:

 $('.inner').dotdotdot({ wrap: 'word', watch: 'window', fallbackToLetter: false, callback: function(isTruncated) { // this detects 'break-word' if($(this).outerWidth() > $(this).closest('.item').outerWidth()) { $(this).text('...'); } } }); 
 .item { margin: 5px; background: red; padding: 2px; display:flex; height: 100px; } .n1 { width: 12px; } .n2 { width: 80px; } .n3 { width: 150px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery.dotdotdot/1.7.4/jquery.dotdotdot.js"></script> <div class="container"> <div class="item n1"> <div class="inner">Proe Schugaienz.</div> </div> <div class="item n2"> <div class="inner"> Proe Schugaienz. More text here. More text here. More text here. </div> </div> <div class="item n3"> <div class="inner"> Proe Schugaienz. More text here. More text here. More text here. </div> </div> </div> 

You should set display: inline-block; 你应该设置display:inline-block; and line-height: 26px; 和行高:26px; (or any suitable value which you find suitable) to .n1 class and also increase the width to 16px (ie enough width to accomodate two letters). (或您认为合适的任何合适的值)到.n1类,并将宽度增加到16px(即足够宽度以容纳两个字母)。 So the final css should be as follows: 所以最终的css应如下:

.item {
  margin: 5px;
  background: red;
  padding: 2px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.n1 {
  line-height: 26px;
  width: 16px;
}
.n2 {
  width: 80px;
}

Also remove the lines of in script.js which u were using to achieve the same result. 同时删除你用来实现相同结果的script.js中的行。 It should work for you. 它应该适合你。

I think easiest solution for this is css "text-overflow:elipsis". 我认为最简单的解决方案是css“text-overflow:elipsis”。 You can use below css snippet for desired result. 您可以使用下面的css片段获得所需的结果。

.item.n1 {width: 20px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;}

Note that; 注意; width is your turning point for dots. 宽度是点的转折点。

To the element you want to show with a ... Apply the following code 要使用...显示的元素应用以下代码

.example-element{
max-width: example-width;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

You can adjust the width at the maximum you want. 您可以根据需要调整宽度。

Since you are already using JS, this is how I would do it without any library/plugin. 由于您已经在使用JS,因此我将在没有任何库/插件的情况下执行此操作。

 const items = document.querySelectorAll('.item') const log = document.querySelector('#log') const MIN_WIDTH = 32 // Check if the elements are smaller than the MIN_WIDTH // and apply a class to hide the text and show an ellipsis. for (let item of items) { if (item.clientWidth < MIN_WIDTH) { item.classList.add('hidden') } } 
 .item { background-color: red; text-overflow: ellipsis; overflow: hidden; margin-bottom: 0.5rem; padding: 0.2rem; } .n1 { width: 1.5rem; // 24px } .n2 { width: 6rem; } /* This will hide the text and display an ellipsis instead */ .hidden { position: relative; white-space: nowrap; } .hidden::before { content: '...'; display: block; position: absolute; top: 0; left: 0; bottom: 0; right: 0; background-color: red; text-align: center; } 
 <div class="container"> <div class="item n1">Proe Schugaienz</div> <div class="item n2">Proe Schugaienz</div> </div> 

If you plan on changing the size of the elements, you can wrap the loop inside a function, and then call it when needed. 如果您打算更改元素的大小,可以将循环包装在函数中,然后在需要时调用它。

  $.fn.overflow = function () { return this.each(function () { if ($(this)[0].scrollWidth > $(this).innerWidth()) { $(this).css('overflow', 'hidden').css('text-overflow', 'ellipsis').css('white-space', 'nowrap').css('min-width', '16px'); } }); }; $(function () { $('.item').overflow(); }); 
  .item { margin: 5px; background: red; padding: 2px; overflow: auto; word-wrap: break-word; } .n1 { width: 8px; } .n2 { width: 80px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="item n1">Proe Schugaienz</div> <div class="item n2">Proe Schugaienz</div> </div> 

I agree to the answers provided before adding text-overflow: ellipsis; 我同意在添加文本溢出之前提供的答案:省略号; to the div's does work even if content does not overflow. 即使内容没有溢出,div也能正常工作。 I believe that you want to add the ellipsis at the beginning of the content which can be achieved by "reverse ellipsis" and without the help of any js code you can achieve this kind of output Here's a plnkr that I have created for you: 我相信你想在内容的开头添加省略号,这可以通过“反向省略号”来实现,没有任何js代码的帮助你可以实现这种输出这里是我为你创建的一个plnkr:

https://plnkr.co/edit/TwotVdtGMaTi6SWaQcbO?p=preview https://plnkr.co/edit/TwotVdtGMaTi6SWaQcbO?p=preview

      .box {
    width: 250px;
    border: 1px solid silver;
    padding: 1em;
    margin: 1em 0;
  }

  .ellipsis {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .reverse-ellipsis {
    /* Your move. */
    text-overflow: clip;
    position: relative;
    background-color: #FFF;
  }

  .reverse-ellipsis:before {
    content: '\02026';
    position: absolute;
    z-index: 1;
    left: -1em;
    background-color: inherit;
    padding-left: 1em;
    margin-left: 0.5em;
  }

  .reverse-ellipsis span {
    min-width: 100%;
    position: relative;
    display: inline-block;
    float: right;
    overflow: visible;
    background-color: inherit;
    text-indent: 0.5em;
  }

  .reverse-ellipsis span:before {
    content: '';
    position: absolute;
    display: inline-block;
    width: 1em;
    height: 1em;
    background-color: inherit;
    z-index: 200;
    left: -.5em;
  }

  body {
    margin: 1em;
  }

HTML 

      <!DOCTYPE html>
  <html>

  <head>

  <link rel="stylesheet" href="style.css" />

  </head>

  <body>

  <p>The goal would be to add ellipsis on the beginning and show the end of the content. Any idea?</p>
  <div class="box  ellipsis  reverse-ellipsis"><span>Here is some long content that doesn't fit.</span></div>
  <div class="box  ellipsis  reverse-ellipsis"><span>Here is some that does fit.</span></div>
  </body>

  </html>

All you need to do is add an extra element within .reverse-ellipsis (here a span); 您需要做的就是在.reverse-ellipsis(这里是一个span)中添加一个额外的元素; I hope this might help you. 我希望这对你有帮助。

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

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