简体   繁体   English

图片 Slider 淡入 Animation

[英]Image Slider Fade In Animation

I'm working on an image slider for one of my project and I'm stuck here - every time I click on the thumbnail image the main image has to appear with some fade-in animation. Do know where I'm doing wrong.我正在为我的一个项目处理图像 slider,但我被困在这里 - 每次我单击缩略图图像时,主图像都必须出现一些淡入 animation。知道我哪里做错了。 First time when the page loads then the class "fadein" is being applied but after it stays intact the fadein doesnt work.第一次加载页面时,将应用 class“淡入淡出”,但在它保持完好无损之后,淡入淡出不起作用。 Any clue?有什么线索吗?

HTML HTML

 var el = document.getElementById("ImageSlider"); //var removefadein = document.querySelector(".fade-in"); function imgSlider(newimage) { el.src = newimage; //el.classList.add("fade-in"); } //el.delay('5000').classList.remove("fade-in"); //removefadein.classList.remove("fade-in");
 * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Nunito', sans-serif; font-size: 18px; } h1, h2, h3, h4, h5, h6 { font-family: 'Noto Serif', serif; } section { position: relative; width: 100%; height: 100vh; display: flex; justify-content: center; align-items: center; } section #ImageSlider { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; } section ul.thumbnails { position: absolute; bottom: 50px; left: 50%; display: flex; transform: translateX(-50%); justify-content: center; align-items: center; list-style: none; z-index: 999; } section ul.thumbnails li { margin: 1em; cursor: pointer; } section ul.thumbnails li img { border: 2px solid black; max-height: 5em; padding: 0; margin: 0; }.fade-in { animation: fadein 4s; -moz-animation: fadein 4s; /* Firefox */ -webkit-animation: fadein 4s; /* Safari and Chrome */ -o-animation: fadein 4s; /* Opera */ } @keyframes fadein { from { opacity:0; } to { opacity:1; } }
 <section> <img class="fade-in" src="https://via.placeholder.com/1980" alt="Image Title" id="ImageSlider"> <ul class="thumbnails"> <li onclick="imgSlider('https://via.placeholder.com/1980')"><img src="https://via.placeholder.com/150" alt="Thumb 1"></li> <li onclick="imgSlider('https://via.placeholder.com/1980/ff3256')"><img src="https://via.placeholder.com/150/ff3256" alt="Thumb 2"></li> </ul> </section>

Not quite sure about logic behind this, but here is a working fix: maybe someone else can explain it better.不太确定这背后的逻辑,但这是一个有效的解决方法:也许其他人可以更好地解释它。

fix logic is that you have to on click:修复逻辑是你必须点击:

  1. get element again再次获取元素
  2. check opacity, if its bigger then 0 set it back to 0检查不透明度,如果它大于 0,则将其设置回 0
  3. remove fade class and re-apply it with slight delay (it can not be in same time as removing to take effect)删除 fade class 并稍微延迟重新应用它(它不能与删除同时生效)

I think your problem is that you have visible element from 0-1 on load.我认为你的问题是你在加载时有 0-1 的可见元素。 Then you didn't set it back to 0, your animation class does not do that, it just say to animate from 0 to 1, but there was no starting opacity 0 to start from.然后你没有将它设置回 0,你的 animation class 没有这样做,它只是说从 0 到 1 设置动画,但是没有起始不透明度 0 开始。

 var el = document.getElementById("ImageSlider"); function imgSlider(newimage) { el = document.getElementById("ImageSlider"); el.src = newimage; console.clear(); console.log(window.getComputedStyle(el).opacity); if (window.getComputedStyle(el).opacity > 0) { console.log(true); el.style.opacity = 0; el.classList.remove("fade-in"); setTimeout(function() { el.classList.add("fade-in"); el.style.opacity = 1; }, 100); } }
 * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Nunito', sans-serif; font-size: 18px; } h1, h2, h3, h4, h5, h6 { font-family: 'Noto Serif', serif; } section { position: relative; width: 100%; height: 100vh; display: flex; justify-content: center; align-items: center; } section #ImageSlider { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; } section ul.thumbnails { position: absolute; bottom: 50px; left: 50%; display: flex; transform: translateX(-50%); justify-content: center; align-items: center; list-style: none; z-index: 999; } section ul.thumbnails li { margin: 1em; cursor: pointer; } section ul.thumbnails li img { border: 2px solid black; max-height: 5em; padding: 0; margin: 0; }.fade-in { animation: fadein 4s; -moz-animation: fadein 4s; /* Firefox */ -webkit-animation: fadein 4s; /* Safari and Chrome */ -o-animation: fadein 4s; /* Opera */ } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
 <section> <img class="fade-in" src="https://via.placeholder.com/1980" alt="Image Title" id="ImageSlider"> <ul class="thumbnails"> <li onclick="imgSlider('https://via.placeholder.com/1980')"><img src="https://via.placeholder.com/150" alt="Thumb 1"></li> <li onclick="imgSlider('https://via.placeholder.com/1980/ff3256')"><img src="https://via.placeholder.com/150/ff3256" alt="Thumb 2"></li> </ul> </section>

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

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