首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在单击时使用动画css?

如何在单击时使用动画css?
EN

Stack Overflow用户
提问于 2018-04-23 04:29:49
回答 1查看 763关注 0票数 0

我很难弄清楚如何让我的移动菜单图标在点击时具有动画效果。如果我可以单击菜单图标,菜单将使用animate.css fadeInDown滑入,然后再次单击时退出菜单(如果使用animate.css fadeOutUp ),那将是最理想的。有没有办法也使用JS来做到这一点?我真的很想学习。

下面是html:

代码语言:javascript
复制
<header>
<div id="pic-1">
<img id="top" src="css/Unknown.png" class="log animated fadeInDown">
<nav>
<a class="burger-nav animated fadeInDown"><i class="fas fa-bars"></i> 
</a>
<ul class="li animated fadeInDown">
<li><a>Home</a></li>
<li><a>About Us</a></li>
<li><a>Contact Us</a></li>
<li><a>Volunteer</a></li>
</ul>
</nav> 
</div>
</header>

下面是CSS (媒体查询):

代码语言:javascript
复制
/* Mobile 1 */ 
@media only screen and (min-width : 320px) {} 
.checkBox {
display: none;
}
.logo {
display: none;
}
.log {
position: fixed;
z-index: 999;
left: -2%;
height: 10%;
width: 35%;
padding-top: 2%;
padding-right: 33%;
padding-left: 33%;
border: solid white;
background-color: white;
}
.burger-nav {
position: fixed;
z-index: 999;
left: 85%;
top: 1%;
font-size: 300%;
}
header nav ul {
overflow: hidden;
height: 0; 
margin-top: 70px; 
width: 100%;
margin-left: -40px;
position: fixed;
z-index: 999;
}
header nav ul.open {
height: auto;
}
header nav ul li {
text-align: center;
width: 100%;
margin: 0;
list-style-type: none;
font-family: Jazz LET, fantasy;
font-size: 17px;
word-spacing: 7px;
letter-spacing: 5px;
font-weight: bold;
}
header nav ul li a:hover {

}
header nav ul li a {
color: #FF6103;
padding: 10px;
border-bottom: 1px solid white;
display: block;
background-color: black;
opacity: .5;
}
.li {
-webkit-animation-duration: 1s;
-webkit-animation-delay: 1s;
-webkit-animation-iteration-count: 1;
-moz-animation-duration: 1s;
-moz-animation-delay: 1s;
-moz-animation-iteration-count: 1;
-o-animation-duration: 1s;
-o-animation-delay: 1s;
-o-animation-iteration-count: 1;
animation-duration: 1s;
animation-delay: 1s;
animation-iteration-count: 1;
}
.fas {
color: purple;
}
#pic-1 {
background-size: 120% 100%;
}
#pic-2 {
background-size: 123% 100%;
}
#pic-3 {
background-size: 120% 100%;
}
.social {
display: none;
}
.circle {
display: none;
}
}

下面是我用过的一些JS:

代码语言:javascript
复制
$(document).ready(function(){ 
$(".burger-nav").on("click", function(){
$("header nav ul").toggleClass("open")
}); 
}); 

我希望有人能帮助我!谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-04-23 05:23:15

嗯,css动画有点棘手。但没有什么是解决不了的。首先,你应该使用@keyframes。一个很好的解释可以在here找到。诀窍是切换两个类(.open.close),这两个类将处理打开和关闭动画。重要的是animation-fill-mode属性,它可以使动画保持其最终状态。此外,您不能使用height: auto;,因为它需要是一个真实的值。max-height: 1000px;将会做到这一点。因此,您可以从max-height: 0;max-height: 1000px;设置动画,反之亦然。以下是作为可运行代码片段的简化代码:

代码语言:javascript
复制
$(document).ready (function() { 
  $('.burger-nav').on ('click', function () {
  
    //check if class open is already set then toggle
    if ($('header nav ul').hasClass ('open'))
      $('header nav ul').toggleClass ('close open');
    else {
      $('header nav ul').removeClass ('close');
      $('header nav ul').addClass ('open');
    }
    
  }); 
});
代码语言:javascript
复制
header nav ul {
  overflow: hidden;
  max-height: 0;       //<-- this needs to be changed
  margin-top: 70px; 
  width: 100%;
  margin-left: -40px;
  position: fixed;
  z-index: 999;
}

header nav ul li {
  text-align: center;
  width: 100%;
  margin: 0;
  list-style-type: none;
  font-family: Jazz LET, fantasy;
  font-size: 17px;
  word-spacing: 7px;
  letter-spacing: 5px;
  font-weight: bold;
}

header nav ul li a {
  color: #FF6103;
  padding: 10px;
  border-bottom: 1px solid white;
  display: block;
  background-color: black;
  opacity: .5;
}

.li {
  -webkit-animation-duration: 1s;
  -webkit-animation-delay: 1s;
  -webkit-animation-iteration-count: 1;
  -moz-animation-duration: 1s;
  -moz-animation-delay: 1s;
  -moz-animation-iteration-count: 1;
  -o-animation-duration: 1s;
  -o-animation-delay: 1s;
  -o-animation-iteration-count: 1;
  animation-duration: 1s;
  animation-delay: 1s;
  animation-iteration-count: 1;
}

header nav ul.open {
  animation-name: open;
  animation-name: open;
  animation-fill-mode: forwards;
}

@keyframes open {
    from {max-height: 0;}
    to {max-height: 1000px;}
}

header nav ul.close {
  animation-name: close;
  animation-name: close;
  animation-fill-mode: both;
}

@keyframes close {
    from {max-height: 1000px;}
    to {max-height: 0;}
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<header>
  <nav>
    <a href="javascript: void (0);" class="burger-nav animated fadeInDown">click_me</a>
    <ul class="li animated fadeInDown">
      <li><a>Home</a></li>
      <li><a>About Us</a></li>
      <li><a>Contact Us</a></li>
      <li><a>Volunteer</a></li>
    </ul>
  </nav>
</header>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49970439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档