简体   繁体   English

轮播 javascript

[英]Carousel javascript

How can I make the 2nd image(the bottom) stay in place till the 1st image(the active) completely finish transition in place?如何使第二张图像(底部)保持原位,直到第一张图像(活动)完全完成原位transition and then start the loop again without reordering the array?然后在不重新排序数组的情况下再次启动循环? I don't know if this is completely wrong or if I'm missing something, I've been trying for a while!我不知道这是完全错误的还是我遗漏了什么,我已经尝试了一段时间! please explain your comment/answer, thanks for helping.请解释您的评论/答案,感谢您的帮助。

 var slideIndex = 0; carousel(); function carousel() { var i; var slideShowSlide = document.querySelector('.slideshow__slide'); var slideBgFigure = Array.from(slideShowSlide.children); var slideWidth = slideBgFigure[0].getBoundingClientRect().width; for (i = 0; i < slideBgFigure.length; i++) { slideBgFigure[i].style.transform = "translateX(-" + slideWidth + "px)"; } slideIndex++; if (slideIndex > slideBgFigure.length) { slideIndex = 1; } slideBgFigure[slideIndex - 1].style.transform = "translateX(0px)"; setTimeout(carousel, 3000); };
 .slideshow__slide { overflow: hidden; height: 359px; position: relative; } .slide__bg { position: absolute; width: 100%; margin: 0; color: #fff; background: #333; overflow-x: hidden; -webkit-box-shadow: inset 0 0 0 1px #f0f0f0; box-shadow: inset 0 0 0 1px #f0f0f0; transition: all 1.5s ease; } .slide__bg img { display: block; }
 <section class="slideshow__slide slide"> <figure class="slide__bg current"> <img src="https://via.placeholder.com/620x200/070" /> </figure> <figure class="slide__bg"> <img src="https://via.placeholder.com/620x200/700" /> </figure> <figure class="slide__bg"> <img src="https://via.placeholder.com/620x200/007" /> </figure> </section>

I don't know why this question got 2 downvotes?我不知道为什么这个问题有 2 个反对票? Maybe it wasted people's time?也许它浪费了人们的时间? It wasted my whole morning too, damn...它也浪费了我整个上午,该死的...

Here might be what the OP wants to get.这可能是 OP 想要得到的。 I used CSS animation to control animation instead of javascript, setInterval instead of setTimeout.我用 CSS 动画来控制动画而不是 javascript,用 setInterval 代替 setTimeout。 I use javascript to control which slide whose the class current .我使用 javascript 来控制哪个幻灯片的类current . But it turned out the most tricky time-consuming part of me is not animation, but z-index .但事实证明,我最棘手的耗时部分不是动画,而是z-index I got a typo and it messed up too.我有一个错字,它也搞砸了。 Holy damn, took a lot of time to do this looks easy problem.该死的,花了很多时间来做这个看起来很简单的问题。


Edit: The simpler code, use CSS transition instead of animation, the javascript code just permutate the classes.编辑:更简单的代码,使用 CSS 过渡而不是动画,javascript 代码只是排列类。

 var slideShowSlide = document.querySelector('.slideshow__slide'); var slideBgFigure = Array.from(slideShowSlide.children); function permutateClassname(array) { var tmp = array[0].className; for (let i = 0; i < array.length - 1; i++) { array[i].className = array[i + 1].className; } array[array.length - 1].className = tmp; } function carousel() { permutateClassname(slideBgFigure); } setInterval(carousel, 3000);
 .slideshow__slide { overflow: hidden; height: 359px; position: relative; } .slide__bg { position: absolute; width: 100%; margin: 0; color: #fff; background: #333; overflow-x: hidden; -webkit-box-shadow: inset 0 0 0 1px #f0f0f0; box-shadow: inset 0 0 0 1px #f0f0f0; } .slide__bg img { display: block; opacity: .3; } .slide__bg { transition: transform 1.5s ease-in; } .slide__bg.slide-out { transform: translateX(-100%); z-index: 0; } .slide__bg.slide-in { transform: translateX(0); z-index: 2; } .slide__bg.middle { transform: translateX(0); z-index: 1; }
 <section class="slideshow__slide slide"> <figure class="slide__bg slide-out"> <img src="https://via.placeholder.com/620x200/070" /> </figure> <figure class="slide__bg slide-in"> <img src="https://via.placeholder.com/620x200/700" /> </figure> <figure class="slide__bg middle"> <img src="https://via.placeholder.com/620x200/007" /> </figure> </section>

Useful link: https://css-tricks.com/controlling-css-animations-transitions-javascript/有用的链接: https : //css-tricks.com/controlling-css-animations-transitions-javascript/

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

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