admin管理员组

文章数量:1302401

I've implemented a fade-in effect using pure Javascript, but I yearn for something more native, like using CSS3. Is there a way to trigger the CSS fade-in effect from Javascript?

Thanks.

I've implemented a fade-in effect using pure Javascript, but I yearn for something more native, like using CSS3. Is there a way to trigger the CSS fade-in effect from Javascript?

Thanks.

Share edited Nov 15, 2011 at 9:32 BoltClock 724k165 gold badges1.4k silver badges1.4k bronze badges asked Nov 14, 2011 at 18:37 user945620user945620 0
Add a ment  | 

2 Answers 2

Reset to default 7

HTML:

<div id="foo" class="hidden">Hello!</div>

CSS:

div {
    -webkit-transition: opacity 1s linear;
}
.hidden {
    opacity: 0;
}

JS:

setTimeout(function() {
    document.getElementById('foo').className = '';
}, 1000);

http://jsfiddle/RYgsA/

here you go

HTML

<div id="box"></div>

CSS

#box {
    width: 100px;
    height: 100px;
    background-color: black;
    opacity: 1;
    -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;
}

JS

function fadde() {
        var box = document.getElementById('box');
        box.style.opacity = (box.style.opacity == 1) ? 0 : 1;   
}
setInterval(fadde, 1000);

example at

https://jsfiddle/z54a75os/

本文标签: cssHow to trigger CSS3 fadein effect using JavascriptStack Overflow