慕粉1703136593
2017-03-07 14:34
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style style="text/css">
*{padding:0;margin:0;}
#tmd{width:300px;height:300px;
background:green;
filter:alpha(opacity:30);
opacity:0.3;
}
</style>
<script>
window.onload=function(){
var adiv=document.getElementById("tmd");
adiv.onmouseover=function(){startmove(100);}
adiv.onmouseout=function(){startmove(30)}}
var timer=null,
var alpha=30;
function startmove(itarget){
var adiv=document.getElementById("tmd");
clearInterval(timer);
timer=setInterval(function(){
var speed=0
if (alpha>itarget){
var speed = -10
}
else{
var speed = 10
}
if (alpha == itarget) {
clearInterval(timer)
}
else
{alpha =alpha+speed;
adiv.style.filter='alpha(opacity:+"alpha"+)';
adiv.style.opacity=alpha/100;
}
},30)
}
</script>
</head>
<div id="tmd"></div>
<body>
</body>
</html>
书写不规范,错误太多:
var timer=null, var alpha=30; // 上一行结尾是逗号,这一行就不要写var了
再比如:
adiv.style.filter='alpha(opacity:+"alpha"+)'; // 单引号和双引号乱用;而且加号怎么在引号里?
……
自己仔细检查一下哈
你的代码运行报错;
var timer = null , alpha = 30;
或者
var timer = null ;
var alpha = 30 ;
还有就是楼上说的对: 这里 if(){}else{}语句里面直接写speed就可以,前面不用加var变量声明了
timer=setInterval(function(){
var speed=0
if (alpha>itarget){
var speed = -10
}
else{
var speed = 10
}
这里 if(){}else{}语句里面直接写speed就可以,前面不用加var变量声明了
JS动画效果
113925 学习 · 1443 问题
相似问题