简体   繁体   English

如何在CSS中转换jQuery淡入淡出效果?

[英]How to convert a jQuery fade effect in CSS?

I have the following code which is going to fade some images but I am interested if there is a way to handle this in CSS. 我有以下代码将淡入淡出一些图像,但是我很感兴趣是否有一种方法可以在CSS中进行处理。

$("#top").stop(true, true).delay(0).fadeTo(fadeTime * 100, 0);
$("#back").stop(true, true).delay(0).fadeTo(fadeTime * 100, 1, function () {
    if (curLoop < loops) {
        if (curImg < imgNo) {
            prevImg = curImg
            curImg++
        } else {
            prevImg = imgNo
            curImg = 1;
            curLoop++console.log("LOOP");
        }

        document.getElementById("back").style.opacity = 0;

        document.getElementById("top").style.opacity = 1;

        document.getElementById("back").src = "frames/frame_" + curImg + ".jpg";
        document.getElementById("top").src = "frames/frame_" + prevImg + ".jpg";
    } else {
        console.log("STOPPED");
        window.clearInterval(myVarSTD);
    }

    if (!initialized) {
        myVarSTD = setInterval(function () {
            startAnimation()
        }, delay * 1000);
        initialized = true;
    }
});

You can't loop through image sources in a pure CSS animation but the below fade effect is possible with CSS3 animations. 您不能在纯CSS动画中循环浏览图像源,但CSS3动画可能会具有以下淡入淡出效果。 Here the front and back images are absolutely positioned and using opacity animation they are faded in and out in an infinite loop. 在这里,前后图像是绝对定位的,并使用opacity动画以无限循环的方式淡入和淡出。 I have used 2 div with background-image but you could do the same for img element also. 我已经将2 divbackground-image但是您也可以对img元素执行相同的操作。

Refer inline comments within the snippet for more explanation of the animation's CSS code. 请参考摘要中的内联注释,以获取有关动画CSS代码的更多说明。

 .front, .back { position: absolute; /* absolute positioning puts them one on top of other */ height: 200px; width: 200px; animation: fade-in-out 10s linear infinite; /* first is animation keyframe's name, second is the duration of the animation, third is timing function and fourth is iteration count */ } .front { background-image: url(http://lorempixel.com/200/200/nature/1); } .back { background-image: url(http://lorempixel.com/200/200/nature/2); z-index: -1; /* keeps the element behind the front */ animation-delay: 5s; /* delay is equal to half the animation duration because the back has to fade-in once the front has faded-out at 50% */ } @keyframes fade-in-out { /* animation settings */ 0%, 100% { opacity: 1; /* at start and end, the image is visible */ } 50% { opacity: 0; /* at the mid point of the animation, the image is invisible */ } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script> <div class='front'></div> <div class='back'></div> 

Yes, it is. 是的。 Your code capture some elements using getElementById as back and top . 您的代码使用getElementById作为backtop捕获了一些元素。

You can use the following code to change CSS properties of those elements: 您可以使用以下代码来更改这些元素的CSS属性:

$('#back').css({'opacity':'1'});

Implemented in your code: 在您的代码中实现:

$("#top").stop(true, true).delay(0).fadeTo(fadeTime*100, 0);
            $("#back").stop(true, true).delay(0).fadeTo(fadeTime*100, 1, function(){
                if(curLoop<loops){
                    if(curImg<imgNo){
                        prevImg = curImg
                        curImg++
                    }else{
                        prevImg = imgNo
                        curImg = 1;
                        curLoop++
                        console.log("LOOP");
                    }               

    $('#back').css({'opacity':'0'});

    $('#top').css({'opacity':'1'});

                    document.getElementById("back").src = "frames/frame_"+curImg+".jpg";
                    document.getElementById("top").src = "frames/frame_"+prevImg+".jpg";            
                }else{
                    console.log("STOPPED");
                    window.clearInterval(myVarSTD);
                }

                if(!initialized){           
                    myVarSTD = setInterval(function(){startAnimation()},delay*1000);
                    initialized = true;
                }
            });

jQuery Transit is an awesome plugin which mimics jQuery's animation functions but in CSS. jQuery Transit是一个很棒的插件,它模仿jQuery的动画功能,但使用CSS。 You get a much higher framerate using CSS too! 您也可以使用CSS获得更高的帧率!

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

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