最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fade outfade in effect with CSS3 - Stack Overflow

programmeradmin2浏览0评论

I'm trying to create a fade out / fade in effect with CSS3 animations. Here is my CSS :

#buttonright, #buttonleft{
-webkit-transition:opacity 0.5s linear;
-moz-transition:opacity 0.5s linear;
-o-transition:opacity 0.5s linear;
-ms-transition:opacity 0.5s linear; 
transition:opacity 0.5s linear;
}

And the Javascript (i'm using jquery) :

$('#buttonleft').css("opacity","0");
$('#buttonright').css("opacity","0");
$('#buttonleft').css("opacity","1");
$('#buttonright').css("opacity","1");

It looks like the browser think it's stupid to set the opacity to 0 then to set it back to 1. Does someone has a possible solution ?

Thank you.

I'm trying to create a fade out / fade in effect with CSS3 animations. Here is my CSS :

#buttonright, #buttonleft{
-webkit-transition:opacity 0.5s linear;
-moz-transition:opacity 0.5s linear;
-o-transition:opacity 0.5s linear;
-ms-transition:opacity 0.5s linear; 
transition:opacity 0.5s linear;
}

And the Javascript (i'm using jquery) :

$('#buttonleft').css("opacity","0");
$('#buttonright').css("opacity","0");
$('#buttonleft').css("opacity","1");
$('#buttonright').css("opacity","1");

It looks like the browser think it's stupid to set the opacity to 0 then to set it back to 1. Does someone has a possible solution ?

Thank you.

Share Improve this question asked Jan 26, 2012 at 13:57 KevinKevin 1,01910 silver badges16 bronze badges 2
  • 1 Just curious...why not handle it with JQuery itself? You can use the fadeIn() and fadeOut() functions provided with JQuery. – Sagar Patil Commented Jan 26, 2012 at 14:02
  • I used jquery before but CSS3 animations have better performance because of the GPU acceleration I think. This is for a web application only designed for Firefox so using CSS3 is not a problem. – Kevin Commented Jan 26, 2012 at 14:18
Add a ment  | 

4 Answers 4

Reset to default 5

Edit: Regard yaki's answer for a pure CSS3 solution.

You're not giving the browser enough time to plete the transition. If you add a setTimeout to the latter statements, it should work.

Something like this:

$('#buttonleft').css("opacity","0");
$('#buttonright').css("opacity","0");
setTimeout(function(){$('#buttonleft').css("opacity","1");}, 5000);
setTimeout(function(){$('#buttonright').css("opacity","1");}, 5000);

Actually accepted solution is not CSS3 solution (it's still requires some javascript code). Please check the code below.

html:

<a id='buttonleft'>Button left</a>
<a id='buttonright'>Button right</a>

css:

 #buttonleft, #buttonright {
    text-align: left;
    background: rgb(180,180,255);
    opacity:0.5;

    /* property duration timing-function delay */
    -webkit-transition: opacity 500ms linear 100ms;
    -moz-transition: opacity 500ms linear 100ms;
    -o-transition: opacity 500ms linear 100ms;
    transition: opacity 500ms linear 100ms;
    }

#buttonleft:hover, #buttonright:hover {
    opacity: 1.0;
}

something like this?

$('#button').hover(
    function() { 
        $(this).animate({opacity: 0}, 500); 
    },
    function() { 
        $(this).animate({opacity: 1}, 500);
    }
);

You can use CSS3 animations now that it is more supported than when you asked the original question. I've created a jsFiddle showing how to do this on hover.

@keyframes demo {
    from {
      animation-timing-function: ease;
      opacity: 1;
    }
    50% {
      animation-timing-function: ease-in;
      opacity: 0;
    }
    to {
      animation-timing-function: ease-inout;
      opacity: 1;
    }
}

img:hover
{
    animation-delay: 0s;
    animation-duration: 2s;
    animation-iteration-count: 1;
    animation-name: demo;
}
发布评论

评论列表(0)

  1. 暂无评论