2014-08-28 30 views
0

我有這個對象構造函數,它具有用於預加載 翻轉圖像對的預加載方法。遇到圖像預加載代碼問題

所以,我有兩個問題:

1:爲什麼是警告對話框只是在做「STR:」不附加任何數據? (這種類型的問題通常是由於我的失明

2:是有可能治療this.buttons_on和this.buttons_off作爲對象中,代替

數字索引,使用刺索引所以側翻事件處理程序不通過 需要循環的buttons_on和buttons_off陣列得到應該被換出的一個;

function _NAV() 
    { 
     this.list_off = []; 
     this.list_on = []; 
     this.buttons_on = []; 
     this.buttons_off = []; 
     this.buttons_all = {}; // .on and .off 
     this.button_events = {}; 
     this.img = true; 
     this.img_ids = {} 
     this.preLoad = function() 
        { 
        if(document.images) //creates image object array for preload. 
         { 
         var STR = ''; 
         for(var i = 0; i < list_off.length; i++) 
          { 
          var lab_on = list_on[i].replace('\.jpg', ''); 
          var lab_off = list_off[i].replace('\.jpg', ''); 
          STR += lab_on+'||'+lab_off+"\n"; 
          this.buttons_on[i] = new Image(); 
          this.buttons_on[i].src = srcPath+list_on[i]; 
          this.bottons_on[i].id = img_ids[i]; 
          this.buttons_off[i] = new Image(); 
          this.buttons_off[i].src = srcPath+list_off[i]; 
          this.buttons_off[i].id = img_ids[i]; 
          } 
         alert("STR: "+STR); 
         } 
        else 
         { 
         this.img = false 
         } 
        } 
     //// ...etc... 

這裏是onload事件觸發之前調用

var rollover = new _NAV(); 
    rollover.preLoad(); 

下面是使用

var srcPath = '../nav_buttons/'; 
var list_off = new Array(); // not new Object; 
list_off[0] = "bio_off.jpg"; 
list_off[1] = "cd_off.jpg"; 
list_off[2] = "home_off.jpg"; 
list_off[3] = "inst_off.jpg"; 
list_off[4] = "photo_off.jpg"; 
list_off[5] = "rev_off.jpg"; 
list_off[6] = "samp_off.jpg"; 


var list_on = new Array(); 
list_on[0] = "bio_on.jpg"; 
list_on[1] = "cd_on.jpg"; 
list_on[2] = "home_on.jpg"; 
list_on[3] = "inst_on.jpg"; 
list_on[4] = "photo_on.jpg"; 
list_on[5] = "rev_on.jpg"; 
list_on[6] = "samp_on.jpg"; 

var img_ids = new Array(); 

感謝的時間和注意力的陣列。

+0

'list_on [i]'?想想看。應該是'this.list_on [i]',但是你什麼時候在該陣列中放置任何東西? – PHPglue 2014-08-28 01:57:53

回答

0

1:

嘗試PHPGlue的建議,並在您的所有成員變量(this.list_onthis.list_offthis.img_ids

您也可以在一行上一個錯字前加this.bottons_on拼寫錯誤。

this.bottons_on[i].id = img_ids[i]; 

2:

是的,你可以使用一個字符串作爲索引。只需製作buttons_onbuttons_off對象而不是數組。

function _NAV() 
{ 
    this.buttons_on = {}; 
    this.buttons_off = {}; 
    // For example: 
    this.buttons_off[lab_off] = new Image(); 
}