<!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
提问的时候最好贴一张带有你错误信息的图片,这样更好让别人给你解决问题,你直接传代码,一转码,太难看了。
小兄弟,你这代码看着辣眼啊
<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 (attr == 'opacity') {//是attr,不是icur,并且你的代码不支持IE8
icur = Math.round(parseFloat(getStyle(obj, attr)) * 100);
} else {
icur = parseInt(getStyle(obj,attr));//这里是逗号,不是点
}
console.log(icur)
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><!------这里少了闭合的>-------->