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

HTML+CSS+JS实现多物体运动

2017-11-04 15:51 363 查看
1.box.html代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>广告</title>
<link rel="stylesheet" type="text/css" href="box.css"/>
<script src="box.js"></script>
</head>

<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>

</html>


2.box.css代码:
body {
padding: 0px;
margin: 0px;
}

ul {
list-style: none;
padding: 0px;
margin: 0px;
}

ul li {
width: 200px;
height: 100px;
margin: 10px 0px;
-webkit-border-radius: 5%;
-moz-border-radius: 5%;
border-radius: 5%;
background-color: pink;
}


3.box.js代码:
window.onload = function () {
var lis = document.getElementsByTagName('li');
for (var i = 0; i < lis.length; i++) {
lis[i].timer = null;
lis[i].onmouseover = function () {
move(this, 400);
}
lis[i].onmouseout = function () {
move(this, 200);
}
}
}

function move(object, target) {
clearInterval(object.timer);
object.timer = setInterval(function () {
var speed = (target - object.offsetWidth) / 8;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if (object.offsetWidth == target) {
clearInterval(object.timer);
} else {
object.style.width = object.offsetWidth + speed + 'px';
}
}, 30)
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: