简体   繁体   English

Flex容器图像滑块轮播

[英]Flex container image slider carousel

I am trying to make an image carousel with a flex container. 我正在尝试使用flex容器制作图像轮播。 I want the carousel to slide the images when certain time passes. 我希望轮播在某些时间过去后滑动图像。 The first iteration is okay but when the second one comes, it does nothing. 第一次迭代是可以的,但是当第二次迭代到来时,它什么也不做。

I do not want to use a third party carousel. 我不想使用第三方轮播。 Thanks for your answers 谢谢你的回答

$(document).ready( function() {

var carouselWidth = $('.rm-carousel-item').width();

var carIndex = 0;
var playCarousel = setInterval(function(){ autoPlayCarousel(carIndex) }, 3000);

function autoPlayCarousel(index){
    var items = $('.racmma-carousel').children();

    for (var i = 0; i < items.length; i++){
        console.log(i);
        var item = items.get(i);
        $(item).css({'transform' : 'translate(' + -carouselWidth +'px, ' + 0 + 'px)'});
    }
    carIndex += 1;
    //if ( carIndex > items.length) { carIndex = 0; }
}
});

This is the fiddle of what I am getting : 这是我所得到的东西:

https://jsfiddle.net/j565rdk8/3/ https://jsfiddle.net/j565rdk8/3/

You are almost good, you simply need to increase the translation on each iteration as actually your are always changing the same value. 您几乎不错,您只需要在每次迭代中增加转换,因为实际上您总是在更改相同的值。 You need to do something like this : 您需要执行以下操作:

'transform': 'translate(' + -(index * carouselWidth) + 'px, ' + 0 + 'px)'

The translaion become bigger on each iteration as the index is incrementing : 随着索引的增加,translaion在每次迭代中都变得更大:

 $(document).ready(function() { var carouselWidth = $('.rm-carousel-item').width(); var carIndex = 0; var playCarousel = setInterval(function() { autoPlayCarousel(carIndex) }, 1000); function autoPlayCarousel(index) { var items = $('.racmma-carousel').children(); for (var i = 0; i < items.length; i++) { var item = items.get(i); $(item).css({ 'transform': 'translate(' + -(index * carouselWidth) + 'px, ' + 0 + 'px)' }); } carIndex += 1; if (carIndex == items.length) { carIndex = 0; } } }); 
 .racmma-carousel { height: 100%; max-width: 10000%; display: -moz-flex; display: -webkit-flex; display: flex; flex-flow: row; justify-content: space-between; overflow: hidden; } .rm-carousel-item { flex-shrink: 0; width: 100%; height: 100%; background-position: center; background-size: cover; background-repeat: no-repeat; transition: 0.6s all ease-in-out; min-height: 200px; } #carousel-item-1 { background-image: url(/bd/media/hero.jpg); background-color: blue; } #carousel-item-2 { background-image: url(/bd/media/hero2.jpg); background-color: red; } #carousel-item-3 { background-image: url(/bd/media/hero3.jpg); background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="racmma-carousel"> <div class="rm-carousel-item" id="carousel-item-1"> </div> <div class="rm-carousel-item" id="carousel-item-2"> </div> <div class="rm-carousel-item" id="carousel-item-3"> </div> </div> 

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

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