Transforming grids into Carousel (slider)

Asked

Viewed 1,566 times

2

I would like to know if it is possible to make a group of cols-* stay as carouel/slider only when less than 750px.

First of all, for those who do not understand bootstrap well, both grids and Carousel are native parts of bootstrap, ie I would like to combine these two:

http://getbootstrap.com/examples/grid/ and http://getbootstrap.com/examples/carousel/

So the question here is about the bootstrap components. It currently works like this:

  • Screens larger or equal than 750px wide:

    grid no desktop ou tablets

  • Screens smaller than 750px wide break the items, this drawing is imitating a cell phone screen (not drawing very well):

    grid em um celular

  • However I wonder if it is possible to turn it into this (when less than 750px), this drawing is imitating a cell screen, the white space is because it has no more html elements and would be a basic example, but on a full page the space would not appear:

    grid no carousel/slider

In other words, in small screens I would like to transform the cols- in a slider, so these columns would continue horizontally instead of breaking, I remember if and I did not mistake having seen a site like this, I was on mobile and I could not analyze the code.

In my current code I use the class col-sm-3 so that when the screen is less than 750px the items are not standing next to each other (the basic concept of boostrap grid system), so:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

<section>
        <h2 class="section-heading text-center">Lançamentos</h2>
        <hr class="primary">
        <div class="row nospace">
            <div class="col-sm-3">
                <div class="item">
                    <img src="images/produto1.jpg" class="img-responsive">
                    <h3>Produto</h3>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="item">
                    <img src="images/produto2.jpg" class="img-responsive">
                    <h3>Produto</h3>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="item">
                    <img src="images/produto3.jpg" class="img-responsive">
                    <h3>Produto</h3>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="item">
                    <img src="images/produto4.jpg" class="img-responsive">
                    <h3>Produto</h3>
                </div>
            </div>
        </div>
    </section>

  • 1

    I’m not sure, but it wouldn’t just be using hidden and visible-xs, and in the visible-xs put the slide you want (there are many for bootstrap). Maybe the answer of this question that talks about the hidden and visible-xs can help...

  • @gustavox I liked the idea, but the items would be duplicated on the page :/

  • If you scroll PHP you could make a template and put one include in both... but then I don’t know if it’s worth it.... btw - > http://shouldiuseacarousel.com/ :P

  • @gustavox I think with the visible-xs i will achieve the desired effect :D In the short year next put the answer!

  • It seems to me that whoever downvoted it is because they didn’t understand that Carousel is a native component of bootstrap. Please comment on the reason for the downvote, so I can understand and try to improve the question.

  • 1

    Well, for the record, it obviously wasn’t me.

  • 1

    For his comment on the visible-xs I can say that you were the one who most noticed that this question speaks of the native components, at the time you quote another component ;)

Show 2 more comments

1 answer

2

Have you taken a look at Owl Carousel it is fully responsive and easy to deploy with Bootstrap.

HTML:

<div class="container">
    <div class="owl-carousel" id="trigger-do-javascript-para-fazer-o-carousel-funcionar">
        <div class="row">
            <div class="item">
                <div class="col-xs-12 col-sm-6 col-md-4">
                    <!-- conteúdo do carousel -->
                </div>
            </div>
            <div class="item">
                <div class="col-xs-12 col-sm-6 col-md-4">
                    <!-- conteúdo do carousel -->
                </div>
            </div>
            <div class="item">
                <div class="col-xs-12 col-sm-6 col-md-4">
                    <!-- conteúdo do carousel -->
                </div>
            </div>
        </div>
    </div> <!-- /.owl-carousel -->
</div> <!-- /.container -->

CSS:

#trigger-do-javascript-para-fazer-o-carousel-funcionar .item { margin: 3px; }
#trigger-do-javascript-para-fazer-o-carousel-funcionar .item img {
    display: block;
    width: 100%;
    height: auto;
}

Javascript (jQuery):

$(document).ready(function() {
    $("#trigger-do-javascript-para-fazer-o-carousel-funcionar").owlCarousel({
        autoPlay: 3000, //Set AutoPlay to 3 seconds
        itemsCustom : [
            [0, 2],     // menos de 450px mostra 2 itens
            [450, 4],   // em 450px mostra 4 itens
            [600, 7],   // em 600px mostra 7 itens
            [700, 9],   // em 700px mostra 9 itens
            [1000, 10], // em 1000px mostra 10 itens
            [1200, 12], // em 1200px mostra 12 itens
        ],
    });
});

The Owl Carousel has several different examples of Carousel, then feel free to choose the one that best suits you :D

  • +1 because it is really a very good library, only it is not quite what I look for, because in my case I would like that above 749px there was no Carousel. Actually the need of Carousel is more to improve the mobile experience. But it’s still a great answer and will help other users ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.