第二张幻灯片不起作用

时间:2017-11-07 07:30:54

标签: javascript ecmascript-6 es6-class

我的滑块有问题,有意义的是图片只会改变一次。 我知道只有下一个按钮wokrs,但没关系,我认为每个功能都是必需的。

我的代码:

class Karuzela {

  constructor() {
    this.next = $('.next');
    this.prev = $('.prev')
    this.current = $('.active');
    this.nextImg = this.current.next();
    this.events();
  }
  events() {
    this.next.click(this.nextSlide.bind(this));

  }
  nextSlide() {
    if (this.nextImg.length) {

      this.current.removeClass('active').css('z-index', -10);
      this.nextImg.addClass('active').css('z-index', 10);
    }

  }

}

var karuzela = new Karuzela
body {
  font-family: "Arial", sans-serif;
  font-size: 14px;
  color: #fff;
  background: #333;
}

a {
  color: #fff;
  text-decoration: none;
}

h1 {
  text-align: center;
}

.container {
  width: 540px;
  margin: 40px auto;
  overflow: auto;
}

.slider-inner {
  width: 500px;
  height: 300px;
  position: relative;
  overflow: hidden;
  float: left;
  padding: 3px;
  border: #666 solid 1px;
}

.slider-inner img {
  display: none;
  width: 500px;
  height: 300px;
}

.slider-inner img.active {
  display: inline-block;
}

.prev,
.next {
  float: left;
  margin-top: 130px;
  cursor: pointer;
}

.prev {
  position: relative;
  margin-right: -45px;
  z-index: 100;
}

.next {
  position: relative;
  margin-left: -45px;
  z-index: 100;
}
<div class="container">
  <h1>JQSlider</h1>
  <div class="slider-outer">
    <img src="images/arrow-left.png" class="prev" alt="Prev">
    <div class="slider-inner">
      <img src="http://s3.amazonaws.com/factmag-images/wp-content/uploads/2015/06/apple-music-150615-616x440.jpg" class="active">
      <img src="http://podcast.iu.edu/upload/IUSEUITS/images/300x300.gif">
      <img src="https://pbs.twimg.com/profile_images/847437647186243584/U1Ie4x2d.jpg">
    </div>
    <img src="images/arrow-right.png" class="next" alt="Next">
  </div>
  <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</div>

请解释并更正我的代码。

感谢。

1 个答案:

答案 0 :(得分:0)

每次前进时都需要将当前电流提升到下一张图像:

&#13;
&#13;
class Karuzela {

  constructor() {
    this.next = $('.next');
    this.prev = $('.prev')
    this.current = $('.active');
    this.events();
  }
  events() {
    this.next.click(this.nextSlide.bind(this));

  }
  nextSlide() {
    var next = this.current.next(); // get the next
    
    if (next.length) {
      this.current.removeClass('active').css('z-index', -10);
      next.addClass('active').css('z-index', 10);
      
      this.current = next; // change current to next
    }

  }

}

var karuzela = new Karuzela
&#13;
body {
  font-family: "Arial", sans-serif;
  font-size: 14px;
  color: #fff;
  background: #333;
}

a {
  color: #fff;
  text-decoration: none;
}

h1 {
  text-align: center;
}

.container {
  width: 540px;
  margin: 40px auto;
  overflow: auto;
}

.slider-inner {
  width: 500px;
  height: 300px;
  position: relative;
  overflow: hidden;
  float: left;
  padding: 3px;
  border: #666 solid 1px;
}

.slider-inner img {
  display: none;
  width: 500px;
  height: 300px;
}

.slider-inner img.active {
  display: inline-block;
}

.prev,
.next {
  float: left;
  margin-top: 130px;
  cursor: pointer;
}

.prev {
  position: relative;
  margin-right: -45px;
  z-index: 100;
}

.next {
  position: relative;
  margin-left: -45px;
  z-index: 100;
}
&#13;
<div class="container">
  <h1>JQSlider</h1>
  <div class="slider-outer">
    <img src="images/arrow-left.png" class="prev" alt="Prev">
    <div class="slider-inner">
      <img src="http://s3.amazonaws.com/factmag-images/wp-content/uploads/2015/06/apple-music-150615-616x440.jpg" class="active">
      <img src="http://podcast.iu.edu/upload/IUSEUITS/images/300x300.gif">
      <img src="https://pbs.twimg.com/profile_images/847437647186243584/U1Ie4x2d.jpg">
    </div>
    <img src="images/arrow-right.png" class="next" alt="Next">
  </div>
  <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</div>
&#13;
&#13;
&#13;