更改自动图像滑块以链接单击滑块

时间:2013-07-27 04:07:35

标签: javascript css css3

我希望更改此图像滑块,作为一个onclick滑块。 此滑块是自动滑动。我需要通过点击链接来滑动每个图像。

DEMO check here

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

1 个答案:

答案 0 :(得分: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>