how to transition between images in a slider

和自甴很熟 提交于 2019-12-04 20:41:12

I was able to solve it!

  

let line = document.getElementById("line");

line.addEventListener("input", function(event){
  setNewImage(event.target.value);
});

function setNewImage(value){
  // console.log(value);
  let currentImage = document.getElementsByClassName("playing");
  let removedImage = currentImage[0].classList.remove("playing");
  let imageToAdd = "image"+value;
  // console.log(imageToAdd);
  
  let getElToAdd = document.getElementsByClassName(imageToAdd);
  
  // console.log(getElToAdd);
  
  let newEl = getElToAdd[0];
  
 newEl.classList.add("playing");
}
.container {
  display: flex;
  justify-content: center;
  flex-direction: column;
  background-color: lavendar;
  width: 400px;
  height: 300px;
}

.image-container {
  width: 380px;
  height: 280px;
/*   background-color: pink; */
}

.scrollbar {
/*   padding: 0 5px 5px 0; */
}
.scrollbar input {
  width: 380px;
}

ul li {
  list-style: none;
}

.image {
  width: 380px;
  height: 260px;
  display: none;
}

.playing {
  display: block;
}
.image1 {
  background: url('http://placekitten.com/380/260') no-repeat;
}

.image2 {
  background: url('http://placekitten.com/378/260') no-repeat;
}

.image3 {
  background: url('http://placekitten.com/380/259') no-repeat;
}

.image4 {
  background: url('http://placekitten.com/379/260') no-repeat;
}

.image5 {
  background: url('http://placekitten.com/383/260') no-repeat;
}

.image6 {
  background: url('http://placekitten.com/380/261') no-repeat;
}
<div class="container">
  <div class="image-container">
    <ul>
      <li><img class="playing image image1" /></li>
      <li><img class="image image2" /></li>
      <li ><img class="image image3" /></li>
      <li><img class="image image4" /></img></li>
      <li><img class="image image5" /></li>
      <li><img class="image image6"/></li>
    </ul>
  </div>
  <div class="scrollbar">
    <input id="line" type="range" min=1 max =6 />
  </div>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!