简体   繁体   English

如何使用CSS3设置ul背景图像fadeIn?

[英]How can I set ul background image fadeIn with CSS3?

I have an Unordered List in my page. 我的页面中有一个无序列表。 Each li elements displayed with CSS3 rotate animation. CSS3中显示的每个li元素都会旋转动画。 I want to set background image with FadeIn effect just after li animation finished. 我想在li动画完成后立即使用FadeIn效果设置背景图像。

I write "addbg" Class for ul like this: 我为ul这样写“ addbg”类:

ul.addbg{
    background: transparent url(../img/circle-line.png) no-repeat 49% 94px;
    -webkit-transition:background 5.6s;  
    -moz-transition:background 5.6s;  
    -o-transition:background 5.6s;  
    transition:background 5.6s; 

}

but nothing happen! 但是什么也没发生!

How can I do that? 我怎样才能做到这一点?

My ul is like this: 我的ul是这样的:

             <ul class="addbg">
                <li>
                    <span>Text1</span>

                </li>
                <li>
                    <span>Text2</span>

                </li>
                <li>
                    <span>Text3</span>

                </li>
                <li>
                    <span>Text4</span>

                </li>
            </ul>

You can't transition background-image property, however you can set the background-image to its :after :pseudo-element and transition its opacity . 您不能转换background-image属性,但是可以将background-image设置为其:after :pseudo-element并transitionopacity

Here's a quick example. 这是一个简单的例子。

 #image { position: relative; width: 300px; height: 100px; text-align: center; line-height: 100px; } #image:after { content: ''; position: absolute; left: 0; top: 0; width: 100%; height: 100%; background: url(http://www.lorempixel.com/300/100); opacity: 0; transition: opacity 1s; z-index: -1; } #image:hover:after { opacity: 1; } 
 <div id="image">Some Content</div> 


If you want to add a delay instead of :hover , you could define @keyframes and add delay ( 3s in the example below) to the animation and set the opacity of the #image:after to 1 after the animation has ended using JavaScript. 如果要添加延迟而不是:hover ,则可以定义@keyframes并在动画中添加delay (下例中为3s ),并在动画使用JavaScript结束后将#image:afteropacity设置为1

animation: fadeIn 1s 1 3s - fadeIn is the animation-name , 1s is the animation-duration , 1 is the animation-iteration and 3s is the animation-delay . animation: fadeIn 1s 1 3s - fadeInanimation-name1sanimation-duration1animation-iteration3sanimation-delay

 var img = document.getElementById('image'); var event = ['webkitAnimationEnd', 'animationend']; for (i = 0; i < event.length; i++) { img.addEventListener(event[i], function() { var ss = document.styleSheets; for (j = 0; j < ss.length; j++) { var rules = ss[j]; for (k = 0; k < rules.cssRules.length; k++) { var r = rules.cssRules[k]; if (r.selectorText == "#image::after" || r.selectorText == "#image:after") { r.style.opacity = 1; } } } }); } 
 #image { position: relative; width: 300px; height: 100px; text-align: center; line-height: 100px; } #image:after { content: ''; position: absolute; left: 0; top: 0; width: 100%; height: 100%; background: url(http://www.lorempixel.com/300/100); -webkit-animation: fadeIn 1s 1 3s; animation: fadeIn 1s 1 3s; z-index: -1; opacity: 0; } @-webkit-keyframes fadeIn { 0% { opacity: 0; } 100% { opacity: 1; } } @keyframes fadeIn { 0% { opacity: 0; } 100% { opacity: 1; } } 
 <div id="image">Some Content</div> 


Alternatively, you could do a pure JavaScript animation using setTimeout() for the delay and setInterval() for the animation without using transition or @keyframes . 另外,您可以使用setTimeout()作为延迟,使用setInterval()作为动画,而不使用transition@keyframes来制作纯JavaScript动画。

 var ss = document.styleSheets; for (i = 0; i < ss.length; i++) { var rules = ss[i]; for (j = 0; j < rules.cssRules.length; j++) { var r = rules.cssRules[j]; if (r.selectorText == "#image::after" || r.selectorText == "#image:after") { var op = 0; setTimeout(function() { var fadeIn = setInterval(function() { r.style.opacity = op; op += 0.005; if (op > 1) { clearTimeout(fadeIn); } }, 7) }, 3000) } } } 
 #image { position: relative; width: 300px; height: 100px; text-align: center; line-height: 100px; } #image:after { content: ''; position: absolute; left: 0; top: 0; width: 100%; height: 100%; background: url(http://www.lorempixel.com/300/100); z-index: -1; opacity: 0; } 
 <div id="image">Some Content</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM