简体   繁体   English

将过渡CSS添加到图像滑块

[英]Add transition css to image slider

I have a slider with 3 images and 3 buttons which change the current image 'src' attribute (and hence change the current image), but now I want to add a smooth transition when I change the image and I would like to get this using css transitions. 我有一个带有3个图像和3个按钮的滑块,它们可以更改当前图像的“ src”属性(并因此更改当前图像),但是现在我想在更改图像时添加一个平滑过渡,我想使用CSS转换。

So when I click on any bullet I need the current image fades out and then the new image fades in. how can I do this? 因此,当我单击任何项​​目符号时,我需要淡出当前图像,然后淡入新图像。我该怎么做?

 var listItemContainer = document.getElementById('carousel-index'); var imageChanger = document.getElementById('image-container').getElementsByTagName('img'); var bulletNumber; for (i = 0; i < listItemContainer.children.length; i++){ (function(index){ listItemContainer.children[i].onclick = function(){ bulletNumber = index; imageChanger[0].setAttribute('src', 'https://civilian-interviewe.000webhostapp.com/img/mini_slider_' + (bulletNumber+1) + '.png'); } })(i); }; 
 body{ text-align:center; } #carousel-index{ margin:0; padding:0; } #carousel-index li { display: inline-block; width: 2em; height: 2em; border-radius: 100%; background-color: #666; cursor: pointer; } 
 <div id="image-container"> <img src="https://civilian-interviewe.000webhostapp.com/img/mini_slider_1.png"/> <ul id="carousel-index"> <li></li> <li></li> <li></li> </ul> </div> 

Here is a CODEPEN 这是一个CODEPEN

PD: I want to do this without Jquery. PD:我想在没有Jquery的情况下执行此操作。

CodePen sample CodePen示例

I've added some css transitions to the css 我在CSS中添加了一些CSS过渡

div#image-container {
opacity:1;
-webkit-transition: opacity 1s;
-moz-transition: opacity 1s;     
transition: opacity 1s; 

}

div#image-container.fade {
opacity:0;
}

and some function to handle the event: 和一些处理事件的函数:

var image = document.getElementById('image-container');
  if(image.className === 'fade'){
    image.className = '';
    setTimeout(function(){
      image.className = 'fade';
    },1000)
  }else{
    image.className = 'fade';
    setTimeout(function(){
      image.className = '';
    },1000)
  }
  setTimeout(function(){
    bulletNumber = index;
 imageChanger[0].setAttribute('src', 'https://civilian-interviewe.000webhostapp.com/img/mini_slider_' + (bulletNumber+1) + '.png');   
  },1000);

use CSS3 animation with add class in javascript 在CSS中将CSS3动画与添加类一起使用

 var listItemContainer = document.getElementById('carousel-index'); var imageChanger = document.getElementById('image-container').getElementsByTagName('img'); var bulletNumber; for (i = 0; i < listItemContainer.children.length; i++) { (function(index) { listItemContainer.children[i].onclick = function() { bulletNumber = index; imageChanger[0].className = "hide"; setTimeout(function(){ imageChanger[0].setAttribute('src', 'https://civilian-interviewe.000webhostapp.com/img/mini_slider_' + (bulletNumber + 1) + '.png'); },501); setTimeout(function(){ imageChanger[0].className = "show"; }, 1001); } })(i); }; 
 body { text-align: center; } #carousel-index { margin: 0; padding: 0; } #carousel-index li { display: inline-block; width: 2em; height: 2em; border-radius: 100%; background-color: #666; cursor: pointer; } #image-container img.show { animation: show .5s; animation-fill-mode: both; } @keyframes show { from { transform:scale(0.7); opacity:0 } to { transform: scale(1); opacity:1 } } #image-container img.hide { animation: hide .5s; animation-fill-mode: both; } @keyframes hide { from { transform:scale(1); opacity:1 } to { transform:scale(0.7); opacity:0 } } 
 <div id="image-container"> <img src="https://civilian-interviewe.000webhostapp.com/img/mini_slider_1.png" /> <ul id="carousel-index"> <li></li> <li></li> <li></li> </ul> </div> 

You can use CSS transition, and i guess that desired property is opacity. 您可以使用CSS过渡,我想所需的属性是不透明度。

 var listItemContainer = document.getElementById('carousel-index'); var imageChanger = document.getElementById('image-container').getElementsByTagName('img'); var bulletNumber; imageChanger[0].classList.add('fadeIn'); for (i = 0; i < listItemContainer.children.length; i++){ (function(index){ listItemContainer.children[i].onclick = function(){ bulletNumber = index; imageChanger[0].classList.remove('fadeIn'); setTimeout(function(){ imageChanger[0].classList.add('fadeIn'); } , 100); imageChanger[0].setAttribute('src', 'https://civilian-interviewe.000webhostapp.com/img/mini_slider_' + (bulletNumber+1) + '.png'); } })(i); }; 
 body{ text-align:center; } #carousel-index{ margin:0; padding:0; } #carousel-index li { display: inline-block; width: 2em; height: 2em; border-radius: 100%; background-color: #666; cursor: pointer; } img { opacity:0; } img.fadeIn { opacity:1; transition:opacity 0.5s ease; } 
 <div id="image-container"> <img src="https://civilian-interviewe.000webhostapp.com/img/mini_slider_1.png"/> <ul id="carousel-index"> <li></li> <li></li> <li></li> </ul> </div> 

Start image opacity should be 0, of course: 当然,开始图像的不透明度应该为0:

img {
  opacity:0;
}
img.fadeIn {
  opacity:1;
  transition:opacity 0.5s ease;
}

And then, on click (remove added class -> set opacity to 0 again), and add it again. 然后,单击(删除添加的类->再次将不透明度设置为0),然后再次添加它。 You can play with values to get desired effect. 您可以使用值来获得想要的效果。

EDIT: fadeOut/fadeIn... it was little tricky, because of one container, and img src changing, but additional timeout solves it: 编辑:fadeOut / fadeIn ...有点棘手,因为一个容器和img src更改,但是额外的超时解决了它:

 var listItemContainer = document.getElementById('carousel-index'); var imageChanger = document.getElementById('image-container').getElementsByTagName('img'); var bulletNumber; imageChanger[0].classList.add('fadeIn'); for (i = 0; i < listItemContainer.children.length; i++){ (function(index){ listItemContainer.children[i].onclick = function(){ bulletNumber = index; imageChanger[0].classList.remove('fadeIn'); imageChanger[0].classList.add('fadeOut'); setTimeout(function(){ imageChanger[0].classList.add('fadeIn'); imageChanger[0].classList.remove('fadeOut'); } , 1000); setTimeout(function(){ imageChanger[0].setAttribute('src', 'https://civilian-interviewe.000webhostapp.com/img/mini_slider_' + (bulletNumber+1) + '.png'); } , 1000); } })(i); }; 
 body{ text-align:center; } #carousel-index{ margin:0; padding:0; } #carousel-index li { display: inline-block; width: 2em; height: 2em; border-radius: 100%; background-color: #666; cursor: pointer; } img { opacity:0; } img.fadeIn { opacity:1; transition:opacity 0.5s ease; } img.fadeOut { opacity:0; transition:opacity 0.5s ease; } 
 <div id="image-container"> <img src="https://civilian-interviewe.000webhostapp.com/img/mini_slider_1.png"/> <ul id="carousel-index"> <li></li> <li></li> <li></li> </ul> </div> 

PS Images should be probably preloaded, in order to all work fine on first load. PS映像可能应该预先加载,以便在首次加载时一切正常。

One more alternative, JS based, didn't change HTML or CSS (explanation as comments in the code) : 另一种替代方法,基于JS,没有更改HTML或CSS (解释为代码中的注释)

 var listItemContainer = document.getElementById('carousel-index'); var imageChanger = document.getElementById('image-container').getElementsByTagName('img')[0]; var newSrc, fadeDelta=-0.01; //don't change 'delta', change 'fadeoutDelay' and 'fadeinDelay' (function initImageChanger(i,count){ imageChanger.style.opacity = 1; //set opacity in JS, otherwise the value returns "" (empty) listItemContainer.children[i].onclick = function(){ var fadeoutDelay=5, fadeinDelay=15, opacity=parseFloat(imageChanger.style.opacity); //change delays to alter fade-speed function changeSrc(){ var src = imageChanger.getAttribute('src'); var ext = src.substring(src.lastIndexOf('.')); //store extension src = src.substring(0,src.lastIndexOf('_')+1); //store source up to the identifying number return src+i+ext; //combine parts into full source } function fade(delay){ imageChanger.style.opacity = (opacity+=fadeDelta); if (fadeDelta<0 && opacity<=0){ //fade-out complete imageChanger.setAttribute('src',newSrc); fadeDelta*=-1, delay=fadeinDelay; //invert fade-direction } else if (fadeDelta>0 && opacity>=1){newSrc=null, fadeDelta*=-1; return;} //fade-in complete, stop function setTimeout(function(){fade(delay);},delay); } //start fade, but only if image isn't already fading, otherwise only change source (and reset) if (changeSrc() != imageChanger.getAttribute('src')){ newSrc=changeSrc(); if (opacity==0 || opacity==1){fade(fadeoutDelay);} else if (fadeDelta>0){fadeDelta *= -1;} //reset fade for new source } }; if (++i < count){initImageChanger(i,count);} //iterate to next element })(0,listItemContainer.children.length); //supply start-arguments 
 body {text-align:center;} #image-container img {width:auto; height:150px;} #carousel-index {margin:0; padding:0;} #carousel-index li {display:inline-block; width:2em; height:2em; border-radius:100%; background-color:#666; cursor:pointer;} 
 <div id="image-container"> <img src="https://civilian-interviewe.000webhostapp.com/img/mini_slider_1.png"/> <ul id="carousel-index"><li></li><li></li><li></li></ul> </div> 
codepen: http://codepen.io/anon/pen/xgwBre?editors=0010 codepen: http ://codepen.io/anon/pen/xgwBre?editors=0010

It's not a perfect solution, but here's one way of doing it without jQuery: 这不是一个完美的解决方案,但这是不使用jQuery的一种方法:

First create a new function: 首先创建一个新函数:

function fadeChange(element) {
    var op = 0.1;
    var timer = setInterval(function () {
        if (op >= 1){
            clearInterval(timer);
        }
        element.style.opacity = op;
        element.style.filter = 'alpha(opacity=' + op * 100 + ")";
        op += op * 0.1;
    }, 10);
}

Then call that function when setting the new image: 然后在设置新图像时调用该函数:

fadeChange(imageChanger[0]);

This is demonstrated through the updated codepen here . 这是通过此处更新的codepen演示的。

It's a bit clunky, but does fade the images. 它有点笨拙,但确实会使图像褪色。 You may want to consider using a single image for the monitor, and then simply changing the contents of the monitor through this method. 您可能要考虑为监视器使用单个图像,然后通过此方法简单地更改监视器的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM