Trim Text To A Specified Number Of Lines - jQuery moreLines

Trim Text To A Specified Number Of Lines - jQuery moreLines
File Size: 5.08 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

moreLines is a jQuery text truncation plugin which can be used to trim text  to a specified number of lines with a read more button.

More features:

  • Fade out characters at the end of the truncated text.
  • Smoothly reveal the full text as you click the more button.
  • Read Less button to collapse the expanded text.
  • Cross-browser and easy to implement.

How to use it:

1. Load the minified version of the jQuery moreLines plugin's script after loading jQuery library.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" 
        integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" 
        crossorigin="anonymous">
</script>
<script src="jquery.morelines.js"></script>

2. Wrap your long text into a container as this:

<div class="b-description_readmore js-description_readmore">
  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 Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore verita tis et quasi architecto beatae vitae dicta sunt explica. 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.
</div>

3. Call the function on the wrapping element and specify the number of lines to display.

$(function() {
  $('.js-description_readmore').moreLines({
    linecount: 3
  });
});

4. Apply a fadeOut effect to the characters at the end of the text.

.b-description_readmore_ellipsis:after {    
  content: '';
  display: inline-block;
  position: absolute;
  bottom: 0;
  right: 0;
  width: 75%;
  height: 25px;    
  background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 65%);
} 

5. Apply you own CSS styles to the Read More and Read Less buttons.

.b-description_readmore_button {
  display: inline-block;
  width: 150px;
  border: 1px solid black;
  padding: 10px 0; 
  cursor: pointer; 
  text-align: center;
}

6. More configuration options with default vaules.

$('.js-description_readmore').moreLines({

  // default CSS classes
  baseclass: 'b-description', 
  basejsclass: 'js-description',
  classspecific: '_readmore',

  // custom text
  buttontxtmore: "read more", 
  buttontxtless: "read less",

  // animation speed in milliseconds
  animationspeed: 250
  
});

This awesome jQuery plugin is developed by tsurule. For more Advanced Usages, please check the demo page or visit the official website.