js mousedown事件怎么获取座标?

guoheng90 2010-01-13 08:58:59
现在页面上有一个显示的div ,我想 触发 mousedown事件,如果鼠标不是在div的范围内,
那么就把div给隐藏,不知道如何实现,请高手指点!
最好兼容 firefox和ie

...全文
973 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
guoheng90 2010-02-08
  • 打赏
  • 举报
回复
$(document).mousedown(function(event) {
var left = calculateOffset(globalSearchSelector, "offsetLeft");
var top = calculateOffset(globalSearchSelector, "offsetTop");
var width = globalSearchSelector.width();
var height = globalSearchSelector.height();
var top1 = calculateOffset(nav_SearchKeyWord, "offsetTop");
var height1 = nav_SearchKeyWord.height();
//在搜索框和提示层范围外清除提示
if (event.clientX < left || event.clientX > left + width) {
clearItems();
}
else {
if (event.clientY < top || event.clientY > height + top) {
if (event.clientY < top1 || event.clientY > height1 + top1) {
clearItems();
}
}
}
});
千游 2010-01-13
  • 打赏
  • 举报
回复
d
一弗楚 2010-01-13
  • 打赏
  • 举报
回复
<html>
<head>
<title> </title>
</head>
<body onmousedown="document.getElementById('div').style.display='none'">
<div id="div" style="cursor:hand;width:200px;height:200px; background-color:red;" onmousedown="if(window.event) window.event.cancelBubble=true; else arguments[0].stopPropagation();"></div>
</body>
<html>
Click_Me 2010-01-13
  • 打赏
  • 举报
回复
你的思路有点问题,跟获取鼠标无关

<body>
<div id="odiv" style="width:100px;height:100px;border:1px solid red;background-color:#D2D2D2"></div>
<script type="text/javascript">
var O = document.getElementById('odiv');
document.onmousedown = function() {
O.style.display = "none";
}
O.onmousedown = function(e) {
!!e ? e.stopPropagation() : event.cancelBubble = true
}
</script>
</body>
h155290688 2010-01-13
  • 打赏
  • 举报
回复
//获得某个节点的相对坐标
function GetElementPoint(object)
{
var x=0,y=0;
while(object.offsetParent)
{

x+=object.offsetLeft;
y+=object.offsetTop;
object=object.offsetParent;
}
x+=object.offsetLeft;
y+=object.offsetTop;
return {'x':x,'y':y};
}
guoheng90 2010-01-13
  • 打赏
  • 举报
回复
[oElement.x, oElement.y, x2, y2] 里面的四个对象 具体是指什么??
呵呵 我是新手!
jianshao810 2010-01-13
  • 打赏
  • 举报
回复
获取某个元素的坐标。
function findPosition(oElement) {
var x2 = 0;
var y2 = 0;
var width = oElement.offsetWidth;
var height = oElement.offsetHeight;
// alert(width + "=" + height);
if (typeof (oElement.offsetParent) != 'undefined') {
for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
posX += oElement.offsetLeft;
posY += oElement.offsetTop;
}
x2 = posX + width;
y2 = posY + height;
return [posX, posY, x2, y2];

} else {
x2 = oElement.x + width;
y2 = oElement.y + height;
return [oElement.x, oElement.y, x2, y2];
}
}
h155290688 2010-01-13
  • 打赏
  • 举报
回复
可以给div添加一个attribute,isin,如果鼠标在div范围内就是true,否则是false,当发生mousedown时检测isin就可以了

87,914

社区成员

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

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