[英]function is too fast?
This function actually works, but I don't think it's a good solution.这个功能确实有效,但我认为这不是一个好的解决方案。 If I do not add setTimeout, the function does not work well.如果我不添加 setTimeout,该功能将无法正常工作。 (It only loads maybe half of the pictures). (它可能只加载一半的图片)。 Is there a problem that I do not use "i" within the for-loop?是否存在我在 for 循环中不使用“i”的问题?
And is it a "good" solution to load the <img>
tags into a hidden "load" div to work with them and sort them into 3 different divs?将<img>
标签加载到隐藏的“加载”div 中以使用它们并将它们分类为 3 个不同的 div 是一个“好”的解决方案吗?
function loadpictures() {
$('#load').empty();
$('#picTop').empty();
$('#picMiddle').empty();
$('#picBottom').empty();
$('#load').load('pages/bilder.html .' + category, function() {
LineW[0] = LineW[1] = LineW[2] = 0;
for (var i = 0; i < $('#load img').length; i++) {
window.setTimeout(function() {
var shortLine = 0;
for (var j = 1; j < 3; j++) {
if (LineW[j] < LineW[shortLine]) {
shortLine = j;
}
}
switch (shortLine) {
case 0:
$('#picTop').prepend($('#load img')[0]);
LineW[0] += $('#picTop img')[0].offsetWidth;
break;
case 1:
$('#picMiddle').prepend($('#load img')[0]);
LineW[1] += $('#picMiddle img')[0].offsetWidth;
break;
case 2:
$('#picBottom').prepend($('#load img')[0]);
LineW[2] += $('#picBottom img')[0].offsetWidth;
break;
}
}, 20);
}
}); }); } }
This example waits for images to load. 此示例等待图像加载。 If you want to refactor it, I would suggest getting an array of image paths then using his/her function.如果你想重构它,我建议获取一组图像路径,然后使用他/她的函数。
function loadImage(path, width, height, target) {
$('<img src="'+ path +'">').load(function() {
$(this).width(width).height(height).appendTo(target);
});
}
I replaced the for-loop with the .each jquery function now it works perfect for me :) Thy anyways!我用 .each jquery 函数替换了 for 循环,现在它对我来说很完美:) 无论如何!
$('#load').load('pages/bilder.html .'+category, function(){
$('#load img').each(function () {
var shortLine = 0;
for(var j = 1; j < 3; j++){
if(LineW[j] < LineW[shortLine]){
shortLine = j;
}
}
switch (shortLine) {
case 0:
$('#picTop').prepend($('#load img')[0]);
LineW[0] += $('#picTop img')[0].offsetWidth +5;
break;
case 1:
$('#picMiddle').prepend($('#load img')[0]);
LineW[1] += $('#picMiddle img')[0].offsetWidth +5;
break;
case 2:
$('#picBottom').prepend($('#load img')[0]);
LineW[2] += $('#picBottom img')[0].offsetWidth +5;
break;
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.