我在这里设置了这个文本截断设置(使用jquery.ellipsis),如果第一个段落超过40个字段则会被截断 - 所有其他段落都被隐藏。
但如何让它变得更聪明,即。迭代多个段落,直到达到40的单词计数,然后截断当前段落 1 的剩余部分,并隐藏以下所有段落?
1 visible: <the part of the word count that applies to that paragraph>
$.truncate = function (container, words) {
var first = container.find('p').first();
if (container.text().split(' ').length >= words) {
if (first.text().split(' ').length >= words) {
first.ellipsis({
visible: words,
more: '... <a href="#">read more</a>',
moreClass: 'unhide',
atFront: false
});
} else {
first.append(' <span class="unhide">...<a href="#">read more</a></span>');
}
}
container.find('.unhide').on('click', function () {
container.find('p').not(':first').show();
$(this).remove();
});
}
$('.article').each(function () {
$.truncate($(this), 40);
$(this).find('p').not(':first').hide();
});
&#13;
.article, .article_nojs {
background: grey;
margin-bottom: 20px;
}
a {
color: blue;
text-decoration: underline;
cursor: pointer;
}
&#13;
<script src="https://rawgit.com/bebraw/jquery.ellipsis/master/dist/jquery.ellipsis.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Current result</h1>
<p>40 word truncation not applied as first paragraph is truncated as is</p>
<div class="article">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
<p>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.</p>
</div>
<h1>Desired result</h1>
<p>40 word truncation across multiple paragraphs</p>
<div class="article_nojs">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam <span class="unhide">...<a href="#">read more</a></span></p>
</div>
&#13;
答案 0 :(得分:1)
这应该会给你一个想法。但是,您需要注意该示例是天真的,并且无法正确处理包含标记的段落,因为省略号似乎将其删除。
我允许您将代码重构为适当的插件;)
var maxWords = 10,
totalConsumedWordCount = 0,
$paragraphs = $('p');
$paragraphs.each(function() {
var $this = $(this),
words = $this.text().split(' '),
wordsLen = words.length,
totalWordCount = totalConsumedWordCount + words.length;
if (totalWordCount > maxWords) {
$this.ellipsis({
visible: Math.abs(totalConsumedWordCount - maxWords),
more: '... <a href="#">read more</a>',
moreClass: 'read-more',
atFront: false
});
$this.nextAll('p').hide();
return false;
}
totalConsumedWordCount = totalWordCount;
});
$(document).on('click', '.read-more', function() {
$paragraphs.show();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/bebraw/jquery.ellipsis/master/dist/jquery.ellipsis.min.js"></script>
<p>Lorem ipsum dolor sit amet, consectetur <b>adipiscing elit</b>.</p>
<p>Praesent <i>porttitor</i> orci ipsum, ut imperdiet eros eleifend eu.</p>
<p>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Morbi fermentum massa et turpis lacinia dapibus.</p>
&#13;