白鹿max
2018-01-15 15:54
window.onload = function () {
/*var ali = document.getElementsByTagName('li'); // 获取LI
for (var i = 0; i < ali.length; i++) {
ali[i].timer = null; //使用数组写法定义独立的定时器,避免公用定时器的争抢。
ali[i].onmouseover = function () {
stratMove(this, 400);
}
ali[i].onmouseout = function () {
stratMove(this, 200);
}
}
}*/
var Li1 = document.getElementById('li1');
//var Li2 = document.getElementById('li2');
Li1.onmouseover = function () {
startMove(this, 'opacity',100);
}
Li1.onmouseout = function () {
stratMove(this, 'opacity', 30);
}
}
//var timer = null; //定义一个定时器,取消公用定时器
var alpha = 30;
function startMove(obj, attr, iTarget) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var icur = 0;
if (attr == 'opacity') {
//Math.round()将一个数四舍五入为一个最接近的整数
icur = Math.round(parseFloat(getStyle(obj, attr)) * 100);
} else {
icur = parseInt(getStyle(obj, attr));
}
var speed = (iTarget - icur) / 8;
speed = speed > 0 ? Math.ceil(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;
}
}
}, 30);
}
function getStyle(obj, attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
} //封装获取样式元素
Li1.onmouseout = function () {
stratMove(this, 'opacity', 30);
}
start拼写错误,其他没看
JS动画效果
113925 学习 · 1443 问题
相似问题