-3

我找到了我想使用的这个脚本,但不知道如何将 href 添加到图像。

脚本的位置

或者如果有人有更好的方法或解决方案。
我认为通过 div 标签选择图像的方法是一种好方法,但是如何呢?

编辑:
根据要求以供将来参考,这里是代码

BounceImage.js

// INITIALIZATION:

imagesDetails = new Array();
bouncingImages2 = new Object();

// CONFIGURATION:

imagesDetails[0] = new Object();
imagesDetails[0].width = 50;
imagesDetails[0].height = 50;
imagesDetails[0].src = 'logo1xMINI.jpg';

imagesDetails[1] = new Object();
imagesDetails[1].width = 40;
imagesDetails[1].height = 40;
imagesDetails[1].src = 'superball.gif';

imagesDetails[2] = new Object();
imagesDetails[2].width = 60;
imagesDetails[2].height = 50;
imagesDetails[2].src = "1.png";

// Template for further images:

   // imagesDetails[3] = new Object();
   // imagesDetails[3].width = ?;
   // imagesDetails[3].height = ?;
   // imagesDetails[3].src = '?';

bouncingImages2.frameRate = 30;
bouncingImages2.minRandomSpeed = 2;
bouncingImages2.maxRandomSpeed = 8;

// MAIN:

bouncingImages2.isLoaded = false;

var winWidth, winHeight;

if (dyn) {
   for (var imageLoop = 0; imageLoop < imagesDetails.length; imageLoop++) imagesDetails[imageLoop].imgObj = loadImg(imagesDetails[imageLoop].src); 
   }

function getWindowDimensions() {
   winWidth = getWinWidth();
   winHeight = getWinHeight();
   }

function loadBouncingImages2() {
   if (dyn && !bouncingImages2.isLoaded) {
      getWindowDimensions();
      for (var layerLoop = 0; layerLoop < imagesDetails.length; layerLoop++) {
         imagesDetails[layerLoop].dirX = (Math.round(Math.random()) == 0) ? 'left' : 'right';
         imagesDetails[layerLoop].dirY = (Math.round(Math.random()) == 0) ? 'up' : 'down';
         imagesDetails[layerLoop].posX = Math.floor(Math.random() * (winWidth - imagesDetails[layerLoop].width - 1)) + getDocScrollLeft();
         imagesDetails[layerLoop].posY = Math.floor(Math.random() * (winHeight - imagesDetails[layerLoop].height - 1)) + getDocScrollTop();
         imagesDetails[layerLoop].speedX = Math.round(Math.random() * (bouncingImages2.maxRandomSpeed - bouncingImages2.minRandomSpeed)) + bouncingImages2.minRandomSpeed;
         imagesDetails[layerLoop].speedY = Math.round(Math.random() * (bouncingImages2.maxRandomSpeed - bouncingImages2.minRandomSpeed)) + bouncingImages2.minRandomSpeed;
         imagesDetails[layerLoop].layerObj = addLayer('bouncingImagesLyr' + layerLoop);
         setLayerSize(imagesDetails[layerLoop].layerObj,imagesDetails[layerLoop].width,imagesDetails[layerLoop].height);
         setLayerClip(imagesDetails[layerLoop].layerObj,0,imagesDetails[layerLoop].width,imagesDetails[layerLoop].height,0);
         setLayerHTML(imagesDetails[layerLoop].layerObj,getImgTag('bouncingImagesImg' + layerLoop,imagesDetails[layerLoop].src,imagesDetails[layerLoop].width,imagesDetails[layerLoop].height,0));
         moveLayerTo(imagesDetails[layerLoop].layerObj,imagesDetails[layerLoop].posX,imagesDetails[layerLoop].posY);
         showLayer(imagesDetails[layerLoop].layerObj);
         }
      bouncingImages2.isLoaded = true;
      moveBouncingImages();
      }
   }

function moveBouncingImages() {
   for (var layerLoop = 0; layerLoop < imagesDetails.length; layerLoop++) {
      if (imagesDetails[layerLoop].dirX == 'left') {
         if (imagesDetails[layerLoop].posX > imagesDetails[layerLoop].speedX) imagesDetails[layerLoop].posX -= imagesDetails[layerLoop].speedX;
         else {
            imagesDetails[layerLoop].dirX = 'right';
            imagesDetails[layerLoop].posX = 0;
            }
         }
      else if (imagesDetails[layerLoop].dirX == 'right') {
         if (imagesDetails[layerLoop].posX + imagesDetails[layerLoop].width < winWidth - imagesDetails[layerLoop].speedX) imagesDetails[layerLoop].posX += imagesDetails[layerLoop].speedX;
         else {
            imagesDetails[layerLoop].dirX = 'left';
            imagesDetails[layerLoop].posX = winWidth - imagesDetails[layerLoop].width;
            }
         }
      if (imagesDetails[layerLoop].dirY == 'up') {
         if (imagesDetails[layerLoop].posY > imagesDetails[layerLoop].speedY) imagesDetails[layerLoop].posY -= imagesDetails[layerLoop].speedY;
         else {
            imagesDetails[layerLoop].dirY = 'down';
            imagesDetails[layerLoop].posY = 0;
            }
         }
      else if (imagesDetails[layerLoop].dirY == 'down') {
         if (imagesDetails[layerLoop].posY + imagesDetails[layerLoop].height < winHeight -     imagesDetails[layerLoop].speedY) imagesDetails[layerLoop].posY += imagesDetails[layerLoop].speedY;
         else {
            imagesDetails[layerLoop].dirY = 'up';
            imagesDetails[layerLoop].posY = winHeight - imagesDetails[layerLoop].height;
            }
         }
      }
   for (var layerLoop = 0; layerLoop < imagesDetails.length; layerLoop++) moveLayerTo(imagesDetails    [layerLoop].layerObj,imagesDetails[layerLoop].posX + getDocScrollLeft(),imagesDetails    [layerLoop].posY + getDocScrollTop());
   window.setTimeout('moveBouncingImages()',bouncingImages2.frameRate);
   }

window.onresize = getWindowDimensions;
window.onload = loadBouncingImages2;

lib.js 在这里太长了

在正文标签中

<body onLoad="javascript:loadBouncingImages2();">
4

2 回答 2

1

似乎是一个微不足道的旧式突兀脚本。

一般解决方案

在函数loadBouncingImages2()中查找setLayerHTML()调用位置并将其第二个参数更改为您需要的任何 HTML 字符串。

可以改成'<a href="...">'+getImgTag(...)+'</a>'.

通用解决方案

将 setLayerHTML 调用替换为

var img = getImgTag('bouncingImagesImg' + layerLoop,imagesDetails[layerLoop].src,imagesDetails[layerLoop].width,imagesDetails[layerLoop].height,0);
setLayerHTML(imagesDetails[layerLoop].layerObj,'<a href="'+imagesDetails[layerLoop].href+'">'+img+'</a>');

之后,只需通过添加href到 imageDetails 来修改配置,例如

imagesDetails[0].href = 'http://www.google.com';
于 2013-06-16T15:15:55.790 回答
0

所有图像的容器都id以开头,bouncingImagesLyr并且 imagesname属性以开头bouncingImagesImg- 您可以使用任何此信息获取它们,然后将类添加到它们。

于 2013-06-16T15:30:09.157 回答