如何判断鼠标在DIV的区域内?
2012-07-26 17:20
369 查看
今天研究了一下这个问题,也普及了一下知识吧。
方法一:
通过mouseover,mouseout来触发事件,才判断鼠标是否在该区域。 但是这种方法的局限性就是,必须要触发mouseover,或mouseout,mouseleave事件才能知道。
方法二:
方法三:
这个方法是最简单的实用的。
具体情况还是要根据自己需要来选择,我是调试了一下方法三,但是具体也没使用上。 其他方法,继续研究中。
方法一:
通过mouseover,mouseout来触发事件,才判断鼠标是否在该区域。 但是这种方法的局限性就是,必须要触发mouseover,或mouseout,mouseleave事件才能知道。
function chkIn() { div_1.innerText = "现在你把鼠标移入层了!"; div_1.style.font = "normal black"; } function chkOut() { div_1.innerText = "现在你把鼠标移出层了!"; div_1.style.font = "bold red"; }
<div id="div_1" style="background-color:lightblue; width:400px; height:300px; " onMouseOver="chkIn()" onMouseOut="chkOut()">This is a DIV </div>
方法二:
function checkIn(e){ var x=window.event.clientX; var y=window.event.clientY; var str= ' '; for(i=0;i <document.body.children.length;i++){ var obj=document.body.children[i]; if(x> obj.offsetLeft &&x <(obj.offsetLeft+obj.clientWidth) &&y> obj.offsetTop &&y <(obj.offsetTop+obj.clientHeight)){ str+= ' <鼠标位于层 '+obj.id+ '范围之内> \n '; }else{ str+= ' <鼠标位于层 '+obj.id+ '范围之外> \n '; } } alert(str); } document.onclick=checkIn
方法三:
这个方法是最简单的实用的。
if(myDiv.contains(window.event.srcElement))即 if(myDiv.contains(鼠标位置的元素对象))
具体情况还是要根据自己需要来选择,我是调试了一下方法三,但是具体也没使用上。 其他方法,继续研究中。
相关文章推荐
- 如何判断鼠标是否在DIV的区域内
- 如何判断鼠标是否在DIV的区域内
- 如何判断一个Div是否可视区域,判断div是否可见
- html 中如何让鼠标箭头移到某个区域(div,input,a等标签)上就变为手型,移出后再变为原型。
- 在一个CButton派生类CArrowButton内部,如何判断鼠标进入Button区域
- 如何判断鼠标按下的点是否在指定矩形区域CRect
- 体会setCapture和releaseCapture判断鼠标是否在某一区域
- qt 中鼠标落点区域判断方法
- 【NGUI】Unity中如何判断NGUI中当前鼠标是否点击UI上
- 怀念:如何判断鼠标离开窗口/浮动在窗口上?
- GDI+如何判断一个点是否在区域内
- 判断鼠标从div的哪一方向划入-------Day74
- 判断鼠标是否落在界面中某一个区域内 .
- 如何判断鼠标单击的单元格相对于单元格所在行的索引值
- js用斜率判断鼠标进入div的四个方向
- 【转】合并单元格时 如何判断所选区域是否为矩形
- 鼠标点是否在图形区域的判断
- JavaGUI事件编程时,如何判断鼠标单击的是哪个按钮?
- 在Qt中如何判断鼠标离开某一操作的时候,在时间差内自动执行某一个操作
- 如何判断DIV中的内容为空