琴&
2019-01-16 16:03【多个运动】函数哪里错了?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style>
div {
width: 100px;
height: 50px;
background-color: red;
margin-top:50px;
}
</style>
<script type="text/javascript">
window.onload=function()
{
var aDiv = document.getElementsByTagName('div');
for(let i=0;i<aDiv.length;i++)
{
aDiv[i].timer = null;
aDiv[i].onmouseover = function ()
{
startMove(this,300);
}
aDiv[i].onmouseout = function ()
{
startMove(this,100);
}
}
}
function startMove(obj,iTarget)
{
clearInterval(obj.timer);
timer=setInterval(function () {
var iSpeed = (iTarget - obj.offsetWidth)/8;
iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
if(obj.offsetWidth == iTarget)
{
clearInterval(obj.timer);
}
else
{
obj.style.width = obj.offsetWidth + iSpeed + 'px';
}
},30)
}
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
</body>
</html>
- 点赞
- 回答
- 收藏
- 复制链接分享
3条回答
为你推荐
- 【多个运动】函数哪里错了?
- html5
- javascript
- css
- chrome
- 3个回答
- 这个变化函数哪里出问题了?
- html5
- javascript
- css
- chrome
- 2个回答
- c语言程序设计学生随机抽号演讲计分系统
- 新手求教
- 2个回答
- 这是怎么回事,不能让这几个div同时运动
- html5
- javascript
- 函数
- 2个回答
- 关于安卓SQLite的问题
- android
- sqlite
- 6个回答
换一换