慕粉3244739
2016-05-26 15:22
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>lianshi</title>
<style>
*{
margin: 0;
padding: 0;
}
ul,li{
list-style: none;
}
#li1{
width:250px;
height: 150px;
background: red;
margin-bottom: 20px;
border: 4px solid #000;
filter: alpha(opacity:30);
opacity: 0.3;
}
</style>
<script type="text/javascript" src="lianshi.js"></script>
<script>
window.onload=function(){
var li=document.getElementById('li1');
li.onmouseover=function(){
startMove(li,'width',400,function(){
startMove(li,'height',400,function(){
startMove(li,'opacity',100);
});
});
}
}
</script>
</head>
<body>
<ul>
<li id="li1"></li>
</ul>
</body>
</html>
/************************JS代码*****************************/
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr]//针对IE浏览器
}else{
return getComputedStyle(obj,false)[attr];//针对火狐浏览器
}
}
function startMove(obj,attr,iTarget,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var icur=0;
if(attr=='opacity'){
icur=Math.round(parseFloat(getStyle(obj.attr))*100);
}else{
icur=parseInt(getStyle(obj,attr));
}
//2算速度
var speed=(iTarget-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed)
//3.检测停止
if(icur==iTarget){
clearInterval(obj.timer)
if(fn){
fn();
}
}else{
if(attr=='opacity'){
obj.style.filter='alpha:(opacity:'+icur+speed+')';
obj.style.opacity=(icur+speed)/100;
}
else{
obj.style[attr]=icur+speed+'px';
}
}
},30)
}
icur=Math.round(parseFloat(getStyle(obj.attr))*100);
obj.attr => obj,attr 细心一点啊兄台。
JS动画效果
113925 学习 · 1443 问题
相似问题