【问题标题】:how to add subtle transiton between images?如何在图像之间添加微妙的过渡?
【发布时间】:2018-04-27 22:23:41
【问题描述】:

我创建了this 问题并且能够让我的滑块工作。现在我需要在图像之间添加平滑过渡。我在 CSS 中尝试了过渡/动画,但效果很奇怪 - 图像之间存在黑色。我认为过渡属性需要添加到 JS 中。

现状 - 由于图像突然变化,存在明显差异 期望 - 我希望图像可以轻松进出。

非常感谢这方面的任何帮助!

附: -> 在这里复制粘贴代码以方便使用:

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>

【问题讨论】:

    标签: javascript css css-animations


    【解决方案1】:

    使用opacity 而不是display 可以添加过渡,并使您的元素位置绝对以使它们彼此之上:

    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;
    }
    
    
    .scrollbar input {
      width: 380px;
    }
    
    ul {
     position:relative; /*make them relative to ul not li*/
     margin:0;
     padding:0;
    }
    /* added this*/
    ul li .image {
      position:absolute;
      top:0;
      left:0;
    }
    /**/
    ul li {
      list-style: none;
    }
    
    .image {
      width: 380px;
      height: 260px;
      opacity: 0;
      transition:1s all;
    }
    
    .playing {
      opacity:1;
    }
    
    .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>
            <div class="playing image image1"></div>
          </li>
          <li>
            <div class="image image2"></div>
          </li>
          <li>
            <div class="image image3"></div>
          </li>
          <li>
            <div class="image image4"></div>
          </li>
          <li>
            <div class="image image5"></div>
          </li>
          <li>
            <div class="image image6"></div>
          </li>
        </ul>
      </div>
      <div class="scrollbar">
        <input id="line" type="range" min=1 max=6>
      </div>
    </div>

    【讨论】:

    • 这很完美!非常感谢 Temani :)。只是一件事,如果我有更多的图像,并且尺寸更大 - 我希望动画更容易看 - 有点像,一个像素一个像素地转换成另一个图像。我该怎么做?
    • @novembersky 这样的事情不是你可以用 CSS 轻易做到的,你需要更复杂的代码。你可以轻松做到的就是淡入淡出、滑动等,但是逐个像素地改变更复杂
    猜你喜欢
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多