<!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>
#div1{
width:200px;
height:200px;
background:#C6F;
border:4px solid #000;
opacity:0.3;
filter=alpha(opacity:30);
}
</style>
<script>
var div1=document.getElementById("div1");
div1.onmouseover=function(){
startMove(this,"opacity",100);
}
div1.onmouseout=function(){
startMove(this,"opacity",30);
}
var alpha=30;
function startMove(obj,attr,iTarget){
clearInterval(obj,timer);
obj.timer=setInterval(function(){
var icur=0;
if(attr=="opacity"){
icur=Math.round(parseFloat(getStyle(obj,attr)))*100;
}
else{
icur=parseInt(getStyle(obj,attr));
}
//var icur=parsetInt(getStyle(obj,attr));
var speed=(iTarget-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(icur==iTarget){
clearInterval(obj.timer);
}
else{
if(attr=="opacity"){
obj.style.filter="alpha(opacity:"+(icur+speed)+")";
obj.style.opacity=(icur+speed)/100;
}
else{
obj.style[attr]=icur+speed+"px";
}
}
},30)
}
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];//currentStyle针对IE浏览器
}
else{
return getComputedStyle(obj,false)[attr];//getComputedStyle针对firefox浏览器
}
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
哈哈~我是看完这个视频的前一半来评论的~然后接着看视频的后半段~原来老师用的就是Math.round。。。。我这还以为你自己想的方法呢。。。
首先你用了一个Math.round这个方法,非常棒。解决了老师该例onmouseout返回0.3出现的小bug。
但是呢,你太马虎了。
首先css style里 filter:alpha(opacity=30);
然后定义function startMove中 :clearInterval(obj,timer);应为obj.timer。但即使你改成了obj.timer还是不行。
因为这之前并没有去定义这个timer.我这里在函数外面定义 timer=null;因为这里我们只调用这一个函数,不需要考虑多物体运动的例子,然后也要改 timer=setInterval();完美运转。下面是我改过的,稍微变动了一下顺序,顺便用window.onload来加载:
<!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>
#div1{
width:200px;
height:200px;
background:#C6F;
border:4px solid #000;
opacity:0.3;
filter:alpha(opacity=30);
}
</style>
<script>
window.onload=function(){
var div1=document.getElementById("div1");
div1.onmouseover=function(){
startMove(this,"opacity",100);
}
div1.onmouseout=function(){
startMove(this,"opacity",30);
}
}
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return getComputedStyle(obj,false)[attr];
}
}
var alpha=30;
var timer=null;
function startMove(obj,attr,iTarget){
clearInterval(timer);
timer=setInterval(function(){
var icur=0;
if(attr=="opacity"){
icur=Math.round(parseFloat(getStyle(obj,attr))*100);
}
else{
icur=parseInt(getStyle(obj,attr));
}
var speed=(iTarget-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(icur==iTarget){
clearInterval(timer);
}
else{
if(attr=="opacity"){
obj.style.filter="alpha(opacity="+(icur+speed)+")";
obj.style.opacity=(icur+speed)/100;
}
else{
obj.style[attr]=icur+speed+"px";
}
}
},30)
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>