CSS3在子元素中添加/移除类时的过渡动画
CSS3 transition animation when adding/removing class in child elements
我有一个问题,当添加类到父类时,CSS3过渡不能在子元素上工作。
下面是我的代码:http://jsfiddle.net/4zwg3/327/图像没有动画,并立即变为50px高度
CSS:.header {
height: 400px;
width: 400px;
background: blue;
}
.small_header img {
height: 50px;
background-size: auto 100%;
-webkit-transition: all 1.7s ease;
transition: all 1.7s ease;
}
.small_header {
height: 100px;
background-size: auto 100%;
-webkit-transition: all 1.7s ease;
transition: all 1.7s ease;
}
HTML: <div class="header">
<img src="http://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg">
</div>
Javascript: var click = 1;
$( ".header" ).on( "click", function() {
console.log('works');
if (click == 1) {
$(".header").addClass("small_header");
$(".small_header").removeClass("big_header");
click = 0;
} else {
$(".header").addClass("big_header");
$(".small_header").removeClass("small_header");
click = 1;
}
});
但是你可以看到image.
没有过渡动画。如何修复?
这个问题是因为图像没有任何开始高度,浏览器不能计算过渡,如果你在small_header类上添加过渡,过渡只在图像缩小时起作用。
$( ".header" ).on( "click", function() {
$(".header").toggleClass("small_header");
});
.header {
height: 400px;
width: 400px;
background: blue;
}
.header img {
height:200px;
-webkit-transition: all 1.7s ease;
transition: all 1.7s ease;
}
.small_header img {
height: 50px;
background-size: auto 100%;
}
.small_header {
height: 100px;
background-size: auto 100%;
-webkit-transition: all 1.7s ease;
transition: all 1.7s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="header">
<img src="http://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg">
</div>
首先,我会避免处理这两个类和"click"变量。
你的JS应该看起来更像:
$( ".header" ).on( "click", function() {
console.log('works');
$(".header").toggleClass("small");
});
你的CSS应该是这样的:
.header {
height: 400px;
width: 400px;
background: blue;
}
.small {
height: 100px;
-webkit-transition: all 1.7s ease;
transition: all 1.7s ease;
}
.small img {
height: 50%;
-webkit-transition: all 1.7s ease;
transition: all 1.7s ease;
}
试试:-
#someDiv{
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
它不能是动画,因为CSS不知道你的图像的起始大小。你应该为计算动画添加图像大小:
.header img {
height: 400px;
}
相关文章:
- 我需要在javascript if语句中使用jquery来动画元素的颜色
- 添加/删除类和动画元素的不同方法
- 悬停时动画元素的 JavaScript 问题
- 动画元素不会在悬停时更改样式
- 在jquery中使用索引动画元素
- 应该为“容器”元素和“动画”元素分配什么位置属性?HTML CSS JavaScript.
- 动画元素角度不消失
- JS获取动画元素的当前位置
- jQuery幻灯片和停止问题:动画元素随着鼠标的快速移动而冻结
- 在视图中检测动画元素
- 鼠标悬停和鼠标移出不触发动画元素
- jQuery动画元素和隐藏
- 当你向下滚动然后向上滚动时如何动画元素
- 当动画元素接触鼠标时触发mouseenter
- 使用滑动回调和Jquery动画元素在幻灯片
- 我如何动画元素存储在一个数组?除了我徘徊在上面的那个
- 引导工具提示跟随动画元素
- 点击动画元素
- 使用jQuery动画元素并调用stop()
- 如何显示动画元素时,它是在查看区域