div跟随鼠标移动代码。不需要jquery,div自动调整

jiguoguo 2009-10-27 05:41:32
div跟随鼠标移动代码。不需要jquery,div自动调整

div到页面的右边后到左边来,鼠标到下面时到div到上面,支持滚动条移动

哪位大虾有这样的代码。我找了好久没找到。包括找了国外的网站
...全文
932 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiguoguo 2009-11-13
  • 打赏
  • 举报
回复
虽然没找到答案,但是多少给点分数。hhh816大哥贴了很多代码。呵呵
jiguoguo 2009-11-13
  • 打赏
  • 举报
回复
楼上的。你没有懂我的意思。呵呵。
hhh816 2009-10-27
  • 打赏
  • 举报
回复
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<div style="background-color:#00CC00; width:100px; height:100px; position:absolute; left:225px; top:80px;" id="div1"></div>
</body>
</html>
<script>
var m_x;
var m_y;
var div_=document.getElementById("div1").style;
var id1;
var id2;
document.onmousemove=function()
{
m_x=event.x;
m_y=event.y;
move();
}

function move()
{
if(parseInt(div_.left)>parseInt(m_x))
{
if(id1==null)
id1=setInterval(div_move_left,10);
}
else
{
if(id1==null)
id1=setInterval(div_move_right,10);
}
if(parseInt(div_.top)>parseInt(m_y))
{
if(id2==null)
id2=setInterval(div_move_top,10);
}
else
{
if(id2==null)
id2=setInterval(div_move_bottom,10);
}
}
function div_move_left()
{
div_.left=parseInt(div_.left)-2;
if(parseInt(div_.left)<parseInt(m_x))
{
clearInterval(id1);
id1=null;
}
}
function div_move_right()
{
div_.left=parseInt(div_.left)+2;
if(parseInt(div_.left)>parseInt(m_x))
{
clearInterval(id1);
id1=null;
}
}
function div_move_top()
{
div_.top=parseInt(div_.top)-2;
if(parseInt(div_.top)<parseInt(m_y))
{
clearInterval(id2);
id2=null;
}
}
function div_move_bottom()
{
div_.top=parseInt(div_.top)+2;
if(parseInt(div_.top)>parseInt(m_y))
{
clearInterval(id2);
id2=null;
}
}
</script>
sangshusen_1988 2009-10-27
  • 打赏
  • 举报
回复
根据offsetWidth还有鼠标的坐标计算下就成了,自己判断下就成了,有你找的这时间,估计你都写成了
zl3450341 2009-10-27
  • 打赏
  • 举报
回复
...做页面的广告?
帮顶下
gukuitian 2009-10-27
  • 打赏
  • 举报
回复
div到页面的右边后到左边来,鼠标到下面时到div到上面,支持滚动条移动


看了好几遍也没看懂这 句
liuhua_sun 2009-10-27
  • 打赏
  • 举报
回复
嗯!还是顶吧!
swandragon 2009-10-27
  • 打赏
  • 举报
回复
判断鼠标的位置,判断层与body的位置

UP

81,094

社区成员

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

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