YPOC
YPOC

Reputation: 685

css transition with javascript has no transition

when I try to make a simple transition with pure CSS all works fine.
But when I try to call a javascript-function which modiefies the CSS when clicked on a link, there is no transition. I want to fade in a grey layer.

HTML:

<a href="someImage.jpg" id="test">
    <img src="someImage.jpg" alt="" />
</a>
<section id="grey"> 
</section>

JS:

var grey = document.getElementById("grey");
var link = document.getElementById("test");
link.onclick = function () {
    grey.style.display = "block";
    grey.style.opacity = "1";
    return false;
};

CSS:

section {
    display:none;
    size and position...
    opacity: 0;
    background-color: rgba(0,0,0,0.5);
    transition: opacity 1s .5s;
}

(see http://jsfiddle.net/7zEhx/5/ )

I'm using FF22, does anyone know any solutions?

Upvotes: 3

Views: 215

Answers (1)

Ry-
Ry-

Reputation: 224852

display: none elements don’t transition, and the display: block hasn’t kicked in before you set the other style. There are horrible hacks like this:

setTimeout(function() {
    grey.style.opacity = "1";
}, 0);

But I’d just set width to 0 instead of setting display and then put it back at 100%.
Updated jsFiddle

Upvotes: 4

Related Questions