我有一個問題,我無法修復在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