function getStyle(obj,attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return window.getComputedStyle(obj,null)[attr];
}
}
var timer = null;
function moveFrame(obj,json,callback) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var flag = true
for (var attr in json) {
var current = 0;
if (attr == 'opacity'){
current = parseInt(getStyle(obj,attr)*100);
} else {
current = parseInt(getStyle(obj,attr));
}
if (attr == 'opacity'){
var step = (json[attr]*100 - current) / 10;
step = step > 0? Math.ceil(step): Math.floor(step);
}else {
var step = (json[attr] - current) / 10;
step = step > 0? Math.ceil(step): Math.floor(step);
}
if(attr == 'opacity') {
if ("opacity" in obj.style) {
obj.style.opacity = parseFloat((current + step) / 100);
}else {
obj.style.filter = "alpha(opacity = "+ (current + step) +")";
}
}
else if(attr == "zIndex")
{
obj.style.zIndex = json[attr];
}
else {
obj.style[attr] = current + step + 'px';
};
if (current != json[attr]) {
flag = false;
};
};
if (flag){
clearInterval(obj.timer);
if (callback){
callback();
}
};
},30);
}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
position: absolute;
opacity: 1;
left: 0;
top: 100px;
}
</style>
</head>
<body>
<button>点击</button>
<div id="demo"></div>
<script src="js/moveFrame.js"></script>
<script>
var demo = document.getElementById("demo");
var btn= document.getElementsByTagName('button')[0];
console.log(btn,demo)
btn.onclick = function () {
moveFrame(demo,{height:"300",left:"200",opacity:"0.3",zIndex:10});
}
</script>
</body>
</html>