我有一个问题,我无法修复在javascript中创建图像轮播。我使用的jCarousel有几个“哈克”的修改,以允许具有不同宽度(纵向/横向) 最终的结果是我使用一些代码,看起来像这样javascript:等待代码中使用的图像加载
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
}
现在基本图像,这个问题似乎是因为图像尚未加载,“img.width”最终为0。这意味着图库中的所有图像的宽度都为0,因此不可见。刷新页面(因此所有的图像都在缓存中),一切正常,“img.width”是正确的值。
设置图像加载警报后强制暂停允许img.width成为正确的值,但显然这是一个可怕的解决方法。 任何人都可以提供一种允许JavaScript加载图像,并获得其大小正确的方式,然后继续脚本?
预先感谢所有帮助
感谢您的解释,我想我找到基纳得到它足够! – 2010-08-12 05:57:48