0

我想更改此图像滑块,作为 onclick 滑块。这个滑块是自动滑动的。我需要通过单击链接来滑动每个图像。

演示检查这里

(function(){
  document.getElementById('slideshow').getElementsByTagName('img')[0].className = "fx";
  window.setInterval(kenBurns, 4000);       
  var images         = document.getElementById('slideshow').getElementsByTagName('img'),
      numberOfImages = images.length,
      i              = 1;
  function kenBurns() {
    if(i==numberOfImages){ i = 0;}
    images[i].className = "fx";
    if(i===0){ images[numberOfImages-2].className = "";}
    if(i===1){ images[numberOfImages-1].className = "";}
    if(i>1){ images[i-2].className = "";}
    i++;
  }
})();
4

1 回答 1

0

更改您的javascript如下:

(function(){
  document.getElementById('slideshow').getElementsByTagName('img')[0].className = "fx";

//remove the part that was auto-advancing the images on a timer

  var images         = document.getElementById('slideshow').getElementsByTagName('img'),
      numberOfImages = images.length,
      i              = 1;

//set the ken burns function to go when you click "slideshow"
  slideshow.onclick=function kenBurns() {

    if(i==numberOfImages){ i = 0;}
    images[i].className = "fx";
    if(i===0){ images[numberOfImages-2].className = "";}
    if(i===1){ images[numberOfImages-1].className = "";}
    if(i>1){ images[i-2].className = "";}
    i++;
  }
})();

编辑:

http://jsfiddle.net/enigmadan/mzRxB/4/

而不是您的 ken burns 效果代码,它仅在图像通过后才删除效果,您必须每次都删除效果。这样,无论你按什么顺序点击链接,每次都能看到效果。

  document.getElementById('slideshow').getElementsByTagName('img')[0].className = "fx";

  var images = document.getElementById('slideshow').getElementsByTagName('img');
  //no longer need i
      numberOfImages = images.length;

                                 //this variable is passed in by the button clicked.  
  window.kenBurns = function (imageNum){

             //using variable to choose image
    images[imageNum].className = "fx";

         //a for loop to remove 'fx' class from all images except the current one.
      for(var i=0;i<numberOfImages;i++){
    if(i===imageNum){ i++}
        images[i].className = "";
    }
  };

然后通过如下所示的链接调用该函数:

<a href="javascript:kenBurns(0);">link1</a>

或者,如果您喜欢onclick

<a onclick="kenBurns(1);" href="javascript:void(0);">link2</a>
于 2013-07-27T04:11:45.960 回答