CSS3 transitions inside jQuery .css()

后端 未结 2 510
既然无缘
既然无缘 2020-12-08 01:07

When I add the transition line into my code it breaks jQuery. How can I fix it?

a(this).next().css({
    left: c,
    transition: \'opacity 1s ease-in-out\'
         


        
2条回答
  •  一整个雨季
    2020-12-08 01:42

    Your code can get messy fast when dealing with CSS3 transitions. I would recommend using a plugin such as jQuery Transit that handles the complexity of CSS3 animations/transitions.

    Moreover, the plugin uses webkit-transform rather than webkit-transition, which allows for mobile devices to use hardware acceleration in order to give your web apps that native look and feel when the animations occur.

    JS Fiddle Live Demo

    Javascript:

    $("#startTransition").on("click", function()
    {
    
        if( $(".boxOne").is(":visible")) 
        {
            $(".boxOne").transition({ x: '-100%', opacity: 0.1 }, function () { $(".boxOne").hide(); });
            $(".boxTwo").css({ x: '100%' });
            $(".boxTwo").show().transition({ x: '0%', opacity: 1.0 });
            return;        
        }
    
        $(".boxTwo").transition({ x: '-100%', opacity: 0.1 }, function () { $(".boxTwo").hide(); });
        $(".boxOne").css({ x: '100%' });
        $(".boxOne").show().transition({ x: '0%', opacity: 1.0 });
    
    });
    

    Most of the hard work of getting cross-browser compatibility is done for you as well and it works like a charm on mobile devices.

提交回复
热议问题