JSFiddle - React, Tailwind, and code Playground

by Jp9573

HTML

<div class="text-container">
  <div class="content hideContent">
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
  </div>
  <div class="show-more">
    <a href="#">Show more</a>
  </div>
</div>

CSS

div.text-container {
  margin: 0 auto;
  width: 75%;
}

.hideContent {
  overflow: hidden;
  line-height: 1em;
  height: 2em;
}

.showContent {
  line-height: 1em;
  height: auto;
}

.content {
  text-align: justify;
}

.show-more {
  text-align: center;
  display: inline;
  float: right;
}

JavaScript

var text = $('.content')
    const originalHeight = text.height();

    $(".show-more a").on("click", function() {
      var $this = $(this);
      var $content = $this.parent().prev("div.content");
      var linkText = $this.text().toUpperCase();
      var fullHeight = text[0].scrollHeight;

      if (linkText === "SHOW MORE") {
        linkText = "Show less";
        $content.addClass('showContent').removeClass('hideContent');
        text.animate({
          'height': fullHeight
        });
      } else {
        linkText = "Show more";
        $content.addClass('hideContent').removeClass('showContent');
        text.animate({
          'height': originalHeight
        });
      };

      $this.text(linkText);
    });