获取鼠标坐标,并且根据鼠标所在div弹出不同内容:
<!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);">鼠标相对于触发事件元素的位置<strong></strong> <div>aaa</div> <div>bbb</div> <div>ccc</div> <div>ddd</div> <div></div> </div> </body> </html>
以上所述是小编给大家介绍的JS获取鼠标坐标并且根据鼠标位置不同弹出不同内容,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
您可能感兴趣的文章: