qq_MissingYou_0
2017-02-18 10:23
为啥我的动画不出来
<style>
*{
margin:0;
padding:0;
}
div{
width:200px;
height:200px;
background:red;
border:4px solid black;
}
</style>
<script>
window.onload=function(){
var odiv=document.getElementById("div1");
alert(odiv.stye.width);
odiv.onmouseover=function(){
startMove();
}
}
var timer=null;
function startMove(){
clearInterval(timer);
var odiv=document.getElementById("div1");
timer=setInterval(function(){
if(odiv.offsetWidth==0){
clearInterval(timer);
}else{
// odiv.style.width=odiv.offsetWidth-9+'px';
odiv.style.width=parseInt(getStyle(odiv,width))-1+'px';
}
},30)}
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return obj.getComputedStyle(obj,false)[attr];
}
}
</script>
</head>
<body>
<div id="div1" ></div>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<style>
*{
margin:0;
padding:0;
}
div{
width:200px;
height:200px;
background:red;
border:4px solid black;
}
</style>
<script>
window.onload=function(){
var odiv=document.getElementById("div1");
odiv.onmouseover=function(){
startMove();
}
}
var timer=null;
function startMove(){
clearInterval(timer);
var odiv=document.getElementById("div1");
timer=setInterval(function(){
if(odiv.offsetWidth==0){
clearInterval(timer);
}else{
// odiv.style.width=odiv.offsetWidth-9+'px';
odiv.style.width=parseInt(getStyle(odiv,"width"))-1+'px';
}
},30)}
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</head>
<body>
<div id="div1" ></div>
</body>
</html>
以上,你的错误好多啊,自己对比吧
odiv.style.width=parseInt(getStyle(odiv,width))-1+'px';你的width没有加引号
window.onload=function(){
var odiv=document.getElementById("div1");
alert(odiv.stye.width);-----style
odiv.onmouseover=function(){
startMove();
JS动画效果
113925 学习 · 1443 问题
相似问题