Jquery 定位图像时遇到问题

发布于 2024-10-16 11:53:52 字数 1137 浏览 2 评论 0原文

继续:

Javascript 在调用 Ajax 之前等待图像加载< /a>

function initResources() {

    var newImage;
    for (i = 0; i < resourceData.length; i++) {

        // Create the image
        newImage = $('<img alt="Big" id="imgA' + i + '" class="mediaImg" width="' + Math.round(resourceData[i][2] * currentScale) + '" height="' + Math.round(resourceData[i][3] * currentScale) + '" />');

        newImage.load(function() {

            alert(i);

            // Position
            $('#imgA' + i).position().top(Math.round(resourceData[i][5] * currentScale));
            $('#imgA' + i).position().left(Math.round(resourceData[i][4] * currentScale));

        });
        newImage[0].src = uploadFolder + '/' + imgData[resourceData[i][1]][1];
        $('#thePage').append(newImage);
    }
}

我有一组图像。当页面最初从服务器加载时,此函数循环遍历所有图像并将它们放置在页面上。

当页面比例发生变化时也会调用此函数。缩放功能会清除 HTML 并使用缩放倍数重新绘制所有内容。

这个函数的问题是它总是提醒resourceData.length,这是循环的结尾。我需要以某种方式将数据传递给加载函数,以便当图像最终加载时,它引用正确的 ID/i。

Follow on from:

Javascript wait for image to load before calling Ajax

function initResources() {

    var newImage;
    for (i = 0; i < resourceData.length; i++) {

        // Create the image
        newImage = $('<img alt="Big" id="imgA' + i + '" class="mediaImg" width="' + Math.round(resourceData[i][2] * currentScale) + '" height="' + Math.round(resourceData[i][3] * currentScale) + '" />');

        newImage.load(function() {

            alert(i);

            // Position
            $('#imgA' + i).position().top(Math.round(resourceData[i][5] * currentScale));
            $('#imgA' + i).position().left(Math.round(resourceData[i][4] * currentScale));

        });
        newImage[0].src = uploadFolder + '/' + imgData[resourceData[i][1]][1];
        $('#thePage').append(newImage);
    }
}

I have an array of images. When the page is loaded initially from the server, this function loops through all the images and places them on the page.

This function is also called when the scale of the page changes. The scaling function clears the HTML and redraws everything with the scale multiplier.

The problem with this function, is that it always alerts resourceData.length, which is the end of the loop. I need to somehow pass data to the load function, so that when the image does finally load, it is referencing the correct ID/i.

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

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

发布评论

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

评论(2

自演自醉 2024-10-23 11:53:52

您有一个在所有回调之间共享的 i 变量。

由于回调是异步执行的,因此当 ilength 时,它们都会在循环结束后执行。

您需要将循环体放入一个单独的函数中,该函数将 i 作为参数。
这样,每个 load 回调将使用一个单独的 i 变量(函数参数),该变量永远不会改变。

You have a single i variable which is shared among all of your callbacks.

Since the callbacks execute asynchronously, they will all execute after the loop finishes, when i is length.

You need to put the loop body into a separate function that takes i as a parameter.
This way, each load callback will use a separate i variable (the function parameter), which will never change.

好多鱼好多余 2024-10-23 11:53:52

您可以使用 $.each() 循环来创建您需要闭包(该索引是它自己的变量,而不是共享的)...以及 .position() 调用也需要一些更改,您应该使用 .css() 这里接受一个对象,如下所示:

function initResources() {
    var newImage;
    $.each(resourceData, function(i, data) {
       newImage = $('<img alt="Big" id="imgA' + i + '" class="mediaImg" width="' + Math.round(data[2] * currentScale) + '" height="' + Math.round(data[3] * currentScale) + '" />');
       newImage.load(function() {
            $(this).css({ top: Math.round(data[5] * currentScale), 
                         left: Math.round(data[4] * currentScale) });
       });
       newImage[0].src = uploadFolder + '/' + imgData[data[1]][1];
       $('#thePage').append(newImage);
    });
}

您可以进一步缩短它,但它不会更有效。保留在我的 .load() 事件处理程序中可以使用 this 引用处理程序所在的元素,无需再次选择它。

You can use a $.each() loop to create the closure (into which that index is it's own variable, not shared) you need...and the .position() call needs a bit of change as well, you should use .css() here which takes an object, like this:

function initResources() {
    var newImage;
    $.each(resourceData, function(i, data) {
       newImage = $('<img alt="Big" id="imgA' + i + '" class="mediaImg" width="' + Math.round(data[2] * currentScale) + '" height="' + Math.round(data[3] * currentScale) + '" />');
       newImage.load(function() {
            $(this).css({ top: Math.round(data[5] * currentScale), 
                         left: Math.round(data[4] * currentScale) });
       });
       newImage[0].src = uploadFolder + '/' + imgData[data[1]][1];
       $('#thePage').append(newImage);
    });
}

You can shorten this up more, but it'll be no more efficient. Keep in mine that inside the .load() event handler you can refer to the element the handler is for with this, no need to select it again.

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