实现简单的动画效果,每隔2s,将p内的内容替换掉让将p向上消失,时间为2s,一步步测试的时候没问题,但运行时,没有任何动画效果
代码如下复制运行:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
#wrap{
width:200px;
height:40px;
border:thin solid red;
}
#content{
transition: all 2s;
height:20px;
}
</style>
</head>
<body>
<div id="wrap">
<p id="content">11111</p>
</div>
<script type="text/javascript">
function animation(){
var array=['111111111111111111','2222222222222222222','33333333333333333'];
var index=0;
setInterval(function(){
var wrap = document.getElementById('wrap');
var content = document.getElementById('content');
content.style.marginTop = -40+'px';
wrap.innerHTML = '<p id="content">'+array[index++]+'</p>';
if(index == array.length){
index = 0;
}
},2000);
}
animation();
</script>
</body>
</html>
逻辑有问题啊,在wrap.innerHTML = '<p id="content">'+array[index++]+'</p>';行上打个断点你就看到了,想实现这个效果你加个css3动画就行了
再加个定时器也可以,只不过这样太耗性能