A lot of websites have a image slider. Most of the time there is just a simple slide effect required and it is not necessary to use a huge JavaScript library like the Revolution Slider or the WooSlider.
We have been using the OWL Carousel for several different web-projects and we are very happy with the simple integration and the features it provides. Together with Advanced Custom Fields and the Gallery Add-on it does not take a lot of time to create a slider and the styling is pretty simple as well.
You can use this tutorial without WordPress and ACF as well, but then you have to set up the list-structure of the slider manually.
HTML
In the standard version we are just getting the images to display:
1 2 3 4 5 6 7 8 9 |
<ul id="owl-demo" class="owl-carousel owl-theme"> <?php $images = get_field('slider'); foreach ($images as $image):?> <li> <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /> </li> <?php endforeach;?> </ul> |
If you would like to show some text like the image title you have to render it inside the list-element (see line 8-10).
1 2 3 4 5 6 7 8 9 10 11 |
<ul id="owl-demo" class="owl-carousel owl-theme"> <?php $images = get_field('slider'); foreach ($images as $image):?> <li> <img class="slide" src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /> <div class="slide-content"> <?php echo $image['title'];?> </div> </li> <?php endforeach;?> </ul> |
JavaScript for OWL Carousel
We have been using some settings like the interval and the slide speed to control the behaviour of the slider. You can find all available settings here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<script> jQuery(function($){ $(document).ready(function(){ var owl = $(".owl-carousel").owlCarousel({ items: 1, slideSpeed: 900, pagination: true, autoplay: true, autoplayTimeout: 3500, autoplayHoverPause: true, addClassActive: true, singleItem: true, }).data('owlCarousel'); $('.owl-item').click(function(){ owl.next(); }) setInterval(function(){ owl.next(); }, 6000); }); }) </script> |
CSS
Basically there is just a styling of the slide appearance required. If you are showing additional content like the title you have to adjust this container with the following code:
1 2 3 4 5 6 7 8 9 10 |
.slide{ position: relative; z-index: 1; } .slider-content{ position: absolute; top: 20px; left: 20px; z-index: 100; } |
Examples
You find examples of the slider here:
http://www.gegenfestplattenabgabe.at
http://www.austrianstartups.com