【问题标题】:Multiple sliders with Very Simple Slider带有非常简单滑块的多个滑块
【发布时间】:2017-11-23 23:46:13
【问题描述】:

我正在尝试将 Very Simple Slider 用于我未来的网站,但是当我想在同一页面上有多个滑块时,我遇到了问题。

当我复制 HTML/CSS/JS 中的所有属性并更改名称以创建新滑块时,它确实会创建一个新滑块,但控制箭头不再起作用。好吧,它们有效,但仅适用于一个滑块。我知道这可能是 JS 问题,但由于我不是开发人员,所以我找不到问题所在。

如果您能帮我找到解决方案 - 以及缩短 CSS 和 JS 的方法,那将非常酷,因为我将不得不通过 css modals 在同一页面上使用此滑块十次。

哦,这是我的代码。

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


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

  $('#slider2').css({
    width: slideWidth,
    height: slideHeight
  });

  $('#slider2 ul').css({
    width: sliderUlWidth,
    marginLeft: -slideWidth
  });

  $('#slider2 ul li:last-child').prependTo('#slider2 ul');

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

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

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

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

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

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

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

#slider2 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_prev2,
a.control_next2 {
  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_prev2:hover,
a.control_next2:hover {
  opacity: 1;
  -webkit-transition: all 0.2s ease;
}

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

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

.slider_option2 {
  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 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 id="slider2">
  <a href="#" class="control_next2">></a>
  <a href="#" class="control_prev2">
    <</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>

【问题讨论】:

  • 宁可使用其他幻灯片/插件...这个有严重的错误,当您尝试使用“自动播放”选项时,打开和关闭它,然后看看...有更好的,当然……

标签: javascript jquery html css slider


【解决方案1】:

您要声明 moveLeftmoveRight 两次,只需将其中一个重命名为滑块2,它就会起作用

编辑:

不推荐您使用的方法,因为每次需要在同一页面上添加新滑块时都必须更改源代码(js和css),您应该研究如何创建插件,这将帮助您学习如何创建通用滑块,同时像 slick 这样的滑块的一个很好的例子将是一个不错的选择,但是由于您正在尝试学习并想要清除错误,这就是为什么添加sn-p 并提出答案。

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


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

  $('#slider2').css({
    width: slideWidth,
    height: slideHeight
  });

  $('#slider2 ul').css({
    width: sliderUlWidth,
    marginLeft: -slideWidth
  });

  $('#slider2 ul li:last-child').prependTo('#slider2 ul');

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

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

  $('a.control_prev2').click(function() {
    moveLeft2();
  });

  $('a.control_next2').click(function() {
    moveRight2();
  });

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

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

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

#slider2 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_prev2,
a.control_next2 {
  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_prev2:hover,
a.control_next2:hover {
  opacity: 1;
  -webkit-transition: all 0.2s ease;
}

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

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

.slider_option2 {
  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 id="slider">
  <a href="#." class="control_next">&gt;</a>
  <a href="#." class="control_prev">
    &lt;</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 id="slider2">
  <a href="#." class="control_next2">&gt;</a>
  <a href="#." class="control_prev2">
    &lt;</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>

【讨论】:

  • 天哪,谢谢!我知道这听起来很愚蠢,但我已经为此疯狂了一天......谢谢!
  • 哈哈,没关系,它发生了,很高兴我能帮上忙,但你不应该使用它你应该使用一个通用滑块,它支持像这样的页面上的多个实例kenwheeler.github.io/slick
  • 这不是要走的路,复制粘贴 JS 或 CSS 代码 - 每个滑块。
  • 是的,我们应该这样做。 pata nhi kya samjtay hen ye log apnay ap ko.
  • 感谢@MuhammadOmerAslam 的建议,我正在查看滑块,它们看起来很酷很简单!
【解决方案2】:

这是一个工作示例。但您应该使其更通用,以便您可以根据需要添加多个滑块。

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

var slide2Count = $('#slider2 ul li').length;
	var slide2Width = $('#slider2 ul li').width();
	var slide2Height = $('#slider2 ul li').height();
	var slider2UlWidth = slide2Count * slide2Width;
	
	$('#slider2').css({ width: slide2Width, height: slide2Height });
	
	$('#slider2 ul').css({ width: slider2UlWidth, marginLeft: - slide2Width });
	
    $('#slider2 ul li:last-child').prependTo('#slider2 ul');

    function moveLeftS2() {
        $('#slider2 ul').animate({
            left: + slide2Width
        }, 200, function () {
            $('#slider2 ul li:last-child').prependTo('#slider2 ul');
            $('#slider2 ul').css('left', '');
        });
    };

    function moveRightS2() {
        $('#slider2 ul').animate({
            left: - slide2Width
        }, 200, function () {
            $('#slider2 ul li:first-child').appendTo('#slider2 ul');
            $('#slider2 ul').css('left', '');
        });
    };

    $('a.control_prev2').click(function () {
        moveLeftS2();
    });

    $('a.control_next2').click(function () {
        moveRightS2();
    });


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

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

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

#slider2 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_prev2,
a.control_next2 {
  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_prev2:hover,
a.control_next2:hover {
  opacity: 1;
  -webkit-transition: all 0.2s ease;
}

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

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

.slider_option2 {
  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 id="slider">
  <a href="javascript:;" class="control_next">></a>
  <a href="javascript:;" 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 id="slider2">
  <a href="javascript:;" class="control_next2">></a>
  <a href="javascript:;" class="control_prev2">
    < </a>
      <ul>
        <li>SLIDE 21</li>
        <li style="background: #aaa;">SLIDE 22</li>
        <li>SLIDE 23</li>
        <li style="background: #aaa;">SLIDE 24</li>
      </ul>
</div>

【讨论】:

  • 如果幻灯片的数量未知怎么办? (无论如何我们都不应该复制粘贴 CSS 和 JS。使用类和插件)
  • @RokoC.Buljan 它只是修复了错误。我已经提到过,你应该让它更通用、更通用,这样你就可以根据需要添加多个,而无需为每个滑块编写 css 和 js 代码
猜你喜欢
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多