【问题标题】:the second slide doesn't work第二张幻灯片不起作用
【发布时间】:2018-04-19 12:27:02
【问题描述】:

我的滑块有问题,意思是图片只更改一次。 我只知道下一个按钮 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>

请解释并更正我的代码。

谢谢。

【问题讨论】:

    标签: javascript ecmascript-6 es6-class


    【解决方案1】:

    每次前进时都需要将当前图像前进到下一个图像:

    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
    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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 2021-10-12
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      相关资源
      最近更新 更多