展开/折叠过渡,可按一下按钮

时间:2019-09-11 14:48:24

标签: javascript html css

我正在从可折叠对象切换到另一个对象,因为后者具有更好的功能,但缺点是样式似乎更困难。

我想将新的过渡动画设置为旧的过渡动画。

在这里您可以看到两个按钮的代码:demo和代码段

var coll = document.getElementsByClassName("oldcol");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.maxHeight) {
      content.style.maxHeight = null;
    } else {
      content.style.maxHeight = content.scrollHeight + "px";
    }
  });
}
.oldcol {
  cursor: help;
  border: none;
  outline: none;
  background: none;
  padding: 0;
  font-size: 1em;
  color: green;
  border-bottom: 1px dashed;
  transition: .3s;
}

.oldcon {
  padding: 0 18px;
  margin: 3px 0;
  max-height: 0;
  overflow: hidden;
  transition: .3s ease-out;
  background-color: #f1f1f1;
  box-shadow: 5px 5px 10px 3px inset rgba(0,0,0,0.60);
}

.container {
  position: relative;
  margin: 0em;
}
.newcon {
  display: none;
  padding: 0.6em;
  margin: 0.5em;
  background: #f1f1f1;
  box-shadow: 5px 5px 10px 3px inset rgba(0,0,0,0.60);
}
.newcol {
  cursor: help;
  border: none;
  outline: none;
  background: none;
  padding: 0;
  font-size: 1em;
  color: green;
  border-bottom: 1px dashed;
  transition: .3s ease-out;
}
.newcol:focus {
  pointer-events: none;
  margin-bottom: 3em;
}
.newcol:focus + .newcon
{
  display: block;
  margin-top: -2.5em;
  position: absolute;
  left: 0;
  right: 0;
}

.fadein {
  animation: fadein 1s;
}
@keyframes fadein {
  from {opacity:0}
  to {opacity:1}
}
Old button: does <button class="oldcol">this</button> work?
<div class="oldcon"><p>Yes!<p/></div>

<hr>

<div class=container>

    New button: does <button class="newcol">this</button><span class=newcon>Quite good</span> work?<br>
    New button fadein: does <button class="newcol">this</button><span class="newcon fadein">Quite good</span> work?<br>

</div>

到目前为止我已经尝试过的

  • transition: all 3s;放入newcon CSS中(无效)
  • 将以下类添加到span(并在html中编写<span class="newcon visible hidden">)(无效)
      visibility: visible;
      opacity: 1;
      transition: opacity 2s linear;
    }
    .newcon.hidden {
      visibility: hidden;
      opacity: 0;
      transition: visibility 0s 2s, opacity 2s linear;
    }
  • 将以下类添加到span中,以使内容淡入淡出,它可以工作,但我不知道如何为淡出效果做同样的事情。我认为问题在于关闭按钮并不需要单击它,但是单击任何地方就足够了(这不是很好的行为,如何解决?)。
    .fadein {
        animation: fadein 1s;
    }
    @keyframes fadein {
        from {opacity:0}
      to {opacity:1}
    }

很容易实现

展开/折叠过渡,例如对How can I transition height: 0; to height: auto; using CSS?的第一个答案,但我不了解如何在我的情况下应用它。

1 个答案:

答案 0 :(得分:1)

这是我在评论中建议的带有line-height过渡的工作版本。

.container {
  width: 250px;
}
.col {
  cursor: help;
  border: none;
  outline: none;
  background: none;
  padding: 0;
  font-size: 1em;
  color: green;
  border-bottom: 1px dashed;
}
.con {
  display: block;
  padding: 0 18px;
  margin: 3px 0;
  overflow: hidden;
  transition: all .3s ease-out;
  line-height: 0em;
  background-color: #f1f1f1;
  box-shadow: 5px 5px 10px 3px inset rgba(0,0,0,0.60);
}
.col:focus {
  color: red;
}
.col:focus + .con
{
  line-height: 1.2em;
  padding: 8px 18px;
  margin: 10px 0;
}
<div class="container">
  This should work 
  <button class="col">right?</button>
  <span class="con">Yes absolutely, but only with text!</span>
  And it should also be responsive.
</div>