问题
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