gpt4 book ai didi

javascript - 如何延迟 JavaScript 代码,直到所有图像都加载到内存中

转载 作者:行者123 更新时间:2023-11-28 20:30:00 25 4
gpt4 key购买 nike

我没有 JavaScript 知识。我正在尝试延迟 $('#image').reel代码直到加载图像,以便我可以获得图像宽度值。

   stitched: imgwidth,
loops: false,
frames: 30,
frame: 1,
wheelable: false
// speed: 0,
// timeout: 1
});

function imageReel() {
$('#image').empty();
var image = '<img id="image" src=\'images/' + arrReel[imgcount] + '.png\' width="1000" height= "700" />';
$('#image-wrapper').html(image);
$('#image').reel({
stitched: imgwidth,
loops: false,
frames: 30,
frame: 1,
wheelable: false
// speed: 0,
// timeout: 1
});
console.log(imgwidth);
}

基于第一个答案的尝试 1

 $('#image').empty();
// var image = '<img id="image" src=\'images/'+arrReel[imgcount]+'.png\' width="1000" height= "700" />';

$(function(){
image=new Image();
image = '<img id="image" src=\'images/'+arrReel[imgcount]+'.png\' width="1000" height= "700" />';

image.onload=function(){
$('#image-wrapper').html(image);
$('#image').reel({

stitched: imgwidth,
loops: false,
frames: 30,
frame: 1,
wheelable: false,
// speed: 0,
// timeout: 1
});
};
$('body').append(image);
});

结果。仍然给我 0 宽度加上创建多个图像

基于 kilian 回答的尝试 2

 <div id="image-wrapper"><img id="image" src='images/outsidedark.png' width="1000" height= "700" onload="someFunction()" /></div>

function someFunction()
{
console.log(imgwidth);
$('#image').reel({

// stitched: 3208,

stitched: imgwidth,
loops: false,
frames: 30,
frame: 1,
wheelable: false,
// speed: 0,
// timeout: 1
});


}

结果 图像不断加载。可能是因为插件的原因,但我认为我需要 onload 仅加载该函数一次并加载多次。宽度仍然是 0,并且由于控制台日志,0 不断显示,就像陷入无限循环一样。

最佳答案

不确定什么是 reel jquery 插件(是插件吗?)...但是下面的代码显示了真实的图像宽度和高度。

$(function(){
var image=new Image();
image.src='http://www.google.com/images/srpr/logo4w.png';

image.onload=function(){
console.log($(this).height());
console.log($(this).width());
};
$('body').append(image);
});

我希望这会有所帮助。

关于javascript - 如何延迟 JavaScript 代码,直到所有图像都加载到内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16608923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com