Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript:等待加载代码中使用的图像_Javascript - Fatal编程技术网

javascript:等待加载代码中使用的图像

javascript:等待加载代码中使用的图像,javascript,Javascript,我在用javascript创建图像旋转木马时遇到了一个无法修复的问题。我使用jcarousel进行了一些“黑客”修改,以允许使用不同宽度的图像(纵向/横向) 最终的结果是我使用了一些类似这样的代码 function mycarousel_itemAddCallback(carousel, first, last, dir) { var thumbdir = 'gallery/' + dir + '/thumbs/' //set the thumbs dir var fulldir

我在用javascript创建图像旋转木马时遇到了一个无法修复的问题。我使用jcarousel进行了一些“黑客”修改,以允许使用不同宽度的图像(纵向/横向) 最终的结果是我使用了一些类似这样的代码

function mycarousel_itemAddCallback(carousel, first, last, dir)
{
    var thumbdir = 'gallery/' + dir + '/thumbs/' //set the thumbs dir
    var fulldir = 'gallery/' + dir + '/full/' //set the full images dir
    for (i = 0; i < 21; i++) {
        var no = i+1; //number image we are on
        var img = new Image(); //make a new image
        img.src = thumbdir + no + '.jpg'; //set its source
        var fullimg = fulldir + no + '.jpg'; //set full omages source as well
        var html = mycarousel_getItemHTML(img.src, fullimg, img.width); //make some html for the image, with width
        carousel.add(i+1, html, img.width); //add the image to the carousel, with width passed in
        tagID = 'jcarousel-item-' + no; //get the css tag of the image just added
        changeStyle(tagID, img.width); // force its height in inline css to prevent bugs
    }
函数mycarousel\u itemAddCallback(carousel,first,last,dir)
{
var thumbdir='gallery/'+dir+'/thumbs/'//设置thumbs dir
var fulldir='gallery/'+dir+'/full/'//设置完整图像目录
对于(i=0;i<21;i++){
var no=i+1;//我们所处的数字图像
var img=new Image();//创建新图像
img.src=thumbdir+no+'.jpg';//设置其源
var fullimg=fulldir+no+'.jpg';//同时设置完整的omages源代码
var html=mycarousel_getItemHTML(img.src,fullimg,img.width);//为图像创建一些html,宽度为
add(i+1,html,img.width);//将图像添加到旋转木马中,并传入宽度
tagID='jcarousel item-'+no;//获取刚刚添加的图像的css标记
changeStyle(tagID,img.width);//在内联css中强制其高度以防止bug
    }
现在本质上,问题似乎是“img.width”最终为0,因为图像尚未加载。这意味着库中所有图像的宽度均为0,因此不可见。刷新页面(使所有图像都已在缓存中),然后使用“img.width”的正确值,一切正常

将图像设置为加载警报后强制暂停可以使img.width成为正确的值,但显然这是一个可怕的解决方法。 在继续脚本之前,有人能建议一种允许javascript加载图像并获得正确大小的方法吗


提前感谢所有帮助

使用
onload
事件如下:

var img = new Image();
img.onload = function(){
  // image has been loaded
};

// proper way to assign any src right now
img.src = 'path there';

alert(img.width);

像这样使用
onload
事件:

var img = new Image();
img.onload = function(){
  // image has been loaded
};

// proper way to assign any src right now
img.src = 'path there';

alert(img.width);

您需要使用闭包将变量传递到函数中:

function foo(x,y)
{
  //do stuff
}


for( var i = 0 ; i < 9 ; i++ )
{    
    var img = new Image();
    img.onload = (function(a,b){ return function(){ foo(a,b) }; })(i,someVar);
    img.src = "bar.jpg";
}
函数foo(x,y) { //做事 } 对于(变量i=0;i<9;i++) { var img=新图像(); img.onload=(函数(a,b){return function(){foo(a,b)};})(i,someVar); img.src=“bar.jpg”; } 嵌套函数的工作方式如下:外部函数在圆括号中,因为我们声明它,然后一次性调用它,所以(i,someVar)作为(a,b)传递给函数。该函数反过来返回指向不接受任何参数的新的第二个函数的指针。 第二个函数中的Wihtin是调用foo的一行代码,其中(a,b)在调用时被设置为当前值i和someVar。当foo最终被调用(通过onload)时,它的x和y参数就是i和someVar

没有人真正理解这些,我们只是假装理解:-)


您可以将奇怪的嵌套函数包装到其他帮助器函数中,使其不那么奇怪。有关我的看法,请参见,

您需要使用闭包将变量传递到函数中:

function foo(x,y)
{
  //do stuff
}


for( var i = 0 ; i < 9 ; i++ )
{    
    var img = new Image();
    img.onload = (function(a,b){ return function(){ foo(a,b) }; })(i,someVar);
    img.src = "bar.jpg";
}
函数foo(x,y) { //做事 } 对于(变量i=0;i<9;i++) { var img=新图像(); img.onload=(函数(a,b){return function(){foo(a,b)};})(i,someVar); img.src=“bar.jpg”; } 嵌套函数的工作方式如下:外部函数在圆括号中,因为我们声明它,然后一次性调用它,所以(i,someVar)作为(a,b)传递给函数。该函数反过来返回指向不接受任何参数的新的第二个函数的指针。 第二个函数中的Wihtin是调用foo的一行代码,其中(a,b)在调用时被设置为当前值i和someVar。当foo最终被调用(通过onload)时,它的x和y参数就是i和someVar

没有人真正理解这些,我们只是假装理解:-)


您可以将奇怪的嵌套函数包装到其他帮助器函数中,使其不那么奇怪。请参阅我对此的理解,

非常感谢您的快速回答!img.onload会在源设置为您完成之前进行吗?这不会导致它等待到“空白”吗图像已加载,而不是img.src中设置的图像?这可能是一个愚蠢的问题,但您能否以正常方式将变量传递到onload函数中?因为我需要在代码中包含一些需要在onload中的变量function@Michael劳森:是的,你可以在onload函数体块中使用一个变量,在之后使用src是一个很好的实践加载。非常感谢您的快速回答!img.onload会在源设置为您完成之前进行吗?这不会导致它等待“空白”吗图像已加载,而不是img.src中设置的图像?这可能是一个愚蠢的问题,但您能否以正常方式将变量传递到onload函数中?因为我需要在代码中包含一些需要在onload中的变量function@Michael劳森:是的,你可以在onload函数体块中使用一个变量,在之后使用src是一个很好的实践负载。谢谢你的解释,我想我找的基纳够了!谢谢你的解释,我想我找的基纳够了!