简体   繁体   English

在CSS滑块上添加自动播放

[英]Add autoplay on CSS slider

I would like to add an autoplay functionality to my css slider but not sure if it is possible with the current code structure. 我想向我的css滑块添加自动播放功能,但不确定当前代码结构是否可行。

on comment i give my working link of codepan 在评论中,我给出了我的Codepan工作链接

Thanks here is css code 谢谢这里是CSS代码

I'm not exactly sure how to do this with just CSS, but it is pretty easy to do with JavaScript. 我不确定如何仅使用CSS来做到这一点,但是使用JavaScript却很容易。

let selected = 0;
const buttons = document.querySelectorAll('input');
setInterval(() => {
  buttons[(selected++) % buttons.length].click();
}, 1000);

This works for your simple example, but you may want a more specific selector for larger applications see document.querySelector . 这适用于您的简单示例,但您可能希望为大型应用程序使用更具体的选择器,请参见document.querySelector

Using an interval, you can simulate clicks on your radios... 使用间隔,您可以模拟收音机上的点击...

 var delay = 1500; var sliderRadios = document.getElementsByName("carousel-3d"); var index=0 var imageCount = sliderRadios.length; setInterval(function(){ index++; if(index>imageCount-1){ index=0; } sliderRadios[index].click(); console.log(sliderRadios[index].id); },delay); 
 .slider-container { position: relative; perspective: 350px; transform-style: preserve-3d; } .carousel-3d-item { position: absolute; top: 50%; left: 40%; outline: 1px solid transparent; } #carousel-3d-controller-2 ~ .carousel-3d-item:nth-of-type(1), #carousel-3d-controller-1 ~ .carousel-3d-item:nth-of-type(1), #carousel-3d-controller-2 ~ .carousel-3d-item:nth-of-type(2), #carousel-3d-controller-1 ~ .carousel-3d-item:nth-of-type(2), #carousel-3d-controller-2 ~ .carousel-3d-item:nth-of-type(3), #carousel-3d-controller-1 ~ .carousel-3d-item:nth-of-type(3), { transition: all 1s cubic-bezier(.48, .16, .15, .98); } .carousel-3d-item:nth-of-type(2), #carousel-3d-controller-1:checked ~ .carousel-3d-item:nth-of-type(2), #carousel-3d-controller-2:checked ~ .carousel-3d-item:nth-of-type(3), #carousel-3d-controller-3:checked ~ .carousel-3d-item:nth-of-type(1) { transform: translateX(-175px) translateZ(-130px); opacity: .9; transition: all 1s cubic-bezier(.48, .16, .15, .98); } .carousel-3d-item:nth-of-type(1), #carousel-3d-controller-1:checked ~ .carousel-3d-item:nth-of-type(1), #carousel-3d-controller-2:checked ~ .carousel-3d-item:nth-of-type(2), #carousel-3d-controller-3:checked ~ .carousel-3d-item:nth-of-type(3) { transform: translateX(0) translateZ(0); opacity: 1; transition: all 1s cubic-bezier(.48, .16, .15, .98); } .carousel-3d-item:nth-of-type(3), #carousel-3d-controller-1:checked ~ .carousel-3d-item:nth-of-type(3), #carousel-3d-controller-2:checked ~ .carousel-3d-item:nth-of-type(1), #carousel-3d-controller-3:checked ~ .carousel-3d-item:nth-of-type(2) { transform: translateX(175px) translateZ(-130px); opacity: .9; transition: all 1s cubic-bezier(.48, .16, .15, .98); } 
 <div class="slider-container"> <figure id="carousel--3d"> <input type="radio" name="carousel-3d" id="carousel-3d-controller-1" /> <input type="radio" name="carousel-3d" id="carousel-3d-controller-2" /> <input type="radio" name="carousel-3d" id="carousel-3d-controller-3" /> <label for="carousel-3d-controller-1" class="carousel-3d-item"> <div class="slide-img"> <img src="https://unsplash.it/400/250?image=974" alt="" /> </div> <div class="slide-content"> <img src="assets/dist/img/easy.png"> <p>Win Discounts cccc</p> </div> </label> <label for="carousel-3d-controller-2" class="carousel-3d-item"> <div class="slide-img"> <img src="https://unsplash.it/400/250?image=974" alt="" /> </div> <div class="slide-content"> <img src="assets/dist/img/easy.png"> <p>Win Discounts bbbb</p> </div> </label> <label for="carousel-3d-controller-3" class="carousel-3d-item"> <div class="slide-img"> <img src="https://unsplash.it/400/250?image=974" alt="" /> </div> <div class="slide-content"> <img src="assets/dist/img/easy.png"> <p>Win Discounts aaaa</p> </div> </label> </figure> </div> 

You better look at the snippet in full page (link at upper-right corner). 您最好在整页中查看该代码段(右上角的链接)。

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

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