1

When I click the "read more" button the hidden content slides down instantly, when I click the button again it slides up smoothly.

How can I have the transition effects to apply on both sliding down and up actions?

function show(div) {
  var element = document.getElementById(div);
  if (element.classList.contains("hideContent")) {
    element.classList.remove("hideContent");
    element.classList.add("showContent");
  } else if (element.classList.contains("showContent")) {
    element.classList.remove("showContent");
    element.classList.add("hideContent");
  }
}
#about {
  height: 4em;
  font-size: 2em;
  text-align: center;
}

#structure {
  height: 4em;
  font-size: 2em;
  text-align: center;
  position: relative;
}

#structure-head {
  margin-left: 10%;
}

.structure {
  background-color: gray;
}

.aboutButton {
  float: right;
  color: white;
  background-color: purple;
}

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

.hideContent {
  line-height: 1em;
  max-height: 4em;
  transition: max-height 0.5s ease-out;
  overflow: hidden;
}

.showContent {
  line-height: 1em;
  max-height: 30em;
  transition: max-height 0.5s ease-in;
}

#structure-wrap {
  margin-top: 2%;
  margin-left: 5%;
  margin-right: 5%;
}
<div class="about">
  <button class="aboutButton" onclick='show("about-wrap")'>Read More</button>
  <div class="content-wrap hideContent" id="about-wrap">
    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.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>
Stickers
  • 75,527
  • 23
  • 147
  • 186

1 Answers1

1

You only need to toggle the class showContent.

function show(div) {
  var element = document.getElementById(div);
  element.classList.toggle("showContent");
}

Pen

Stickers
  • 75,527
  • 23
  • 147
  • 186
  • Interesting that this also works. Does classList.toggle have different implemention than manually adding and removing classes? In my answer it was enough to add `overflow: hidden` to the `.showContent` class without changing any of the js code. Any thoughts? – etarhan Sep 14 '18 at 20:14
  • The only difference in js is whether or not to keep the `hideContent` class, transition property works only when both initial and new states exist. – Stickers Sep 15 '18 at 15:10
  • By the way, animate height rather than max-height is recommend, and do it all within js for dynamic length, see https://stackoverflow.com/q/44467909/483779 – Stickers Sep 15 '18 at 23:28