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)
}