适应简单的滑块

时间:2016-05-10 08:09:24

标签: javascript jquery html css

我正在使用这个非常基本的滑块,它可以正常工作 - 但是当页面上有多个滑块时,它们会在单击右侧或左侧按钮时同时滑动。

我如何让每个人都独一无二?

感谢给予的任何帮助

谢谢

可在此处找到工作示例 - > https://codepen.io/zuraizm/pen/vGDHl< -

jQuery(document).ready(function ($) {
  $('#checkbox').change(function(){
    setInterval(function () {
      moveRight();
    }, 3000);
  });

  var slideCount = $('#slider ul li').length;
  var slideWidth = $('#slider ul li').width();
  var slideHeight = $('#slider ul li').height();
  var sliderUlWidth = slideCount * slideWidth;

  $('#slider').css({ width: slideWidth, height: slideHeight });
  $('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });
  $('#slider ul li:last-child').prependTo('#slider ul');

  function moveLeft() {
    $('#slider ul').animate({
      left: + slideWidth
    }, 200, function () {
      $('#slider ul li:last-child').prependTo('#slider ul');
      $('#slider ul').css('left', '');
    });
  };

  function moveRight() {
    $('#slider ul').animate({
      left: - slideWidth
    }, 200, function () {
      $('#slider ul li:first-child').appendTo('#slider ul');
      $('#slider ul').css('left', '');
    });
  };

  $('a.control_prev').click(function () {
    moveLeft();
  });

  $('a.control_next').click(function () {
    moveRight();
  });

});
#slider {
  position: relative;
  overflow: hidden;
  margin: 20px auto 0 auto;
  border-radius: 4px;
  width: 100% !important;
  height: auto !important;
}

#slider ul {
  position: relative;
  margin: 0;
  padding: 0;
  height: 200px;
  list-style: none;
}

#slider ul li {
  position: relative;
  display: block;
  float: left;
  margin: 0;
  padding: 0;
  width: auto;
  height: auto;
  background: #ccc;
  text-align: center;
  line-height: 300px;
}

a.control_prev, a.control_next, a.control_prev_1, a.control_next_1 {
  position: absolute;
  top: 40%;
  z-index: 999;
  display: block;
  padding: 4% 3%;
  width: auto;
  height: auto;
  background: #2a2a2a;
  color: #fff;
  text-decoration: none;
  font-weight: 600;
  font-size: 18px;
  opacity: 0.8;
  cursor: pointer;
}

a.control_prev:hover, a.control_prev_1:hover, a.control_next:hover, a.control_next_1:hover {
  opacity: 1;
  -webkit-transition: all 0.2s ease;
}

a.control_prev, a.control_prev_1 {
  border-radius: 0 2px 2px 0;
}

a.control_next, a.control_next_1 {
  right: 0;
  border-radius: 2px 0 0 2px;
}

.slider_option {
  position: relative;
  width: 100%;
  margin: 10px auto;
}

.slider_option > label {
  padding-top: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>


<div id="slider">
  <a href="javascript:void(0);" class="control_next">>></a>
  <a href="javascript:void(0);" class="control_prev"><</a>
    <ul>
      <li><img src="//farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg" alt="" class="img-responsive" /></li>
      <li><img src="//farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg" alt="" class="img-responsive" /></li>
      <li><img src="//farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg" alt="" class="img-responsive" /></li>
      <li><img src="//farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg" alt="" class="img-responsive" /></li>
    </ul>  
    </div>

  <div class="slider_option">
    <input type="checkbox" id="checkbox">
    <label for="checkbox">Autoplay Slider</label>
  </div>

1 个答案:

答案 0 :(得分:0)

所有代码都基于HTML ID | Column_Name | Column_Value 属性来解决元素,这些元素应该对页面上的单个元素是唯一的。

由于您已经在使用jQuery,我建议您继续阅读将函数结构化为plugin的一部分,该{{3}}可以应用于页面上的多个元素。