Sha*_*aka 9 html css jquery css3 css-animations
我有以下代码:
$('.div-1').on('click', function() {
$('.block').addClass('animated');
});
$('.div-2').on('click', function() {
$('.block2').addClass('animated');
});
Run Code Online (Sandbox Code Playgroud)
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%;
}
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
这是一个笔
我所试图做的事:点击时,div-1
或div-2
为部分对应的动画block
,并block2
应该从起源的底部中央的div-1
或div-2
.它目前从左角开始制作动画.
我的问题是:如何获取动画从起源底部中央的div-1
或div-2
,而不是左上角.此外,当点击div-1
或div-2
当前打开的部分应该关闭(使用它的打开方式的反向动画)并且相应的点击div
应该作为新动画打开.如何通过脚本来完成?我不想使用外部库(animate.css).我已经尝试了几种方法,但我不知道如何做到这一点,因为我无法弄清楚逻辑.
如果你想像你问的那样玩动画的起点,你可以使用transform-origin CSS属性.写
transform-origin: 50% 100%;
-webkit-transform-origin: 50% 100%;
Run Code Online (Sandbox Code Playgroud)
使它从底部中心开始.
要使用反转动画,只需在动画指令后添加关键字alternate.它将播放完成后恢复的动画.
例如:
.animated {
-webkit-animation: animateThis 0.2s ease alternate;
animation: animateThis 0.2s ease alternate;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
Run Code Online (Sandbox Code Playgroud)
使用单独的动画来隐藏块
@-webkit-keyframes shrinkThis {
0% {
height: 100%;
width: 100%;
}
100% {
height: 0;
width: 0;
}
}
Run Code Online (Sandbox Code Playgroud)
并且可以通过回调知道该块被隐藏,然后展开相应的块
$(".block").bind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd",
function() {
$('.block2').removeClass('shrink');
$('.block2').addClass('expand');
$('.block2').unbind();
});
Run Code Online (Sandbox Code Playgroud)
这是笔
编辑定位: 您可以按照某人指出的那样使用变换原点。但它仅适用于缩放、平移等变换。
该块的 css 应类似于以下内容:
.block {
/*background: red;*/
position:absolute;
border:solid 1px;
height: 100%;
width: 100%;
transform-origin: 20px 0%;
-webkit-transform-origin: 20px 0%;
}
Run Code Online (Sandbox Code Playgroud)
变换使用比例而不是修改高度和宽度:
@-webkit-keyframes animateThis {
0% {
-webkit-transform: scale(0.1,0.1);
}
100% {
-webkit-transform: scale(1,1);
}
}
Run Code Online (Sandbox Code Playgroud)
这是更新后的PEN
需要改进的地方:变换原点应该动态计算。