Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

javascript - Toggling class doesn't trigger transition


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not only the comas in the HTML need to be fixed.

You need to specify the property on which the transition wil apply... If not sure, use all.

For the transition to take effect, the height has to be defined in the #expSect1 rule... Because the transition need a numeric value to start with. auto does not work.

Additionally, an id is more specific than a class... So you need to use both the class and the id on the rule to be toggled.

See the below working snippet.

function expandFunction(elementID) {
  let element = document.getElementById(elementID);
  element.classList.toggle("setHeightStyle");
}
#expSect1.setHeightStyle {
  height: 60px;
}

#expSect1 {
  overflow: hidden;
  cursor: pointer;
  height: 200px;
  transition: all 1s;
  background: blue;
}
<div class="setHeightStyle" id="expSect1" onclick="expandFunction(id)">
  <h1>Title</h1>
  <br>
  <p>Content!<br>More content!</p>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...