您的位置:首页 > Web前端 > CSS

链式运动

2016-09-29 09:27 183 查看
CSS:

#li1{
width: 200px;
height: 100px;
border: 4px solid gray;
filter: alpha(opacity:30);
opacity: 0.3;
background: red;
list-style-type: none;
}

HTML:

<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="UTF-8">
<title>链式运动</title>
<link rel="stylesheet" href="move.css">
<script src="move.js">

</script>
<script>
window.onload = function(){
var li = document.getElementById('li1');
li.onmouseover = function(){
startMover(li,400,'width',function(){
startMover(li,200,'height',function(){
startMover(li,100,'opacity');
});
});
}
li.onmouseout = function(){
startMover(li,30,'opacity',function(){
startMover(li,100,'height',function(){
startMover(li,200,'width');
})
})

}
}
</script>
<style type="text/css">

</style>

</head>

<body>
<ul><li id="li1"></li></ul>

</body>

</html>

javascript:

function startMover(obj,iTarget,attr,fn){
clearInterval(obj.timer)
obj.timer = setInterval(function(){
var icur = 0;
if (attr == 'opacity') {
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);
if (fn) {
fn();
}
}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)
}

function getStyle(obj, attr) {

    if (obj.currentStyle) {

        return obj.currentStyle[attr];

    } else {

        return getComputedStyle(obj, false)[attr];

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  html css