jquery - 如何使用css3为div设置动画

标签 jquery html css css-animations

我有以下代码:

$('.div-1').on('click', function() {
  $('.block').addClass('animated');
});
$('.div-2').on('click', function() {
  $('.block2').addClass('animated');
});
html,
body {
  height: 100%;
}
.div-1 {
  display: inline-block;
  padding: 40px;
  background: tomato;
}
.div-2 {
  display: inline-block;
  padding: 40px;
  background: tan;
}
.block2 {
  background: green;
}
.animated {
  -webkit-animation: animateThis 0.2s ease;
  animation: animateThis 0.2s ease;
  -webkit-animation-fill-mode: forwards;
  animation-fill-mode: forwards;
}
.block {
  background: red;
}
@-webkit-keyframes animateThis {
  0% {
    height: 0;
    width: 0;
  }
  100% {
    height: 100%;
    width: 100%;
  }
}
keyframes animateThis {
  0% {
    height: 0;
    width: 0;
  }
  100% {
    height: 100%;
    width: 100%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div-1"></div>
<div class="div-2"></div>

<section class="block"></section>
<section class="block2"></section>

这是一个 PEN

我想做的是:当点击 div-1div-2 时,block 部分的相应动画>block2 应该起源于div-1div-2底部中心。它目前从左 Angular 开始动画。

我的问题是:如何让动画起源于 div-1div-2bottom center 而不是左 Angular 。此外,当单击 div-1div-2 时,当前打开的部分应该关闭(带有打开方式的反向动画)和相应的单击 div 应作为新动画打开。这如何通过脚本来完成?我不想使用外部库 (animate.css)。我已经尝试了几种方法来解决这个问题,但我不知道该怎么做,因为我无法理解逻辑。

最佳答案

如果你想像你所要求的那样调整动画的起点,你可以使用 transform-origin CSS 属性。 写

transform-origin: 50% 100%;
-webkit-transform-origin: 50% 100%;

让它从底部中心开始。

要使用反转动画,只需在动画指令后添加关键字alternate。完成后会播放还原的动画。

例子:

.animated {
  -webkit-animation: animateThis 0.2s ease alternate;
  animation: animateThis 0.2s ease alternate;
  -webkit-animation-fill-mode: forwards;
  animation-fill-mode: forwards;
}

关于jquery - 如何使用css3为div设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29912579/

相关文章:

html - 如何将 <ul> 水平居中?

java - Ajax post调用应该返回到html页面

javascript - typeahead.js 带有回车键的搜索框导航

jQuery 时间选择器 'Object does not support this property or method'

html - 将内容添加到带有过渡的悬停 anchor 链接

html - Flexbox - 如何在中心制作 Logo 并将其他元素包裹起来

javascript - 通过点击获取元素的id(JS)

单击父级时的 jQuery 切换复选框

javascript - Rails5 + JQuery 进度条不起作用

html - 导航栏的边框