如何获取鼠标位置

编程入门 行业动态 更新时间:2024-10-28 07:31:01
本文介绍了如何获取鼠标位置-相对于元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想获取鼠标相对于光标所在元素的位置.

I would like to get the mouse position, relative to the element the cursor is in.

以下代码捕获了鼠标相对于页面的位置.

The following code capture the position where mouse is relative to the page.

$( "div" ).mousemove(function( event ) { var pageCoords = "( " + event.pageX + ", " + event.pageY + " )"; var clientCoords = "( " + event.clientX + ", " + event.clientY + " )"; $( "span:first" ).text( "( event.pageX, event.pageY ) : " + pageCoords ); $( "span:last" ).text( "( event.clientX, event.clientY ) : " + clientCoords ); });

api.jquery/mousemove/

推荐答案

尝试一下 JSFiddle

您需要包括.js文件才能使用此代码: Jquery-2.1.1. js

You need to include .js file for using this code: Jquery-2.1.1.js

相对鼠标位置的代码在此处:

For Relative mouse position the code is here:

var x,y; $("#div1").mousemove(function(event) { var offset = $(this).offset(); x = event.pageX- offset.left; y = event.pageY- offset.top; $("#div1").html("(X: "+x+", Y: "+y+")"); });

更多推荐

如何获取鼠标位置

本文发布于:2023-05-30 09:20:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/365352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   位置

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!