jquery - 添加幻灯片按钮 - jquery

标签 jquery css slideshow

我这里有这个非常简单的幻灯片:http://jsfiddle.net/Jtec5/2/
代码如下:
HTML:

<div id="slideshow">
    <div>
        <img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
    </div>
    <div>
        <img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
    </div>
    <div>
        <img src="http://gillespaquette.ca/images/stack-icon.png">
    </div>
</div>
<ul></ul>

CSS:

#slideshow {
    margin: 50px auto;
    position: relative;
    width: 240px;
    height: 240px;
    padding: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}
#slideshow > div {
    position: absolute;
    top: 10px;
    left: 10px;
    right: 10px;
    bottom: 10px;
}

#slideshow img {
    max-width:240px;
    max-height:240px;
}

ul {
    list-style:none;
    margin:0px;
    padding:0px;
}
ul li {
    float:left;
    border-radius:10px;
    width:10px;
    height:10px;
    border:1px solid white;
    background:grey;
}
ul li.active {
    background:black;
}

J查询:

$("#slideshow > div:gt(0)").hide();

var index = 1;
var maxindex = $('#slideshow > div').length;

setInterval(function () {
    $('#slideshow > div:first')
        .fadeOut(1000)
        .next()
        .fadeIn(1000)
        .end()
        .appendTo('#slideshow');
    $('ul li').removeClass('active');
    $('ul li:eq(' + index + ')').addClass('active');
    index = index < maxindex - 1 ? index + 1 : 0;
}, 3000);

for (var i = 0; i < maxindex; i++) {
    $('ul').append('<li class="' + (i == 0 ? 'active' : '') + '"></li>');
}

你可以在幻灯片中看到有一些按钮告诉我我在哪张照片中,我试图将每个按钮与其照片相关联所以当我点击它时它会带我到它的照片,如果更改整个代码需要按钮会更好,因为我不喜欢我的代码,我认为它太长了。

最佳答案

我还制作了一个 fiddle ,希望可以满足您的需求。 http://jsfiddle.net/L7s4e/ 主要代码如下:

  $("#slideshow > div:gt(0)").hide();

  index = 0;
  maxIndex = $('#slideshow > div').length;

  $mainDiv = $('#slideshow div');
  for (var i = 0; i < maxIndex; i++) {
      var $li = $("<li/>", {
          "data-index": i,
          "class": (i == 0 ? 'active' : '')
      }).on("click",

      function () {
          /* uncomment below if you want slideshow to stop on selection
    to restart call startSlideshow();*/
          //clearInterval(intervalId);
          var imageIndex = $(this).data("index");
          $mainDiv.eq(index).fadeOut(1000);
          $mainDiv.eq(imageIndex).fadeIn(1000);
          index = imageIndex;
          $('ul li').removeClass('active');
          $('ul li:eq(' + index + ')').addClass('active');
      });

      $('ul').append($li);
  }

  function startSlideshow() {

      intervalId = setInterval(function () {
          $('#slideshow > div').eq(index).fadeOut(1000);
          index = index < maxIndex - 1 ? index + 1 : 0;
          $('#slideshow > div').eq(index).fadeIn(1000);
          $('ul li').removeClass('active');
          $('ul li:eq(' + index + ')').addClass('active');
      }, 3000);
  }

  startSlideshow();

希望它能如您所愿。

关于jquery - 添加幻灯片按钮 - jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18275327/

相关文章:

jquery - 在循环迭代时添加 CSS 类,JQuery

html - 纯全屏 CSS 幻灯片,对背景幻灯片元素上的置换元素具有双重淡入/淡出效果

javascript匹配()错误

JavaScript:如何从 keyup 事件中获取发送者输入元素的值?

javascript - 我想要有可以搜索我的图像的链接

html - 父 div 背景的透明度

css - 从 MUI 设计 DatePicker 的样式

javascript - 非常短的 jQuery 图像幻灯片

javascript - 如何实现幻灯片过渡效果?

javascript - 使用 jQuery 定位动态 HTML