复制一个 JQuery 滑块

4

首次发布者,长期读者。 我正在开发一个基本的jQuery滑块。我的问题是...制作一个只使用少量jQuery的滑块,并且可以在同一页上复制另一个滑块。

我的当前问题?让第二个滑块工作。我只是复制了代码并更改了CSS,但这不符合DRY(不重复自己)原则。

我想将代码最小化以适用于两个不同的滑块。谢谢S.O.社区!

Codepen

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

});    
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600); 

html {
  border-top: 5px solid #fff;
  background: #58DDAF;
  color: #2a2a2a;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans';
}

h1 {
  color: #fff;
  text-align: center;
  font-weight: 300;
}

#slider {
  position: relative;
  overflow: hidden;
  margin: 20px auto 0 auto;
  border-radius: 4px;
}

#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: 500px;
  height: 300px;
  background: #ccc;
  text-align: center;
  line-height: 300px;
}

a.control_prev, a.control_next {
  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_next:hover {
  opacity: 1;
  -webkit-transition: all 0.2s ease;
}

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

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

.slider_option {
  position: relative;
  margin: 10px auto;
  width: 160px;
  font-size: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Incredibly Basic Slider</h1>
<div id="slider">
  <a href="#" class="control_next"> >> </a>
  <a href="#" class="control_prev"> << </a>
  <ul>
    <li>SLIDE 1</li>
    <li style="background: #aaa;">SLIDE 2</li>
    <li>SLIDE 3</li>
    <li style="background: #aaa;">SLIDE 4</li>
  </ul>  
</div>

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

随意使用你喜欢的编辑器。

1个回答

1
你的主要问题是在动画滑块时使用了相同的ID。你需要将其泛化,以便只动画实际被选中的滑块。
我所做的更改如下:
1. 删除名为“slider”的ID,并将其替换为类。你仍然可以为滑块使用ID,但不应重复使用。 2. 在按钮的单击事件上,提供将使用父项(即滑块)动画的函数。通过这样做,你可以在moveLeft/moveRight函数中确保只动画活动的滑块。 请注意,你必须对自动播放功能采取类似的解决方案。我建议你将复选框移动到滑块DIV内部。

jQuery(document).ready(function($) {

  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) {
    
    $(slider).find('ul').animate({
        left: +slideWidth
    }, 200, function() {
        var sliderUl = $(slider).find('ul');
        $(slider).find('ul li:last-child').prependTo(sliderUl);
        $(slider).find('ul').css('left', '');
    });
    
  };

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

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

  $('a.control_next').click(function() {
      moveRight($(this).parent());
  });
  
  $('#checkbox').change(function() {
     setInterval(function() {
         moveRight();
     }, 3000);
  });

});
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600);
 html {
  border-top: 5px solid #fff;
  background: #58DDAF;
  color: #2a2a2a;
}
html,
body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans';
}
h1 {
  color: #fff;
  text-align: center;
  font-weight: 300;
}
.slider {
  position: relative;
  overflow: hidden;
  margin: 20px auto 0 auto;
  border-radius: 4px;
}
.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: 500px;
  height: 300px;
  background: #ccc;
  text-align: center;
  line-height: 300px;
}
a.control_prev,
a.control_next {
  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_next:hover {
  opacity: 1;
  -webkit-transition: all 0.2s ease;
}
a.control_prev {
  border-radius: 0 2px 2px 0;
}
a.control_next {
  right: 0;
  border-radius: 2px 0 0 2px;
}
.slider_option {
  position: relative;
  margin: 10px auto;
  width: 160px;
  font-size: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slider">
  <a href="#" class="control_next"> >> </a>
  <a href="#" class="control_prev"> << </a>
      <ul>
        <li>SLIDE 1</li>
        <li style="background: #aaa;">SLIDE 2</li>
        <li>SLIDE 3</li>
        <li style="background: #aaa;">SLIDE 4</li>
      </ul>
</div>

<div class="slider">
  <a class="control_next"> >> </a>
  <a class="control_prev"> << </a>
      <ul>
        <li>SLIDE 1</li>
        <li style="background: #aaa;">SLIDE 2</li>
        <li>SLIDE 3</li>
        <li style="background: #aaa;">SLIDE 4</li>
      </ul>
</div>

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接