图像预加载代码出现问题

Having trouble with image preload code

本文关键字:问题 代码 加载 图像      更新时间:2023-09-26

我有一个对象构造函数,它有一个用于预加载的预加载方法滚动图像对。

所以,我有两个问题:

1:为什么警报对话框只执行"STR:"而没有附加任何数据?(这种类型的问题通常是由于我的失明

2:是否可以将this.button_on和this.buttons_off视为其中的对象,而不是

数字索引,使用sting索引,这样滚动事件处理程序就不需要循环通过button_on和button_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...

这是加载事件触发之前的调用

  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();

感谢您的时间和关注。

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();
}