Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Owl Carousel - Items are stacking on top of each other

I'm trying to group images into groups of 8, and use each grouping as a separate slide for Owl Carousel. However, rather than stacking horizontally like normal, the groupings are just stacked vertically.

My owl settings:

//Gallery carousel
gallery();
function gallery(){
  $('.gallery').owlCarousel({
    margin: 10,
    nav: true,
    items: 1,
  });
}

The php generating the HTML (uses ACF gallery plugin for WordPress)

<!-- Gallery Thumbs -->
<?php 
$images = get_field('gallery');

if( $images ): ?>
    <div class="gallery">
        <div class="gallery-group"><!-- Group the images in pairs of 8 -->
            <?php $i = 0; ?>

            <?php foreach( $images as $image ): ?>

                <?php $caption = $image['caption']; ?>
                    <a data-fancybox="gallery" href="<?php echo $image['url']; ?>">
                        <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
                    </a>

                <?php if ($caption): ?>
                    <p><?php echo $caption; ?></p>
                <?php endif; ?>

                <?php $i++; ?>

                <?php if ($i % 8 == 0): ?>
                    </div>
                    <div class="gallery-group">
                <?php endif; ?>

            <?php endforeach; ?>
        </div>
    </div>
<?php endif; ?>

I got the following CSS that applies to the carousel:

.hentry{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.gallery{
  width: 1000px;
}

I've included owl.carousel.min.css and owl.theme.default.min.css, and I'm running the latest version of jQuery. No errors in the console.

I'm not sure if this has anything to do with it, but one thing to note is that I use negative margins on some elements like my header and footer to stretch out background colors. Perhaps this is affecting things?

like image 735
Jordan Carter Avatar asked Apr 27 '17 15:04

Jordan Carter


People also ask

How do you show only one item on Owl carousel?

If you want the Owl Carousel only display one image at a time instead of a few images, please pass the option singleItem: true in the object, the setup codes should be: $(document). ready(function(){ $(". owl-carousel").

How do you turn off auto slide on Owl carousel?

In order to stop owl auto play you can trigger the corresponding event: stop. owl. autoplay: Stops autoplay.

How do you hide the dots in Owl carousel?

Since you are using OwlCarousel version 1, please check out their documentation. This also says pagination (dots) can be turned off with: pagination: false, The dots: false, is for version 2.

What is responsive owl carousel?

Owl Carousel is an easy to use, updateable, fast and free responsive carousel slider with editable code structure. It works with the jquery library and is compatible with many common browsers. They can also be used by using directional arrows, dragging and dropping and automatic looping.


2 Answers

I found a solution to this using the following SCSS:

.gallery{
	max-width: 1000px;
	width: 100%;
	overflow-x: hidden;
	
	.owl-stage{
		display: flex;
	}
}

It just feels strange how I need to add that. You'd think the plugin would be able to handle this on its own. It's also a messy solution, because all of the images load the bad way, and then shift to the proper way, so I have to hook on to the carousel init event to fade it in. Just feels like a lot of hoops to go through, so if anybody knows a better solution, please feel free to let me know.

like image 152
Jordan Carter Avatar answered Oct 07 '22 02:10

Jordan Carter


The trouble is that for some reason owl is not adding its "owl-carousel" class to the main element so its styles aren't working. Add it manually and it will be good to go.

$('.gallery').addClass('owl-carousel').owlCarousel({
        margin: 10,
        nav: true,
        items: 1,
});
like image 26
Hlsg Avatar answered Oct 07 '22 01:10

Hlsg