[英]Animate Javascript classList.toggle
I recently started to learn CSS and HTML and found this tutorial for a responsive navbar. 我最近开始学习CSS和HTML,并为响应式导航栏找到了本教程。
They show the mobile version of the navbar with the javascript function classList.toggle. 它们显示了带有javascript函数classList.toggle的导航栏的移动版本。 Is there any way to animate this and add a transition?
有没有办法对此进行动画处理并添加过渡?
function myFunction() {
document.getElementsByClassName("topnav")[0].classList.toggle("responsive");}
You can't animate the class attribute, but you can animate CSS properties. 您不能设置class属性的动画,但是可以设置CSS属性的动画。
You should have an initial CSS definition for your .topnav
class, like this one: 您应该为
.topnav
类具有一个初始CSS定义,如下所示:
.topnav {
background-color: black; /* Set an initial background */
transition: background-color 200ms; /* Tell browser to use a transition when background-color changed */
}
Thanks to this code, anytime the background-color of your .topnav
element will change, a transition will occur. 多亏了这段代码,
.topnav
您的.topnav
元素的背景颜色发生变化,就会发生过渡。
You can for example add this CSS: 例如,您可以添加以下CSS:
.topnav.responsive {
background-color: blue;
}
And this will work with your JS code. 这将与您的JS代码一起使用。
Of course, my examples are basic and will just add a transition for the background-color, but it should help you about your problem :) 当然,我的示例是基本示例,只会添加背景色的过渡,但它可以帮助您解决问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.