First of all, there are several ways to do this. For example:
- Showing partial content based on height. You set the height of the element and the "Show more" button or link simply leaves the height complete.
- Use a script to "cut" the text and save the original, which will be displayed again when clicking the button or link.
- Render the original hidden text and the cut text visible and simply change the elements by clicking on the link.
- Render the page with the cut text and load the full content via Ajax.
Each approach has advantages and disadvantages, having different levels of implementation complexity.
The approach may vary depending on the content of the publications, that is, if there are HTML tags or some dynamic content.
Assuming you want to avoid new server access, that a little more text increasing the page size is no problem for you and that you really want to limit by number of characters, the second or third options are more reasonable.
Example
Starting from the second approach, I made a very simple script that cuts the texts of div
s at page startup and automatically adds a link "Read more".
Cut in text makes a regular expression search to count the amount of spaces until you find the umpteenth word
var wordLimit = 50;
$(function() {
//trata o conteúdo na inicialização da página
$('.show-summary').each(function() {
var post = $(this);
var text = post.text();
//encontra palavra limite
var re = /[\s]+/gm, results = null, count = 0;
while ((results = re.exec(text)) !== null && ++count < wordLimit) { }
//resume o texto e coloca o link
if (results !== null && count >= wordLimit) {
var summary = text.substring(0, re.lastIndex - results[0].length);
post.text(summary + '...');
post.data('original-text', text);
post.append('<br/><a href="#" class="read-more">Leia mais</a>');
}
});
//ao clicar num link "Leia mais", mostra o conteúdo original
$('.read-more').on('click', function() {
var post = $(this).closest('.show-summary');
var text = post.data('original-text');
post.text(text);
});
});
.show-summary {
width: 300px;
background: #eee;
margin: 5px;
float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="show-summary">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="show-summary">Sed ut perspiciatis unde omnis iste natus error sit voluptatem</div>
<div class="show-summary">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
<div class="show-summary">Sed ut perspiciatis unde omnis iste natus error sit</div>
<div class="show-summary">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium</div>
Man, you really helped me, thank you!
– pxntxs.
@pxntxs. has other ways, I tried to show with pure CSS for you to take the basic ideas, and adapt to your real case. The important thing is you understand how it works.
– Bacco
I get it. Thank you very much!
– pxntxs.