码迷,mamicode.com
首页 > Web开发 > 详细

JS简单动画封装

时间:2019-05-07 21:29:25      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:timer   osi   ima   target   ext   charset   doctype   htm   count   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding:0;
}
.box{
width: 100px;
height: 100px;
border:1px solid red;
background-color: #FA5B5B;
position: relative;
left: 100px;
top: 100px;
opacity: 1;
}
</style>
</head>
<body>
<div class="box" id="box"></div>
<script type="text/javascript">

var obox = document.getElementById("box");
function animate(elem,targetJSON,time){
var count = 0;
var interval = 50;
var nowJSON = {};
var maxcount = time/interval;//次数
for(var k in targetJSON){
nowJSON[k] = parseFloat(Fecehcomputerstyle(elem,k));
}
var stepJSON = {};
for(var k in targetJSON){
targetJSON[k] = parseFloat(targetJSON[k]);
stepJSON[k] = parseFloat((targetJSON[k]-nowJSON[k])/maxcount);
}
console.log(nowJSON);
console.log(targetJSON);
console.log(stepJSON);
var timer = setInterval(function(){
for(var k in nowJSON){
nowJSON[k] += stepJSON[k];
if(k != "opacity"){
elem.style[k] = nowJSON[k] + "px";}
else{
elem.style[k] = nowJSON[k];
}
// console.log(elem.style[k]);
}
count++;
if(count == maxcount){
if(k != "opacity"){
elem.style[k] = targetJSON[k] + "px";}
else{
elem.style[k] = targetJSON[k];;
}
clearInterval(timer);
}
},interval);
function Fecehcomputerstyle(obj,property){
if(window.getComputedStyle){
return window.getComputedStyle(obj)[property];
}else{
return obj.currentStyle[property];
}//轮子
}
}
animate(obox,{"opacity":0.1},6000);
</script>
</body>
</html>

JS简单动画封装

标签:timer   osi   ima   target   ext   charset   doctype   htm   count   

原文地址:https://www.cnblogs.com/ljy0414/p/10827972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!