【问题标题】:Generate an ellipsis on text overflow在文本溢出时生成省略号
【发布时间】:2015-11-30 08:56:02
【问题描述】:

我一直在寻找一种在文本超过一定行数时添加省略号的方法。我不想使用插件,我从另一个答案中找到了纯 JS 代码。但是,省略号“...”适用于每个元素,即使它没有超过数字的限制。

HTML:

<p class="product-title">This is my product title example</p>

CSS:

.product-title {
    line-height: 1.4rem;
    height: 2.8rem;
}

!!我添加了两倍于行高的高度,以使文本超过两行溢出。如果我想要三行,我放三倍的行高。

JS:

function dotify(element) {
    var limit = element.offsetTop + element.offsetHeight;
    var dots = document.createElement('span');
    if (element['data-inner'])
        element.innerHTML = element['data-inner'];
    else
        element['data-inner'] = element.innerHTML;
    dots.appendChild(document.createTextNode('...'));
    element.style.overflow = 'hidden';
    element.appendChild(dots);
    while (dots.offsetTop + dots.offsetHeight > limit) {
        dots.previousSibling.data = dots.previousSibling.data
            .replace(/\W*\w+\W*$/, '')
    }
}


jQuery(".product-title").each(function() {
    dotify(this);
});

编辑前后的例子:

之前: 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.

之后: Lorem ipsum dolor sit amet, consectetur adipiscing elit,

sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...

【问题讨论】:

  • 您可以仅使用 css (jsfiddle.net/6s4wz2wL/1) 执行此操作,但不确定“行”,因为示例是单行。你能用“之前”和“之后”的例子来更新你的问题吗?
  • @freedomn-m 仅当您希望“之后”为一行时才有效。让我用之前和之后更新问题。
  • @freedomn-m 复制粘贴错误:P

标签: javascript jquery css


【解决方案1】:

我认为你可以在应用之前检查内容的高度

function dotify(element) {
  if (element.clientHeight >= element.scrollHeight) {
    return
  }
  var limit = element.offsetTop + element.offsetHeight;
  var dots = document.createElement('span');
  if (element['data-inner']) {
    element.innerHTML = element['data-inner'];
  } else {
    element['data-inner'] = element.innerHTML;
  }
  dots.appendChild(document.createTextNode('...'));
  element.style.overflow = 'hidden';
  element.appendChild(dots);
  while (dots.offsetTop + dots.offsetHeight > limit) {
    dots.previousSibling.data = dots.previousSibling.data.replace(/\W*\w+\W*$/, '')
  }
}


jQuery(".product-title").each(function() {
  dotify(this);
});
.product-title {
  line-height: 1.4rem;
  height: 2.9rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p id="x1" class="product-title">This is my product title example</p>
<p id="x2" class="product-title">This is my product title example This is my product title example This is my product title example This is my product title example</p>
<p id="x3" class="product-title">This is my product title example This is my product title example This is my product title example This is my product title example This is my product title example</p>
<p id="x4" class="product-title">This is my product title example This is my product title example This is my product title example This is my product title example This is my product title example This is my product title example This is my product title example This is my product title example</p>

【讨论】:

  • 实际上可行。谢谢你。我试图做它 height() 而不是 clientHeight 并且由于某种原因它不起作用。以你的方式,我没有问题。
猜你喜欢
  • 1970-01-01
  • 2018-07-22
  • 1970-01-01
  • 2013-04-01
  • 2017-01-21
  • 2019-04-29
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多