这个 JS/Jquery 方法有什么问题? (数组、点击事件)

发布于 2024-10-27 14:25:20 字数 2213 浏览 2 评论 0原文

编辑 我已经弄清楚出了什么问题。如果您想看看是否也能找到它,请随意。否则我在答案中发布了答案。

我将继续发布所有代码,因为我之前已经问过这个问题,但遗漏了我认为对于发现问题不必要的东西,但回答者想要所有的代码。

以下代码是根据我的其他一些代码建模的,这些代码工作得很好,但在这种方法中它不想工作。我有一个名为 #container 的初始 div,它是俄克拉荷马州地图的图像,在该图像上您可以看到该州的不同部分。在我的旧代码中,当用户单击片段的特定区域时,它会关闭 #container div 并打开该片段的“放大”图片的图像的 div。在我的新代码应该使我的代码更简洁之后,它不会关闭 #container (而且我怀疑不会打开新的 div)。

我通过用户单击时的鼠标位置来确定要打开哪个段 (#region)。我使用像素坐标,并且我已经完成了缩放这些坐标的数学运算,因此屏幕尺寸是什么并不重要。

现在,这是新的且有缺陷的代码:

$(document).ready(function()
{


//Main Oklahoma map
$("#container").click(function(e)
{
    var x = event.pageX;
    var y = event.pageY;

    const scaling_Screen_Max_Pixel_X = 1679;
    const scaling_Screen_Max_Pixel_Y = 924;

    // Math for the scaling of different sized windows
    var currentX = $(document).width();
    var currentY = $(document).height();
    var xScale = currentX/scaling_Screen_Max_Pixel_X;
    var yScale = currentY/scaling_Screen_Max_Pixel_Y;

    // Variables for different regions
    var zoneX = x/xScale;
    var zoneY = y/yScale;

    // Arrays containing the min and max x and y values of the rectangular area around a farm
    var minX = [47, 593, 593, 958, 600, 744, 852, 1025, 1060, 1159, 1366];
    var maxX = [553, 958, 792, 1011, 1124, 1124, 1149, 1598, 1280, 1623, 1551];
    var minY = [250, 250, 473, 349, 526, 665, 495, 248, 471, 520, 481];
    var maxY = [330, 473, 515, 478, 665, 721, 526, 471, 500, 763, 520];

    var regionNumber = [1,2,2,2,3,3,3,4,4,5,5];

    /** Loops through the values within the coordinate arrays to
        determine if the user clicked within a certain area **/
    for (var i = 0; i < minX.length; i++)
    {
        if(zoneX >= minX[i] && zoneX <= maxX[i] && zoneY >= minY[i] && zoneY <= maxY[i]) 
        {
            $("#region"+region[i]).toggle();
            $("#container").toggle(); //toggle off
        }
    }
   }); 
 });

minX、maxX 等数组中的坐标是按顺序排列的,因此这四个数组中的每个数组中的第一个元素属于第一个区域 (#region1)。我拥有 regionNumber 数组的原因是因为我对某些区域有多个“矩形”坐标集,因为这些区域不是完美的矩形。

正如我之前所说,当我单击某个区域时什么也不会发生。你明白这里的问题是什么吗?

EDIT I've figured out what was wrong. If you want to see if you can find it too, feel free. Otherwise I posted the answer in the answers.

I'll go ahead and post all of the code since I've asked this question before but left out the things I thought were unnecessary to finding the problem, but the answerers wanted all of the code.

The following code is modeled after some other code of mine that works perfectly fine, but in this method it does not want to work. I have an initial div called #container which is an image of an Oklahoma map, and on that image you see different segments of the state. In my old code, when a user clicks in certain area of a segment, it would toggle the #container div off and toggle on a div of an image of a 'zoomed in' picture of that segment. After my new code which is supposed to make my code more concise, it will not toggle the #container off (and I suspect not toggle the new div on).

I determine which segment (#region) to turn on by using mouse position when the user clicks. I use pixel coordinates, and I have done the math to scale those coordinates so it doesn't matter what the screen size is.

And now, here is the new and buggy code:

$(document).ready(function()
{


//Main Oklahoma map
$("#container").click(function(e)
{
    var x = event.pageX;
    var y = event.pageY;

    const scaling_Screen_Max_Pixel_X = 1679;
    const scaling_Screen_Max_Pixel_Y = 924;

    // Math for the scaling of different sized windows
    var currentX = $(document).width();
    var currentY = $(document).height();
    var xScale = currentX/scaling_Screen_Max_Pixel_X;
    var yScale = currentY/scaling_Screen_Max_Pixel_Y;

    // Variables for different regions
    var zoneX = x/xScale;
    var zoneY = y/yScale;

    // Arrays containing the min and max x and y values of the rectangular area around a farm
    var minX = [47, 593, 593, 958, 600, 744, 852, 1025, 1060, 1159, 1366];
    var maxX = [553, 958, 792, 1011, 1124, 1124, 1149, 1598, 1280, 1623, 1551];
    var minY = [250, 250, 473, 349, 526, 665, 495, 248, 471, 520, 481];
    var maxY = [330, 473, 515, 478, 665, 721, 526, 471, 500, 763, 520];

    var regionNumber = [1,2,2,2,3,3,3,4,4,5,5];

    /** Loops through the values within the coordinate arrays to
        determine if the user clicked within a certain area **/
    for (var i = 0; i < minX.length; i++)
    {
        if(zoneX >= minX[i] && zoneX <= maxX[i] && zoneY >= minY[i] && zoneY <= maxY[i]) 
        {
            $("#region"+region[i]).toggle();
            $("#container").toggle(); //toggle off
        }
    }
   }); 
 });

The coordinates in the minX, maxX, etc. arrays are in order, so the first element in each of those four arrays belong to the first region (#region1). The reason I have the regionNumber array is because I have more than one "rectangle" set of coordinates for some regions because the regions are not perfectly rectangular.

As I've said before, nothing happens when I click a region. Do you see what the problem is here?

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

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

发布评论

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

评论(1

单调的奢华 2024-11-03 14:25:20

哎呀!当我坐在这里盯着我的代码时,我意识到问题出在哪里。我忘了说 $("#region"+regionNumber[i]).toggle(); 而不是 $("#region"+region[i]).toggle();

现在效果很好。谢谢大家的关注!

OOPS! While sitting here staring at my code, I realized what the problem was. I forgot to say $("#region"+regionNumber[i]).toggle(); instead of $("#region"+region[i]).toggle();.

Now it works just fine. Thanks for looking everyone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文