简体   繁体   English

如何在不影响div内文本的情况下为div背景颜色设置动画?

[英]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. 我需要为divbackground-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.

相关问题 如何设置div背景的不透明度? - How can I animate the opacity of the background of a div? 替换 div 内的文本而不影响其中的任何 HTML 标签 - Replace text inside a div without affecting any HTML tags inside of it CSS,JavaScript,HTML,如何将div中的文本与图像旁边的中间对齐? 并为文本设置动画 - CSS, JavaScript, HTML, how can i align a text inside a div to middle beside an image? and animate the text 隐藏元素时如何为div的背景设置动画? - How can I animate the background of a div when an element is hidden? 如何悬停div并使其背景图像慢慢消失而又不影响其内部内容? - How to hover a div and make its background-image slowly dissapear without affecting the content that's inside it? 根据div中的文本更改div背景颜色 - Change div background color based on text inside div 如何在不使用.animate()的情况下平稳地移动div? - How can I move a div smoothly, without using .animate()? 如何替换div ID中的div类文本? - how can I replace div class text inside div id? 如何在不影响子元素的情况下更改div中的文本 - How to change text in div without affecting child elements 在不影响 div 大小的情况下,将文本完美地放入 div(高度和宽度)内 - Fit text perfectly inside a div (height and width) without affecting the size of the div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM