control123
2016-09-23 15:42
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>
dbhs
</title>
<style type="text/css">
div{
width:200px;
height:100px;
background-color:yellow;
border:2px solid #000;
opacity: 0.3;
filter:alpha(opacity:30);
}
</style>
<script type="text/javascript">
window.onload=function(){
var odiv =document.getElementById("div");
odiv.onmouseover=function(){
startMove(odiv,'width',400);
}
}
function startMove(obj,attr,iTarget,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var icur=null;
if(attr=='opacity'){
icur=Math.round(parseFloat(getStyle(obj,attr))*100);
}else{
icur=parseInt(getStyle(obj,attr));
}
var speed=(iTarget-icur)/10;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(icur==iTarget){
clearInterval(obj.timer);
if(fn){
fn();
}
}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];
}else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</script>
</head>
<body>
<div id="div1">
</div>
</body>
</html>
你的id获取错了var odiv =document.getElementById("div");
改成var odiv =document.getElementById("div1");
我没有这水平,只能围观了
往上数倒数第七行多写了一个</script>结束标签,你看是不是这个原因
JS动画效果
113920 学习 · 1502 问题
相似问题