<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
ul,li{
list-style: none;
}
ul li{
width: 100px;
height: 100px;
background: #808002;
margin-bottom: 10px;
border: 4px solid #ccc;
filter: alpha(opacity:50);//IE兼容
opacity: 0.5;//Chrome兼容
cursor: pointer;
}
</style>
<script type="text/javascript">
window.onload=function(){
//载入滑动动效
var ddjli=document.getElementsByTagName("li");
for(var i=0;i<ddjli.length;i++){
ddjli[i].timer=null;//时间片私有化
ddjli[i].onmouseover=function(){
demo(this,"width",400,function(){
demo(this,"height",400);
});
//demo(this,"height",400);
//demo(this,"opacity",80);
};
ddjli[i].onmouseout=function(){
demo(this,"width",100,function(){
demo(this,"height",100);
});
//demo(this,"height",100);
//demo(this,"opacity",50);
};
}
}
//集成封装-缓冲拉长/透明度变幻/长宽动效
function demo(obj,attr,itarget,fn){
//先清除定时器,防止迭代
clearInterval(obj.timer);
obj.timer=setInterval(function(){
//样式获取obj的attr属性
var icur=0;
if(attr=="opacity"){
icur=Math.round(parseFloat(getStyle(obj,attr))*100);
}else{
icur=parseInt(getStyle(obj,attr));
}
//变加速,距离差越近速度越小,缓冲效果
var speed=(itarget-icur)/10;
//速度取整
if(speed>0){
speed=Math.ceil(speed);
}else{
speed=Math.floor(speed);
}
if(icur==itarget){
clearInterval(obj.timer);
}else{
if(attr=="opacity"){
obj.style.filter="alpha(opacity:"+icur+speed+")";
obj.style.opacity=(icur+speed)/100;
}else{
obj.style[attr]=icur+speed+"px";
}
}
if(fn){
fn();
}
},30);
}
//样式BUG处理,解决offsetWidth漏洞问题
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</head>
<body>
<ul>
<li>选项一</li>
<li>选项二</li>
<li>选项三</li>
</ul>
</body>
</html>