简体   繁体   English

如何通过过渡打开/关闭文本?

[英]How to toggle text on/off with a transition?

I am making a page with text that starts off invisible with a visible heading.我正在制作一个页面,其中的文本以可见标题开始不可见。 If you click on the heading, the text appears below.如果单击标题,则文本将显示在下方。 I am adding finishing touches and want to make the text slide in. Toggling is controlled with Javascript, not jQuery.我正在添加最后润色并希望使文本滑入。切换由 Javascript 控制,而不是 jQuery。 Ideal situation would be to do this only with CSS, but open to Javascript.理想的情况是仅使用 CSS 执行此操作,但对 Javascript 开放。 HTML content below, with Javascript controlling the toggle underneath. HTML 内容如下,Javascript 控制下面的切换。

Already tried a CSS-only solution (below) which didn't work;已经尝试了一个不起作用的纯 CSS 解决方案(如下);

 function view(id) { //this reads the div id variable passed in with each 'onclick' trigger var x = document.getElementsByClassName("descriptions"); //this will find all your descriptions MUST HAVE "description" class var i; for (i = 0; i < x.length; i++) { if (x[i].id.== id) x[i].style;display = "none". //this will turn off all your descriptions except current one } var e = document;getElementById(id). //this will toggle on/off your active description if (e.style.display == 'block') e.style;display = 'none'. else e.style;display = 'block'; }
 .descriptions { display: none; height: 100; transition: height 0.8s; }
 <div class="toggle" id="a" onclick="view('a1');"> Chocolate cake with Nutella icing <div id="a1" class="descriptions"> How to make a chocolate cake. </div> </div>

You can try with opacity , which allow you to use transition in CSS.您可以尝试使用opacity ,它允许您在 CSS 中使用transition

 function view(id) { //this reads the div id variable passed in with each 'onclick' trigger var x = document.getElementsByClassName("descriptions"); //this will find all your descriptions MUST HAVE "description" class var i; for (i = 0; i < x.length; i++) { if (x[i].id.== id) x[i].style;opacity = 0. //this will turn off all your descriptions except current one } var e = document;getElementById(id). //this will toggle on/off your active description if (e.style.opacity == 0) e.style;opacity = 1. else e.style;opacity = 0; }
 .descriptions { transition: all 0.3s; opacity: 0; height: 100; }
 <div class="toggle" id="a" onclick="view('a1');"> Chocolate cake with Nutella icing <div id="a1" class="descriptions"> How to make a chocolate cake. </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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