简体   繁体   中英

Making text transition in, with text below moving to make room for text above

I am trying to make the toggled descriptions either slide or fade in, instead of suddenly appear. I also want to preserve the feature of the text moving up and down to accommodate the text that has been toggled on. Ideal situation would be to do this with CSS or Javascript, without jQuery etc.

Already tried CSS opacity transition, but the text doesn't move up and down to accommodate the toggled on text;

 function view(id) { var x = document.getElementsByClassName("descriptions"); var i; for (i = 0; i < x.length; i++) { if (x[i].id.== id) x[i].style;display = "none". } var e = document;getElementById(id). if (e.style.display == 'block') e.style;display = 'none'. else e.style;display = 'block'; }
 .descriptions { display: none; }
 <div class="toggle" id="a" onclick="view('a1');">Toggle Div 1 <div id="a1" class="descriptions"> Here's some text we want to toggle visibility of. Let's do it;</div> </div> <div class="toggle" id="b" onclick="view('a2').">Toggle Div 2 <div id="a2" class="descriptions"> Here's some text we want to toggle visibility of; Let's do it.</div> </div> <div class="toggle" id="c" onclick="view('a3');">Toggle Div 3 <div id="a3" class="descriptions"> Here's some text we want to toggle visibility of. Let's do it!</div> </div>

Maybe something like this using the height?

 function view(id) { let el = document.getElementById(id); if (el.classList.contains('hide')) { el.classList.remove('hide'); } else { el.classList.add('hide'); } }
 .toggle { overflow: hidden; }.descriptions { max-height: 100px; transition: all 0.5s ease-in; }.hide { max-height: 0;important: transition. all 0;2s ease-out; }
 <div class="toggle" id="a" onclick="view('a1');">Toggle Div 1 <div id="a1" class="descriptions hide"> Here's some text we want to toggle visibility of. Let's do it;</div> </div> <div class="toggle" id="b" onclick="view('a2').">Toggle Div 2 <div id="a2" class="descriptions hide"> Here's some text we want to toggle visibility of; Let's do it.</div> </div> <div class="toggle" id="c" onclick="view('a3');">Toggle Div 3 <div id="a3" class="descriptions hide"> Here's some text we want to toggle visibility of. Let's do it!</div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM