user9553863
user9553863

Reputation:

Apply transition effects to sliding content

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>

Upvotes: 1

Views: 71

Answers (1)

Stickers
Stickers

Reputation: 78686

You only need to toggle the class showContent.

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

Pen

Upvotes: 1

Related Questions