确定用户单击图像的位置

发布于 2024-10-21 06:36:24 字数 1140 浏览 3 评论 0原文

我正在使用 Javascript/jQuery 和 CSS 来让俄克拉荷马州地图的“背景”图像填充屏幕。这个想法是,用户将单击地图上几个不同“区域”中的某个区域,并且将根据他们单击的区域触发事件。

我的问题是我需要确定用户点击的位置。我尝试过使用像素,但像素不可靠,因为尺寸会根据屏幕尺寸而改变。我曾考虑过将背景图像切割成单独的图像并使它们各自成为自己的容器,但这似乎不必要地复杂。

那么,有没有办法让我确定用户点击屏幕的位置?我可以提供一些我已经编写并尝试过的代码;现在一切都支离破碎了,因为我仍在尝试不同的事情!

这是我之前尝试过的一件事:

$(document).ready(function()
{
    $("#container").click(function(e)
    {
        var x = ("#container").pageX;
        var y = ("#container").pageY;
        if(x >= ? && x <= ? && y >= ? && y <= ?) 
        {
            alert('You clicked the panhandel!');
        }
   }); 
});

我将 if 语句要求中的 xy 值设置为“装箱”我想要警报的区域的像素值弹出,但什么也没发生。

编辑 感谢您的帮助和时间,我非常感激!我困惑的最后一件事是 if() 内部应该包含什么内容。由于元素将是屏幕的大小,我认为我真的不需要偏移,但是

 if(x >= 200 && x <= 300 && y >= 200 && y <= 300) 
won't be accurate for a different sized screen;

也就是说,对于另一个尺寸的屏幕,我找到的图像部分有多大的“坐标”可能会有所不同,那么我应该如何处理 xy 呢?

我不知道如何正确地解释这一点,如果最后一部分无法回答,我会理解并继续寻找!

I'm working with Javascript/jQuery and CSS to have a "background" image of a map of Oklahoma fill the screen. The idea is that the user will click a certain one of a few different "areas" of the map, and an event will fire based on which area they click.

My problem is that I need to determine where the user clicks. I've played around with using pixels, but pixels are unreliable since the dimensions are changed by the screen size. I've thought about cutting the background image into separate images and making them each their own container, but that seems unnecessarily complicated.

So, is there a way for me to determine where a user clicks on a screen? I may supply some of the code I've made and tried in a bit; right now it's all in pieces as I'm still trying different things!

Here is one thing that I tried previously:

$(document).ready(function()
{
    $("#container").click(function(e)
    {
        var x = ("#container").pageX;
        var y = ("#container").pageY;
        if(x >= ? && x <= ? && y >= ? && y <= ?) 
        {
            alert('You clicked the panhandel!');
        }
   }); 
});

I set the x and y values within the if statement requirements to the pixel values which "boxed" the area that I wanted the alert to pop up, but nothing happened.

EDIT
Thank you for your help and time, I appreciate it greatly! The last thing I'm confused about is what should go inside the if(). Since the element will BE the size of the screen, I don't think I really need offset, but

 if(x >= 200 && x <= 300 && y >= 200 && y <= 300) 

won't be accurate for a different sized screen;

That is, the "coordinates" I find for how big a section of the image is might be different for another sized screen, so what should I do about x and y?

I'm not sure how to explain this decently, and if this last part can't be answered I'll understand and keep looking!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独行侠 2024-10-28 06:36:24

http://api.jquery.com/event.pageX/http://api.jquery.com/event.pageY/

$(element).click(function(event) {
    var x = event.pageX,
        y = event.pageY;
});

http://api.jquery.com/event.pageX/ and http://api.jquery.com/event.pageY/

$(element).click(function(event) {
    var x = event.pageX,
        y = event.pageY;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文