[英]How can i animate a div background color without affecting the text inside the div?
I need to animate a div
's background-color
and set opacity:0
while also keeping text inside the div
visible. 我需要为
div
的background-color
设置动画并设置opacity:0
同时还要使div
内的文本可见。
CSS: CSS:
<style type="text/css">
#block {
background-color:#FF7F7F;
display:none;
padding:2px;
}
</style>
JS: JS:
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#animate').click(function() {
$('#block').show('slow');
$('#block').animate({
opacity: 0.00
});
});
});
</script>
HTML: HTML:
<div id="block">
<p id="text">
hello there
</p>
</div>
<a href="#" id="animate">Animate</a>
The problem is that I just need the background transparent and the text to stay visible. 问题是我只需要透明的背景和保持可见的文本即可。 Like when you check for post in Facebook, the background color is animating to be transparent and the text stays visible to readers.
就像您在Facebook中检查帖子时一样,背景颜色的动画效果是透明的,并且文本仍对读者可见。
Instead of opacity, you need to change the background colour using rgba or hsla. 您需要使用rgba或hsla更改背景颜色,而不是不透明度。 Neither are supported in old IEs.
旧版IE均不支持。
Instead, you could absolutely position a div behind the text, then animate opacity of that, leaving the text in a different div on top with a transparent background. 取而代之的是,您可以将div绝对定位在文本后面,然后为其设置动画性,使文本位于顶部具有透明背景的其他div中。
I would suggest having separate "background div" positioned under the text div. 我建议在文本div下放置单独的“背景div”。 use relative positioning to do that.
使用相对定位来做到这一点。 then you have all the freedom to animate the background div as you wish keeping text intact
那么您就可以自由设置背景div的动画效果,并保持文本完整
check this out http://www.impressivewebs.com/css-opacity-that-doesnt-affect-child-elements/ 看看这个http://www.impressivewebs.com/css-opacity-that-doesnt-affect-child-elements/
<div id="wrapper">
<div id="block">
</div>
<p id="text">
hello there
</p>
</div>
<a href="#" id="animate">Animate</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.