How to trigger CSS3 fade-in effect using Javascript?

空扰寡人 提交于 2020-01-01 07:22:56

问题


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.


回答1:


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.net/RYgsA/




回答2:


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.net/z54a75os/



来源:https://stackoverflow.com/questions/8126404/how-to-trigger-css3-fade-in-effect-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!