如何使用CSS正确淡入背景图像

时间:2013-06-10 21:00:24

标签: html css animation background fade

我如何拥有一个包含背景图片的id,只用CSS来淡入?

以下是我在css文件中的内容:

#header{
clear:both;
position:relative; 
z-index: 999; 
border-top: 3px solid #4C718F; 
border-bottom: 1px solid #C1D7E2; 
background: url(images/top-bg.png) repeat top right;
animation: fadein 3s;
-moz-animation: fadein 3s; /* Firefox */
-webkit-animation: fadein 3s; /* Safari and Chrome */
-o-animation: fadein 3s; /* Opera */

@keyframes fadein {
from {
    opacity:0;
}
to {
    opacity:3;
}
}
@-moz-keyframes fadein { /* Firefox */
from {
    opacity:0;
}
to {
    opacity:3;
}
}
@-webkit-keyframes fadein { /* Safari and Chrome */
from {
    opacity:0;
}
to {
    opacity:3;
}
}
@-o-keyframes fadein { /* Opera */
from {
    opacity:0;
}
to {
    opacity: 3;
}
}
}

背景图像根本没有褪色。我将不胜感激,谢谢。

编辑 - 这是我在HTML中调用类的地方:     

<div id="header-out">

    <div id="header">
        <div class="top-bar-wrap">
            <div id="top" class="col-full">
            <div id="logo" class="col-full">

            <?php if ($woo_options['woo_texttitle'] <> "true") : $logo = $woo_options['woo_logo']; ?>
                <a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('description'); ?>">
                    <img id="ML" src="<?php if ($logo) echo $logo; else { bloginfo('template_directory'); ?>/images/logo.png<?php } ?>" alt="<?php bloginfo('name'); ?>" />
                </a>
            <?php endif; ?>

            <?php if( is_singular() && !is_front_page() ) : ?>
                <span class="site-title"><a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a></span>
            <?php else : ?>
                <h1 class="site-title"><a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a></h1>
            <?php endif; ?>
                <span class="site-description"><?php bloginfo('description'); ?></span>

            </div><!-- /#logo -->

            <?php if ( $woo_options['woo_ad_top'] == 'true' ) { ?>
            <div id="topad">
                <?php if ($woo_options['woo_ad_top_adsense'] <> "") { echo stripslashes($woo_options['woo_ad_top_adsense']);  } else { ?>
                    <a href="<?php echo $woo_options['woo_ad_top_url']; ?>"><img src="<?php echo $woo_options['woo_ad_top_image']; ?>" width="468" height="60" alt="advert" /></a>
                <?php } ?>
            </div><!-- /#topad -->
            <?php } ?>

            <div id="navigation" class="asotv">
            <img src="wp-content/themes/biznizz/images/as-seen-on-tv.png">
                <?php
                if ( function_exists('has_nav_menu') && has_nav_menu('primary-menu') ) {
                    wp_nav_menu( array( 'depth' => 6, 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'main-nav', 'menu_class' => 'nav fl', 'theme_location' => 'primary-menu' ) );
                } else {
                ?>
                <ul id="main-nav" class="nav fl">
                    <?php
                    if ( isset($woo_options['woo_custom_nav_menu']) AND $woo_options['woo_custom_nav_menu'] == 'true' ) {
                        if ( function_exists('woo_custom_navigation_output') )
                            woo_custom_navigation_output();
                    } else { ?>
                        <?php if ( is_page() ) $highlight = "page_item"; else $highlight = "page_item current_page_item"; ?>
                        <li class="<?php echo $highlight; ?>"><a href="<?php bloginfo('url'); ?>"><?php _e('Home', 'woothemes') ?></a></li>
                        <?php
                            wp_list_pages('sort_column=menu_order&depth=6&title_li=&exclude=');
                    }
                    ?>
                </ul><!-- /#nav -->
                <?php } ?>
            </div><!-- /#navigation -->

            <div class="fix"></div>

        </div><!-- /#top -->
       </div>
        <?php //if ($woo_options['woo_slider'] == 'true' && is_home()) include ( TEMPLATEPATH . '/includes/featured.php' ); ?>
    </div><!-- /#header -->

</div><!-- /#header-out -->

3 个答案:

答案 0 :(得分:3)

我只想告诉你修改你的OPACITY 3到其他任何一个 CSS中 0.0到1.0

@keyframes fadein {
from {
    opacity:0;
}
to {
    opacity:1;
}
}
@-moz-keyframes fadein { /* Firefox */
from {
    opacity:0;
}
to {
    opacity:1;
}
}
@-webkit-keyframes fadein { /* Safari and Chrome */
from {
    opacity:0;
}
to {
    opacity:1;
}
}
@-o-keyframes fadein { /* Opera */
from {
    opacity:0;
}
to {
    opacity: 1;
}
}

答案 1 :(得分:1)

您可以查看here

这将告诉您有关背景图像的转换。

尝试background-image更改为content

答案 2 :(得分:0)

我找到了解决问题的方法。我只需按如下方式编辑该类:

#header{
clear:both;
position:relative; 
z-index: 999; 
border-top: 3px solid #4C718F; 
border-bottom: 1px solid #C1D7E2; 
background: url(images/top-bg.png) repeat top right;
animation: fadein 3s;
-moz-animation: fadein 3s; /* Firefox */
-webkit-animation: fadein 3s; /* Safari and Chrome */
-o-animation: fadein 3s; /* Opera */

@keyframes fadein {
from {
opacity:0;
}
to {
opacity:1;
}
}
@-moz-keyframes fadein { /* Firefox */
from {
opacity:0;
}
to {
opacity:1;
}
}
@-webkit-keyframes fadein { /* Safari and Chrome */
from {
opacity:0;
}
to {
opacity:1;

谢谢大家的帮助,

拉​​尔夫。