如何添加到这个javascript图像旋转器的链接?

时间:2022-04-30 07:15:31

Here is the current, working, javascript image rotator. Believe it was originally created with Dreamweaver. Inherited project.

这是当前的,工作的javascript图像旋转器。相信它最初是用Dreamweaver创建的。遗传工程。

Trying to figure out how to add links to the below function. There are only 3 images that are being used and each needs an individual link. Thanks in advance!

试图找出如何添加到下面函数的链接。只有3个图像被使用,每个需要一个单独的链接。提前谢谢!

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2]; x.onclick=function(){window.location.href=a[i+3]}; }
}
// Comma separated list of images to rotate 
var imgs = new Array('images/swap1.jpg','images/swap2.jpg','images/swap3.jpg');
imgLinks = new Array('http://example.com','http://scifit.com/proseries/proseries.shtml','http://example.com');
// delay in milliseconds between image swaps 1000 = 1 second 
var delay = 8000;
var counter = 0;

function preloadImgs(){
  for(var i=0;i<imgs.length;i++){
    MM_preloadImages(imgs[i]);
  }
}

function randomImages(){
  if(counter == (imgs.length)){
    counter = 0;
  }
  MM_swapImage('rotator', '', imgs[counter++]);
  setTimeout('randomImages()', delay);
}

1 个解决方案

#1


3  

UPDATED ANSWER: Please use this code instead. I've re-written my original answer, because there were a few problems with it. Replace the entire Dreamweaver inserted code with this:

更新后的答案:请使用此代码。我重新写了我最初的答案,因为它有一些问题。替换整个Dreamweaver插入的代码:

function MM_preloadImages() { //v3.0
    var d=document;
    if(d.images){
        if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
        for(i=0; i<a.length; i++)
            if (a[i].indexOf("#")!=0){
                d.MM_p[j]=new Image;
                d.MM_p[j++].src=a[i];
            }
    }
}

function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr;
    for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
    var p,i,x;
    if(!d) d=document;
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document;
        n=n.substring(0,p);
    }
    if(!(x=d[n])&&d.all) x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n);
    return x;
}

function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments;
    document.MM_sr=new Array;
    for(i=0;i<(a.length-2);i+=3) {
        console.log(a);
        if ((x=MM_findObj(a[i]))!=null){
            document.MM_sr[j++]=x;
            if(!x.oSrc) x.oSrc=x.src;
            x.src=a[i+2];
            x.onclick=function(){
                window.location.href=a[i];
            };            
        }
    }
}
// Comma separated list of images to rotate 
var imgs = new Array('images/swap1.jpg','images/swap2.jpg','images/swap3.jpg');
imgLinks = new Array('http://scifit.com/proseries/proseries.shtml','http://scifit.com/proseries/proseries.shtml','http://scifit.com/proseries/proseries.shtml');
// delay in milliseconds between image swaps 1000 = 1 second 
var delay = 8000;
var counter = 0;

function preloadImgs(){
    for(var i=0;i<imgs.length;i++){
        MM_preloadImages(imgs[i]);
    }
}

function randomImages(){
    if(counter == (imgs.length)){
        counter = -1;
    }
    counter++;
    MM_swapImage('rotator', '', imgs[counter], imgLinks[counter]);
    setTimeout('randomImages()', delay);
}

Then, in your BODY tag, you'll likely see something like this:

然后,在你的BODY标签中,你可能会看到这样的东西:

<body onload="preloadImgs() ...

<身体onload = " preloadimgs()……< p>

Change that to this:

变化:

<body onload="preloadImgs();randomImages();">

<身体onload = " preloadimgs();randomimages();">

That will force the first call to randomImages, which causes the initial image to be bound by the work done in MM_swapImage. The reason you were getting undefined URLs is because in this line of my code:

这将迫使第一个调用随机图像,这会导致初始图像被MM_swapImage中的工作绑定。你得到未定义url的原因是因为在我的代码中:

x.onclick=function(){ ...

x.onclick = function(){…

I was calling for a[i+3] when I should have just called for a[i].

当我应该打电话给我的时候,我正在打电话找一个[I +3]。


By no means is this a great answer from a semantical standpoint, but it works.

从语义的角度来看,这绝不是一个伟大的答案,但它确实有效。

#1


3  

UPDATED ANSWER: Please use this code instead. I've re-written my original answer, because there were a few problems with it. Replace the entire Dreamweaver inserted code with this:

更新后的答案:请使用此代码。我重新写了我最初的答案,因为它有一些问题。替换整个Dreamweaver插入的代码:

function MM_preloadImages() { //v3.0
    var d=document;
    if(d.images){
        if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
        for(i=0; i<a.length; i++)
            if (a[i].indexOf("#")!=0){
                d.MM_p[j]=new Image;
                d.MM_p[j++].src=a[i];
            }
    }
}

function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr;
    for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
    var p,i,x;
    if(!d) d=document;
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document;
        n=n.substring(0,p);
    }
    if(!(x=d[n])&&d.all) x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n);
    return x;
}

function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments;
    document.MM_sr=new Array;
    for(i=0;i<(a.length-2);i+=3) {
        console.log(a);
        if ((x=MM_findObj(a[i]))!=null){
            document.MM_sr[j++]=x;
            if(!x.oSrc) x.oSrc=x.src;
            x.src=a[i+2];
            x.onclick=function(){
                window.location.href=a[i];
            };            
        }
    }
}
// Comma separated list of images to rotate 
var imgs = new Array('images/swap1.jpg','images/swap2.jpg','images/swap3.jpg');
imgLinks = new Array('http://scifit.com/proseries/proseries.shtml','http://scifit.com/proseries/proseries.shtml','http://scifit.com/proseries/proseries.shtml');
// delay in milliseconds between image swaps 1000 = 1 second 
var delay = 8000;
var counter = 0;

function preloadImgs(){
    for(var i=0;i<imgs.length;i++){
        MM_preloadImages(imgs[i]);
    }
}

function randomImages(){
    if(counter == (imgs.length)){
        counter = -1;
    }
    counter++;
    MM_swapImage('rotator', '', imgs[counter], imgLinks[counter]);
    setTimeout('randomImages()', delay);
}

Then, in your BODY tag, you'll likely see something like this:

然后,在你的BODY标签中,你可能会看到这样的东西:

<body onload="preloadImgs() ...

<身体onload = " preloadimgs()……< p>

Change that to this:

变化:

<body onload="preloadImgs();randomImages();">

<身体onload = " preloadimgs();randomimages();">

That will force the first call to randomImages, which causes the initial image to be bound by the work done in MM_swapImage. The reason you were getting undefined URLs is because in this line of my code:

这将迫使第一个调用随机图像,这会导致初始图像被MM_swapImage中的工作绑定。你得到未定义url的原因是因为在我的代码中:

x.onclick=function(){ ...

x.onclick = function(){…

I was calling for a[i+3] when I should have just called for a[i].

当我应该打电话给我的时候,我正在打电话找一个[I +3]。


By no means is this a great answer from a semantical standpoint, but it works.

从语义的角度来看,这绝不是一个伟大的答案,但它确实有效。