在div中获取鼠标位置? [英] Get mouse position within div?

查看:384
本文介绍了在div中获取鼠标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

鼠标相对于div的位置

在画布中使用javascript获取鼠标位置

鼠标在一个固定大小但是有自动边距的画布中的位置?

How can I get the position of the mouse within a canvas that is a fixed size but has an automatic margin?

我不能使它的位置固定,不能只使用普通鼠标

I can't make its position fixed and can't just use the regular mouse position on the page.

此代码的工作原理完美:

This code works perfectly:

mouseX = e.pageX - div.offsetLeft;
mouseY = e.pageY - div.offsetTop;


推荐答案

使用jQuery:

var divPos = {};
var offset = $("#divid").offset();
$(document).mousemove(function(e){
    divPos = {
        left: e.pageX - offset.left,
        top: e.pageY - offset.top
    };
});

这篇关于在div中获取鼠标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆