JS获取鼠标坐标并且根据鼠标位置不同弹出不同内容_javascript技巧_脚本之家

拿到鼠标坐标,並且依据鼠标所在div弹出分歧内容:

获取鼠标坐标,况且依照鼠标所在div弹出不相同内容:

多少个与一定有关的定义:

鼠标的距离var mouseX;var mouseY;function show { var infoDiv = document.getElementById; mouseOver; document.getElementById.innerHTML = mouseX+" "+mouseY ; infoDiv.style.display = "block"; //infoDiv.innerHTML = mouseX+" "+mouseY; infoDiv.style.left = mouseX + 10 + "px"; infoDiv.style.top = mouseY + 10 + "px"; }function hide() { var infoDiv = document.getElementById.style.display = "none";;}function mouseOver { e = obj || window.event; // 此处记录鼠标停留在组建上的时候的位置, 可以自己通过加减常量来控制离鼠标的距离. mouseX = e.layerX|| e.offsetX; mouseY = e.layerY|| e.offsetY; if { infoDiv.innerHTML = "aaa"; } else if { infoDiv.innerHTML = "bbb"; } else if { infoDiv.innerHTML = "ccc"; } else if { infoDiv.innerHTML = "ddd"; }else{ infoDiv.innerHTML = "eee"; }}鼠标相对于触发事件元素的位置 aaa bbb ccc ddd 
<!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>
<head>
<title>鼠标的距离</title>
<script type="text/javascript">
var mouseX;
var mouseY;
function show(event) {
 var infoDiv = document.getElementById('infoDiv');
 mouseOver(event);
    document.getElementById("a").innerHTML = mouseX+" "+mouseY ;
    infoDiv.style.display = "block";
 //infoDiv.innerHTML = mouseX+" "+mouseY;
    infoDiv.style.left = mouseX + 10 + "px";
    infoDiv.style.top = mouseY + 10 + "px";    
}
function hide() {
    var infoDiv = document.getElementById('infoDiv').style.display = "none";;
}
function mouseOver(obj) {
    e = obj || window.event;
    // 此处记录鼠标停留在组建上的时候的位置, 可以自己通过加减常量来控制离鼠标的距离.
    mouseX =  e.layerX|| e.offsetX;
    mouseY =  e.layerY|| e.offsetY; 
    if(e.target.id == "aaa")
    {
        infoDiv.innerHTML = "aaa";
    }
    else if(e.target.id == "bbb")
    {
        infoDiv.innerHTML = "bbb";
    }
    else if(e.target.id == "ccc")
    {
        infoDiv.innerHTML = "ccc";
    }
    else if(e.target.id == "ddd")
    {
        infoDiv.innerHTML = "ddd";
    }else{
        infoDiv.innerHTML = "eee";
        }


}
</script>
</head>
<body>
<div onmousemove="show(event);" onmouseout="hide();" style="margin:150px ; background:#ff0; height:300px; width:300px; position:relative; ">鼠标相对于触发事件元素的位置<strong id="a"></strong>
    <div id="aaa">aaa</div>
    <div id="bbb">bbb</div>
    <div id="ccc">ccc</div>
    <div id="ddd">ddd</div>
  <div id="infoDiv" style="display: none; position: absolute; width: 100px; height: 100px; background-color: #F1F19B;"></div>
</div>
</body>
</html>

js:

上述所述是作者给大家介绍的JS获取鼠标坐标而且依据鼠标地点分裂弹出分化内容,希望对大家具有利于,假如大家有别的疑窦款待给作者留言,我会及时还原大家的!

 

clientX/clientY属性:拿到事件发生时鼠标指针在视口中的水平和垂直坐标。

screenX/screenY品质:获取鼠标事件时有发生时鼠标光标相对于整个Computer荧屏的坐标音讯。

pageX/pageY属性:能够得到鼠标事件发生时鼠标光标相对于一切文书档案成分的坐标地点(满含滚动)。在页面未有滚动的情事下,经常pageX/pageY的值与clientX/clientY的值至极。