蛋疼少年的和谐青春
2016-01-05 12:56
/**
* Created by Administrator on 2016/1/5.
*/
function setMove(obj,sty,goal){
var div =document.getElementById("wrap");
var timer=null;
timer=setInterval(function(){
var ist = 0
if(sty=='opacity'){
ist=Math.round(parseFloat(setStyle(obj,sty))*100);
}else{
ist=parseInt(setStyle(obj,sty));
}
var speed = (goal-ist)/10;
speed= speed>0? Math.ceil(speed):Math.floor(speed);
if(ist == goal){
clearInterval(timer);
}else{
if(sty=='opacity'){
obj.style.filter='alpha(opacity'+(ist + speed)+')';
obj.style.opacity=(ist + speed)/100;
}else {
obj.style[sty] = ist + speed + "px";
}
}
},30);
}
function setStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
<html> <head> <title> 事件</title> <meta charset="utf-8"> <title>111111111</title> <style> *{ margin: 0; padding: 0; } div{ width: 200px; height: 200px; background-color: #0B5DE5; } </style> <script> function setMove(obj,sty,goal){ var div =document.getElementById("wrap"); var timer=null; timer=setInterval(function(){ var ist = 0 if(sty=='opacity'){ ist=Math.round(parseFloat(setStyle(obj,sty))*100); }else{ ist=parseInt(setStyle(obj,sty)); } var speed = (goal-ist)/10; speed= speed>0? Math.ceil(speed):Math.floor(speed); if(ist == goal){ clearInterval(timer); }else{ if(sty=='opacity'){ obj.style.filter='alpha(opacity'+(ist + speed)+')'; obj.style.opacity=(ist + speed)/100; }else { obj.style[sty] = ist + speed + "px"; } } },30); } function setStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return getComputedStyle(obj,false)[attr]; } } </script> </head> <body> <div id="div"></div> <script> var div = document.getElementById('div'); div.onmouseover = function(){ setMove(this,'width',400); } </script> </body> </html>
是400呀。
JS动画效果
113923 学习 · 1443 问题
相似问题