点击按钮移动DIV

史上最难读懂的一个人 2013-05-22 11:45:11
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>点击按钮让DIV层弹性移动</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
#div1{
background: #FFCC66;
border:#FF6600 1px solid;
height:100px;
width:100px;
position:relative;
left:0px;
}
</style>
<script type="text/javascript">
var t=null;

function startMove()
{
if(t)
{
clearInterval(t);
}
t=setInterval(move, 30);
}
var step=0;
function move()
{
var odiv=document.getElementById("div1");
step+=(100-odiv.offsetLeft)/50;
step=step*0.98

odiv.style.left=odiv.offsetLeft+step;

}
</script>
</head>
<body>
<div id="div1">
</div>
<input type="button" value="移动" onclick="startMove()"/>
</body>
</html>

如code标题,这段代码在IE里要允许运行ActiveX控件才能运行,在谷歌浏览器根本运行不起来。
求兼容谷歌浏览器的修改方法。
...全文
304 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

<style type="text/css">
#div1{
background: #FFCC66;
border:#FF6600 1px solid;
height:100px;
width:100px;
position:relative;
left:0px;
}
</style>
<script type="text/javascript">
 var t=null;
 
 function startMove()
 {
  if(t)
  {
   clearInterval(t);
  }
  t=setInterval(move, 30);
 }
 
 var step=0;
 function move()
 {
  var odiv=document.getElementById("div1");
   step+=(100-odiv.offsetLeft)/50;
   step*=0.98;
 
  odiv.style.left=odiv.offsetLeft+step+"px";
  
 }
</script>
</head>
<body>
<div id="div1">
</div>
<input type="button" value="移动" onclick="startMove()"/>
</body>
</html>
  • 打赏
  • 举报
回复
odiv.style.left=odiv.offsetLeft+step + 'px';
Lake9146 2013-05-22
  • 打赏
  • 举报
回复
jquery是不是弄这个是不是简单点

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧