一、Html布局
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
二、Css样式
<style>
body,ul{
margin:0;
padding: 0;
}
ul,li{
list-style: none;
}
ul li{
width:200px;
height: 100px;
background: yellow;
margin-bottom: 20px;
filter:alpha(opacity:30); /*针对 IE8 以及更早的版本*/
opacity:0.3;
}
</style>
三、Js部分
<script>
window.onload = function(){
var aLi = document.getElementsByTagName("li");
for (var i = 0; i < aLi.length; i++) {
aLi[i].timer = null;
//多物体运动 必须给每个li定义定时器
aLi[i].onmouseover = function(){
startMove(this,400);
}
aLi[i].onmouseout = function(){
startMove(this,200);
}
}
}
startMove = function(obj,iTarget){
clearInterval(obj.timer);
//开启定时器前先关闭所有定时器
obj.timer = setInterval(function(){
var speed = (iTarget - obj.offsetWidth)/8;
//为了方便直接使用offsetWidth 后面的demo中会修改
speed = speed > 0?Math.ceil(speed):Math.floor(speed);
//缓冲运动 speed>0向上取整 speed<0向下取整
if(obj.offsetWidth == iTarget){
clearInterval(obj.timer);
}else{
obj.style.width = obj.offsetWidth + speed + "px";
}
},30)
}
</script>
<script>
window.onload = function(){
var aLi = document.getElementsByTagName("li");
for (var i = 0; i < aLi.length; i++) {
aLi[i].alpha = 30;
//多物体运动 透明度也不能共用
aLi[i].timer = null;
//多物体运动 必须给每个li定义定时器
aLi[i].onmouseover = function(){
startMove(this,100);
}
aLi[i].onmouseout = function(){
startMove(this,30);
}
}
}
startMove = function(obj,alphaNum){
clearInterval(obj.timer);
//开启定时器前先关闭所有定时器
obj.timer = setInterval(function(){
var clarity = 0;
//定义透明度改变的速度
if(obj.alpha > alphaNum){
clarity = -10;
}else{
clarity = 10;
}
if(obj.alpha == alphaNum){
clearInterval(obj.timer);
}else{
obj.alpha += clarity;
obj.style.filter = 'alpha(opacity: '+obj.alpha+')';
//针对 IE8 以及更早的版本
obj.style.opacity = obj.alpha/100;
}
},30)
}
</script>