"Read more text..." functionality

Simple jQuery snippet to have a "see more" text functionality

by Olivia Ng

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>
<div>
  <!-- data-miniheight is the height of the container you'd want it to appear as. when 'see more' is clicked, the entire div expands to show ALL text. -->
  <p class="text-truncate" data-miniheight="150">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
    survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
    software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took
    a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets
    containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
    standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing...

CSS

.text-truncate {
  display: block;
  overflow: hidden;
}

.text-expand,
.text-contract {
  margin-top: 10px;
  color: #136a8a;
  cursor: pointer;
  font-size: 13px;
}

.text-expand:hover,
.text-contract:hover {
  color: #23527c;
}

.hide {
  display: none;
}

JavaScript

$(function() {
  var animspeed = 550; // animation speed in milliseconds

  var $longtext = $('.text-truncate');
  var height = $longtext.height();
  var mini = $longtext.data('miniheight');

  if (height > mini) {
    $('.text-expand').removeClass('hide');
    $longtext.attr('data-fullheight', height + 'px');
    $longtext.attr('data-miniheight', mini + 'px');
    $longtext.css('height', mini + 'px');
  } else {
    $longtext.css('height', height + 'px');
  }

  $('.text-expand').on('click', function(e) {
    $text = $(this).prev();

    $text.animate({
      'height': $text.attr('data-fullheight')
    }, animspeed);
    $(this).next('.text-contract').removeClass('hide');
    $(this).addClass('hide');
  });

  $('.text-contract').on('click', function(e) {
    $text = $(this).prev().prev();

    $text.animate({
      'height': $text.attr('data-miniheight')
    }, animspeed);
    $(this).prev('.text-expand').removeClass('hide');
    $(this).addClass('hide');
  });
});