【问题标题】:Add links to images in array在数组中添加指向图像的链接
【发布时间】:2015-01-17 02:05:18
【问题描述】:

我正在尝试将链接添加到我的 JS 数组中的图像,以便在单击时将您定向到信息页面。

我尝试使用以下代码,但图像不显示。

var WorkArray = new Array(['work/01.png',"http://www.stackoverflow.com"],
                         ['work/02.png',"http://www.stackoverflow.com"],
                         ['work/03.png',"http://www.stackoverflow.com"],
                         ['work/04.png',"http://www.stackoverflow.com"]);

如果我删除图片显示的链接。

我正在使用此代码的变体:http://bouncingdvdlogo.com/

这是我的完整代码:

var width = 400;
var height = 400;
var swoosh = 4;
var stopafter=0; //set time in seconds before image disappears. Use 0 for never

var maxswoosh = 50;
var xMax;
var yMax;
var xPos = 0;
var yPos = 0;
var xDir = 'right';
var yDir = 'down';
var screenSavouring = true;
var tempswoosh;
var newXDir;
var newYDir;

function setupShit() {
    if (document.all) {
        xMax = document.body.clientWidth
        yMax = document.body.clientHeight
        document.all("work").style.visibility = "visible";
        }
    else if (document.layers||document.getElementById) {
        xMax = window.innerWidth-0;
        yMax = window.innerHeight;
        if (document.getElementById)
        document.getElementById("work").style.visibility="visible"
        else
        document.layers["work"].visibility = "show";
    }
    setTimeout('moveIt()',500);
    }


function moveIt() {
if (screenSavouring == true) {
calculatePosition();
if (document.all) {
document.all("work").style.left = xPos + document.body.scrollLeft;
document.all("work").style.top = yPos + document.body.scrollTop;
}
else if (document.layers) {
document.layers["work"].left = xPos + pageXOffset;
document.layers["work"].top = yPos + pageYOffset;
}
else if (document.getElementById) {
document.getElementById("work").style.left = xPos + pageXOffset;
document.getElementById("work").style.top = yPos + pageYOffset;
}
doit=setTimeout('moveIt()',30);
}
}

function calculatePosition() {
    if (xDir == "right") {
        if (xPos > (xMax - width - swoosh)) { 
        xDir = "left";
        cC();
        }
    }
    else if (xDir == "left") {
        if (xPos < (0 + swoosh)) {
        xDir = "right";
        cC();
        }
    }
    if (yDir == "down") {
        if (yPos > (yMax - height - swoosh)) {
        yDir = "up";
        cC();
        }
    }
    else if (yDir == "up") {
        if (yPos < (0 + swoosh)) {
        yDir = "down";
        cC();
        }
    }
    if (xDir == "right") {
        xPos = xPos + swoosh;
        }
    else if (xDir == "left") {
        xPos = xPos - swoosh;
        }
    else {
        xPos = xPos;
        }
    if (yDir == "down") {
        yPos = yPos + swoosh;
        }
    else if (yDir == "up") {
        yPos = yPos - swoosh;
        }
    else {
        yPos = yPos;
        }
    }

if (document.all||document.layers||document.getElementById){
window.onload = setupShit;
window.onresize = new Function("window.location.reload()");
}

var WorkArray = new Array(['work/01.png',"http://www.stackoverflow.com", "Your text goes here"],
                             ['work/02.png',"http://www.stackoverflow.com", "Your text goes here"],
                             ['work/03.png',"http://www.stackoverflow.com", "Your text goes here"],
                             ['work/04.png',"http://www.stackoverflow.com", "Your text goes here"]);


var nelements = WorkArray.length;

preload_image_object = new Image();
    var i = 0;
    for(i=0; i<=nelements; i++)  {
    preload_image_object.src = WorkArray[i];
    }

var currentImage = 0

function cC() {
    currentImage = (currentImage + 1) % WorkArray.length;
    document.getElementById("work").style.backgroundImage="url('"+WorkArray[currentImage]+"')";
}

谢谢!

【问题讨论】:

  • 这是一个嵌套数组。使用.src = WorkArray[i][0]
  • @abhitalks 你不是说“嵌套数组”吗?
  • 感谢@DarkAshelin。已更正。
  • @abhitalks,您为什么不将其发布为答案?
  • @Victor:不值得。也许只是操作的一种类型。他一定是错过了。

标签: javascript arrays image url hyperlink


【解决方案1】:

尝试用下面替换你的 for 循环

for(i=0; i<nelements; i++)  {
preload_image_object.src = WorkArray[i][0];
}

还将函数 cC() 替换为以下内容:

function cC() {
    var nelements = WorkArray.length;
    var rdmnum = Math.floor(Math.random() * nelements);
    var currentImage = rdmnum++;
    if (currentImage == nelements) {
        rdmnum = Math.floor(Math.random() * nelements);
    }
    currentImage = (currentImage + 1) % WorkArray.length;
    document.getElementById("work").style.backgroundImage = "url('" + WorkArray[currentImage][0] + "')";
}

【讨论】:

  • 你改过i
  • 是的,我做到了。我真的只是 JS 的菜鸟,所以可能还缺少另一部分......
  • 试试我在上面的答案中添加的 cC() 函数
  • 没有成功...当我在 WorkArray[currentImage] 之后添加 [0] 时,图像再次消失。如果您想查看我们的网站,请访问我们的网站:holymothership.be
  • 我已经使用了另一种解决方案。我不能给所有图像一个单独的链接,所以我用 jquery 做了一个解决方法。当我使用您提供的代码时,图像不会显示。
猜你喜欢
  • 2016-07-27
  • 1970-01-01
  • 2014-10-18
  • 2016-06-21
  • 1970-01-01
  • 2020-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多