[javascript]运动函数
以'px'为单位的运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div {
width: 100px;
height: 50px;
background: red;
cursor: pointer;
margin-top: 20px;
font-size: 14px;
border: 0 solid #000;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div>aaaaaaaaaaaaaa</div>
<div></div>
<script>
function getStyle (obj, attr){
if (obj.currentStyle) {
return obj.currentStyle;
} else {
return getComputedStyle(obj, false)[attr];
}
}
function startMove (obj, attr, iTarget){
// var oDiv = document.getElementById("div1");
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var iCur = parseInt(getStyle(obj, attr));
var iSpeed = (iTarget-iCur)/8;
iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if (iCur === iTarget) {
clearInterval(obj.timer);
} else {
obj.style[attr] = iCur + iSpeed + "px";
}
}, 30);
}
window.onload = function (){
var aDiv = document.getElementsByTagName("div");
var i = 0;
aDiv[0].onclick = function (){
startMove(this, 'width', 300);
}
aDiv[1].onclick = function (){
startMove(this, 'height', 200);
}
aDiv[2].onclick = function (){
startMove(this, 'font-size', 100);
}
aDiv[3].onclick = function (){
startMove(this, 'borderWidth', 100);
}
}
</script>
</body>
</html>

浙公网安备 33010602011771号