0

I'm switching from a collapsible object to another one since the latter has better features but, as a drawback, it seems harder to style.

I'd like to style the new transition animation as the old one.

Here you can see the code of the two buttons: demo and snippet

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>

What I've tried so far

  • put transition: all 3s; in the newcon css (doesn't work)
  • added the following classes to span (and writing <span class="newcon visible hidden"> in the html)(doesn't work)
      visibility: visible;
      opacity: 1;
      transition: opacity 2s linear;
    }
    .newcon.hidden {
      visibility: hidden;
      opacity: 0;
      transition: visibility 0s 2s, opacity 2s linear;
    }
  • added the following class to span which let fade in the content, it works but I don't know how to do the same for the fade out effect. I think the problem is that to close the button it is not needed to click on it, but it is enough to click everywhere (this is not a good behaviour, how to fix it?).
    .fadein {
        animation: fadein 1s;
    }
    @keyframes fadein {
        from {opacity:0}
      to {opacity:1}
    }

There are easy implementations

of the expand/collapse transition, such as the first answer to How can I transition height: 0; to height: auto; using CSS?, but I don't uderstand how to apply it in my case.

sound wave
  • 3,191
  • 3
  • 11
  • 29
  • 1
    I see the problem, but not the motivation. What is the reason you are moving away from the working old method of setting the max-height in JavaScript and transitioning on the max-height property? – Jochem Kuijpers Sep 11 '19 at 16:13
  • I'm open to all solutions if they meet my needs, I'm new in the world of html/css/js, could you explain more about the old method? – sound wave Sep 11 '19 at 16:31
  • 1
    So in the old method, you would just set the `max-height` property to 0, meaning the element cannot be larger than having no height (i.e. being invisible). Then when you want to show it, your old method sets the `max-height` equal to the `scrollHeight` via JavaScript, which is the exact height in pixels of the inner content. That seems to be exactly what you wanted, right? So my question is: why do you want to change that? – Jochem Kuijpers Sep 11 '19 at 16:39
  • Ah you were talking about the js of my old button! Well, I explained all the reasons why I wanted to change button here https://stackoverflow.com/questions/57846345/use-details-and-summary-tags-as-collapsible-inline-elements. Are you saying that the js can be used also with the new button? – sound wave Sep 11 '19 at 17:32
  • 1
    Ah, no, you're probably right that setting `max-height` wouldn't work. Something I have used before on only-text elements is and animating the `line-height` instead of `max-height` property. This has a similar effect, is responsive, but only works on text. – Jochem Kuijpers Sep 11 '19 at 20:56
  • Ok thanks for the support anyway. – sound wave Sep 11 '19 at 21:01

1 Answers1

1

Here's a working version with the line-height transition that I proposed in the comments.

.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>
Jochem Kuijpers
  • 1,770
  • 3
  • 17
  • 34
  • Thank you very much for the help! Unfortunately i'm using these buttons to hide text+equations (svg or png generated by quicklatex plugin for wordpress). – sound wave Sep 11 '19 at 21:47