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代码:
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)
}
<!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)
}
相关文章推荐
- html+css+js实现xp window界面及有关功能
- HTML+CSS+JS实现轮播效果
- html+css+js实现火影背景切换登录页面
- js html css实现复选框全选与反选
- HTML+CSS+jquery 实现环形比例图效果 AngularJS+css实现环形比例图效果
- html+css+js实现滑动导航条(转载)
- html+css+js实现的简易下拉菜单
- HTML+CSS+JS+node.js实现websocket聊天室
- 关于grunt实现js,html,css编译压缩
- 基于HTML+CSS+JS实现增加删除修改tab导航特效代码
- 网页天幕代码!从css、html到js的完美实现(自执行函数加原型的完美应用)!!
- Html+css+js+jquery实现简单页面登录
- 用html+css+js实现选项卡切换效果
- 一段js+html+css实现的loading图标效果
- php正则去除网页中所有的html,js,css,注释的实现方法
- .NET, MVC框架下利用html, CSS,js实现弹出窗口
- js实现单一html页面两套css切换代码
- js实现多物体不同运动框架
- html+css+js实现原生轮播特效