<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin: 0;padding: 0;}
ul {list-style: none;}
li{
width: 200px;
height: 100px;
border: 1px solid #999;
background-color: yellow;
margin: 20px;
}
</style>
</head>
<body>
<div id="">
<ul>
<li id="li1"></li>
<li id="li2"></li>
</ul>
</div>
</body>
</html>
<script type="text/javascript">
var li1 = document.getElementById('li1');
var li2 = document.getElementById('li2');
li1.onmouseover = function () {
starMove(this,'opacity',100)
}
li1.onmouseout = function () {
starMove(this,'opacity',30)
}
function getStyle (obj,attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else{
return getComputedStyle(obj,false)[attr];
}
}
function starMove (obj,attr,target) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var icur = 0;
if (icur == 'opacity') {
icur = Math.round(parseFloat(getStyle(obj,attr))*100);
}else {
icur = parseInt(getStyle(obj.attr));
}
var speed = (target - icur)/8;
speed = speed > 0?Math.ceil(speed):Math.floor(speed);
if (icur == target) {
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';
}
}
},30)
}
</script