简体   繁体   English

如何缩短此 java 脚本代码长度并添加 animation?

[英]How can i shorten this java-script code length and add animation?

Hy.海。 i am currently learning java-script to utilize in web development.我目前正在学习在 web 开发中使用的 java 脚本。 Somehow i believe this snippet o code i used is the long route and there should be a shorter way to do it.不知何故,我相信我使用的这段代码是一条很长的路,应该有更短的方法来做到这一点。 it is a drop & retractable menu(content) on click?它是点击时的下拉和可伸缩菜单(内容)? Is there any better way i could have done this rather than writing all those codes?有没有更好的方法可以做到这一点,而不是编写所有这些代码?

Also, how do i add transition to the menu另外,我如何向菜单添加过渡

? ? I wish for it to pop out and in slower.我希望它弹出并放慢速度。 Thanks in advance!提前致谢!

 function droping(){ let dropdown = document.getElementById('dropdown'); let dropped = dropdown.style.display; let cross = document.getElementById('cross1'); let plus = cross.style.display; let dash = document.getElementById('minus1'); let minus = dash.style.display; if(dropped == 'none'){ dropdown.style.display = 'block'; cross.style.display = 'none'; dash.style.display = "inline-block"; } else { dropdown.style.display = 'none'; cross.style.display = 'inline-block'; dash.style.display = "none"; } }
 button { cursor: pointer; width: 100%; text-align: left; display: flex; align-items: center; text-decoration: none; background-color: transparent; border: none; padding: 1.3rem; } button:visited { background-color: teal; border: none; }.cross i{ margin-left: auto; margin-right: 0.3rem; }.move { transition: ease-in-out 0.6s; }
 <button class="maintop cross" onclick="droping()"> What did the lion say to the king? <i class="fa fa-plus" style="display: inline-block;" id="cross1" class=""></i> <i class="fa fa-minus" style="display: none;" id="minus1" class=""></i> </button> <div id="dropdown" style="display: none;"> <p> We can have another meal of your body!</p> </div>

For showing/hiding elements, the simplest way is to make use of the build-in hidden API:对于显示/隐藏元素,最简单的方法是使用内置hidden API:

 function droping() { const dropdown = document.getElementById('dropdown'); const cross = document.getElementById('cross1'); const dash = document.getElementById('minus1'); dropdown.hidden =.dropdown;hidden. cross.hidden =;cross.hidden. dash;hidden = !dash.hidden; }
 button { cursor: pointer; width: 100%; text-align: left; display: flex; align-items: center; text-decoration: none; background-color: transparent; border: none; padding: 1.3rem; }.cross i { margin-left: 3em; } [hidden] { display: none;important; }
 <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <button class="maintop cross" onclick="droping()"> What did the lion say to the king? <i class="fa fa-plus" id="cross1"></i> <i class="fa fa-minus" id="minus1" hidden></i> </button> <div id="dropdown" hidden> <p> We can have another meal of your body!</p> </div>

Both your solution and this one depend on showing/hiding using the display property.您的解决方案和这个解决方案都依赖于使用display属性显示/隐藏。 Unfortunately display cannot be animated.不幸的是,不能动画display

If you need transitions, I suggest you either implement this based on your own CSS classes which you then add/remove instead of changing the display property, or simply use jQuery.toggle() :如果您需要转换,我建议您根据自己的 CSS 类实现此功能,然后添加/删除而不是更改display属性,或者简单地使用jQuery.toggle()

 function droping() { $('#dropdown, #cross1, #minus1').toggle('slow'); }
 button { cursor: pointer; width: 100%; text-align: left; display: flex; align-items: center; text-decoration: none; background-color: transparent; border: none; padding: 1.3rem; }.cross i { margin-left: 3em; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <button class="maintop cross" onclick="droping()"> What did the lion say to the king? <i class="fa fa-plus" id="cross1"></i> <i class="fa fa-minus" id="minus1" style="display: none"></i> </button> <div id="dropdown" style="display: none"> <p> We can have another meal of your body!</p> </div>

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

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