Reputation: 14277
I have this text truncation setup here (using jquery.ellipsis) where the first paragraph is truncated if it has more than 40 words - all other paragraphs are hidden.
But how to make it smarter, ie. iterate through multiple paragraphs until the word count of 40 has been reached, then truncate the remainder of the current paragraph1, and hide all the following ones?
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();
});
.article, .article_nojs {
background: grey;
margin-bottom: 20px;
}
a {
color: blue;
text-decoration: underline;
cursor: pointer;
}
<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>
Upvotes: 2
Views: 832
Reputation: 43718
This should give you the idea. However, you need to be aware that the example is naive and will not correctly handle paragraphs that contains markup since ellipsis seems to strip it out.
I'll let you refactor the code into an appropriate plugin ;)
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();
});
<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>
Upvotes: 1