Jquery 定位图像时遇到问题
继续:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有一个在所有回调之间共享的
i
变量。由于回调是异步执行的,因此当
i
为length
时,它们都会在循环结束后执行。您需要将循环体放入一个单独的函数中,该函数将
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
islength
.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 separatei
variable (the function parameter), which will never change.您可以使用
$.each()
循环来创建您需要闭包(该索引是它自己的变量,而不是共享的)...以及.position()
调用也需要一些更改,您应该使用.css()
这里接受一个对象,如下所示:您可以进一步缩短它,但它不会更有效。保留在我的
.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: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 withthis
, no need to select it again.