how to transition between images in a slider

。_饼干妹妹 提交于 2020-01-13 07:10:28

问题


I have a simple image slider with input range. I want to pick input range value and be able to fade-in and fade out images.

The problem is - my setup has to remain this way. I need to have img tags within li and need to have images as css background url under classnames.

How do i identify the current playing and transition to the next depending on where the slider is?

Requirement: If user goes to range 2 on slider, image2 should be visible. if user goes to range 4, image4 should be visible and so on.

I have been able to read the input range and locate the image with that image class.

How do I remove the "active" state of previous one and insert this new image?

Please find code attached

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

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

function setNewImage(value){
  let currentImage = document.getElementsByClassName("playing");
  let newImageClassName = "image"+value;
}
.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>

I am getting the input range value. Help from here is greatly appreciated! Thanks!


回答1:


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>


来源:https://stackoverflow.com/questions/50066119/how-to-transition-between-images-in-a-slider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!