Simple image slideshow

From UnionCloud Support
Revision as of 15:01, 29 March 2017 by Ash (talk | contribs) (CSS)
Jump to: navigation, search

The following code snippets allow you to add simple image slideshow functionality to your page or site, which will fade nicely between images after a set duration. The duration defaults to 5 seconds, but can be adjusted per slideshow if needed.

The CSS and Javascript snippets can be added to either a single page's CSS and Javascript, or can be added to your Global CSS and Global Javascript to enable the functionality site wide.

Please Note: The slideshow will be the size of the first image in the sequence. Ideally all the images in the sequence should be the same size.

CSS

 1 .image-slideshow  {
 2     position: relative;
 3     overflow: hidden;
 4 }
 5 
 6 .image-slideshow img {
 7     top: 0;
 8     left: 0;
 9     position: absolute;
10     z-index: 1;
11     transition: opacity 1s;
12     opacity: 0;
13     width: 100% !important;
14     height: auto !important;
15     vertical-align: top;
16     will-change: opacity;
17 }
18 
19 .image-slideshow img:first-child {
20     position: relative;
21 }
22 
23 .image-slideshow img.active-image {
24     z-index: 3;
25     opacity: 1;
26 }
27 
28 .image-slideshow img.previous-image {
29     z-index: 2;
30     opacity: 1;
31 }

Javascript

$(function() {
    $('.image-slideshow').each(function(index) {
        var slideshowElement = this;
        var duration = $(slideshowElement).attr('data-speed');
        if (typeof duration == typeof undefined || duration == false) {
            duration = 5;
        } else {
            duration = parseInt(duration);
        }
        $(slideshowElement).children('img:first').addClass('active-image');
        setInterval(function() { CycleImage(slideshowElement); }, duration * 1000);
    });
});

function CycleImage(slideshowElement){
    var active = $(slideshowElement).children('.active-image');
    var next = (active.next().length > 0) ? active.next() : $(slideshowElement).children('img:first');
    active.addClass('previous-image');
    active.removeClass('active-image');
    next.addClass('active-image');
}

$(".image-slideshow").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){
    $(this).children('.previous-image').removeClass('previous-image');
});

Usage

To use the slideshow on a page, change the editor (CKEditor) to source view, and insert the follow snippet where you want the slideshow to appear:

<p class="image-slideshow">PUT IMAGES HERE</p>

Then change back to normal view, and replace the "PUT IMAGES HERE" text with the images you want to use, by inserting images as normal.

If you want to change the duration between each image change from 5 seconds to something else, add a "data-speed" attribute to the <p> tag set to the number of seconds you want the duration to be. For example for a 10 second duration, you tag would look like this:

<p class="image-slideshow" data-speed="10">PUT IMAGES HERE</p>