郑尔多斯
2016-10-02 20:19
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>任意值运动</title>
<style>
body{
margin:0;
padding:0;
}
div{
width:200px;
height:200px;
background: green;
margin:50px;
font-size:10px;
}
</style>
<script>
window.onload = function () {
var oDiv1 = document.getElementById('div1');
var oDiv2 = document.getElementById('div2');
var oDiv3 = document.getElementById('div3');
oDiv1.onmouseover = function () {
startMove(this,'height',400);
};
oDiv1.onmouseout = function () {
startMove(this,'height',200);
};
oDiv2.onmouseover = function () {
startMove(this,'width',400);
};
oDiv2.onmouseout = function () {
startMove(this,'width',200);
};
oDiv3.onmouseover = function () {
startMove(this,'fontSize',50);
};
oDiv3.onmouseout = function () {
startMove(this,'fontSize',10);
};
} ;
var iCount = 0;
function startMove(obj,attr,iTarget) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var iCur = parseInt(getStyle(obj,attr));
var speed = (iTarget - iCur) / 8;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if(iCur == iTarget){
clearInterval(obj.timer);
}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>
</head>
<body>
<div id = 'div1'></div>
<div id = 'div2'></div>
<div id = 'div3'>hello world</div>
</body>
</html>
我把你的代码运行后没有出现你说的问题啊
跟你一样,坑
你加上
`*{ margin:0; padding:0}` 试看
JS动画效果
113925 学习 · 1443 问题
相似问题