海之夜
2019-01-12 16:34
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.div1{
height: 200px;
width: 200px;
background: red;
position: relative;
left: 0;
opacity: 0.3;
}
</style>
<script >
window.onload=function(){
var Div=document.getElementById('div1')
Div.onmouseover=function(){
startMove(100)
}
Div.onmouseout=function(){
startMove(30)
}
}
var timer=null;
var alpha=30;
function starMove(itarget){
var Div=document.getElementById('div1')
clearInterval(timer);
time=setInterval(function(){
var speed=0;
if(alpha>itarget){
speed=-10;
}else{
speed=10;
}
if(alpha==itarget){
clearInterval(timer);
}else{
alpha+=speed;
Div.style.opacity=alpha/100;
}
})
}
</script>
</head>
<body>
<div class="div1">
</div>
</body>
</html>
time=setInterval 这行里,应该是timer ,不是time
Div.style.opacity=alpha/100;这行有问题
改为:Div.style.filter='alpha(opactiy:100'+alpha+')';
Div.style.opacity=alpha/100;
JS动画效果
113924 学习 · 1443 问题
相似问题