zaterdag 26 juli 2014

jQuery Image Slideshow Plugin

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

jquery-image-slideshow

A nice photo goes a long way towards making a design stand out. But we at Tutorialzine realized that sometimes a single picture is not enough and what you really need is a smooth slideshow of images to capture the user’s attention and bring dynamics to the app. However, the implementation of such slideshows can sometimes be tricky, so we decided to make a tiny plugin to do the job for you.


How it works


It’s really simple! First, you have to insert an image into the html as you would normally do. After that’s done, you have to add a data attribute – data-slideshow – and set its value to be the path of a series of images that you’d like to be turned into a slideshow:


<img src="...." data-slideshow="img1.jpg|img2.jpg|img3.jpg" />

All that is left, is to include our plugin in your page, call its slideShow() method and your slideshow is good to go!


The Code


The plugin consists of a javasctript file and a css one.


We’ll start with the .js file!


assets/jQuery-slideshow-plugin/plugin.js


This file holds a regular jQuery plugin. First of all we need to define our default options.


options = $.extend( timeOut: 3000, // how long each slide stays on screen showNavigation: true, // show previous/next arrows pauseOnHover: true, // pause when hovering with the mouse swipeNavigation: true // (basic) support for swipe gestures
, options);

The basic idea is that we take the sources from the data-slideshow attribute of a certain image and insert them into a div as its background. This div has the dimensions of the original picture and after it has collected all the images for the slides (including the one we started with) replaces it. Let’s look at the code to make it a bit clearer.


// Variables
var intervals = [], slideshowImgs = [], originalSrc, img, cont, width, height, // Creates an object with all the elements with a 'data-slideshow' attribute container = this.filter(function () return $(this).data('slideshow');
); // Cycle through all the elements from the container object
// Later on we'll use the "i" variable to distinguish the separate slideshows from one another for (var i = 0; i < container.length; i++) "); for (var j = 0; j < slideshowImgs[i].length; j++) img = $('<div class="slide" style="background-image: url(' + slideshowImgs[i][j] + ')">'); img.appendTo(helpdiv); // Replace the original element with the helper <div> cont.replaceWith(helpdiv); // Activate the slideshow automaticSlide(i)

Upon activation the images start to fade in and out after one another automatically.

Depending on the settings we can also control the slideshow by clicking and hovering.

Thanks to hammer.js we made it possible to swipe through the pictures, as well.


Let’s check out the functions that move the slides around!


// Slideshow auto switch function automaticSlide(index) // Hide all the images except the first one $('#slideshow-container-' + index + ' .slide:gt(0)').hide(); // Every few seconds fade out the first image, fade in the next one, // then take the first and append it to the container again, so it becomes last intervals[index] = setInterval(function () $('#slideshow-container-' + index + ' .slide:first').fadeOut("slow") .next('.slide').fadeIn("slow") .end().appendTo('#slideshow-container-' + index + ''); , options.timeOut);
// Pause on hover and resume on mouse leave if (options.pauseOnHover) (function hoverPause() $('.slideshow').on( 'mouseenter.hover': function () clearInterval(intervals[($(this).attr('id').split('-')[2])]) , 'mouseleave.hover': function () automaticSlide($(this).attr('id').split('-')[2]) ); )()
// We use this to prevent the slideshow from resuming once we've stopped it function hoverStop(id) $('#' + id + '').off('mouseenter.hover mouseleave.hover');
// Create the navigation buttons function createNavigation() // The buttons themselves var leftArrow = $('<div class="leftBtn slideBtn hide">'); var rightArrow = $('<div class="rightBtn slideBtn hide">'); // Arrows for the buttons var nextPointer = $('<span class="pointer next"></span>'); var prevPointer = $('<span class="pointer previous"></span>'); prevPointer.appendTo(leftArrow); nextPointer.appendTo(rightArrow); leftArrow.appendTo(helpdiv); rightArrow.appendTo(helpdiv);
// Slideshow manual switch if (options.showNavigation) // This shows the navigation when the mouse enters the slideshow
// and hides it again when it leaves it $('.slideshow').on( 'mouseenter': function () $(this).find('.leftBtn, .rightBtn').removeClass('hide') , 'mouseleave': function () $(this).find('.leftBtn, .rightBtn').addClass('hide') ); // Upon click, stop the automatic slideshow and change the slide $('.leftBtn').on('click', function () // Clear the corresponding interval to stop the slideshow // ( intervals is an array, so we give it the number of the slideshow container) clearInterval(intervals[($(this).parent().attr('id').split('-')[2])]); // Make the last slide visible and set it as first in the slideshow container $(this).parent().find('.slide:last').fadeIn("slow") .insertBefore($(this).parent().find('.slide:first').fadeOut("slow")); hoverStop($(this).parent().attr('id')); ); $('.rightBtn').on('click', function () // Clear the corresponding interval to stop the slideshow clearInterval(intervals[($(this).parent().attr('id').split('-')[2])]); // Fade out the current image and append it to the parent, making it last // Fade in the next one $(this).parent().find('.slide:first').fadeOut("slow") .next('.slide').fadeIn("slow") .end().appendTo($(this).parent()); hoverStop($(this).parent().attr('id')); );
// Change slide on swipe // Same as the 'on click' functions, but we use hammer.js this time if (options.swipeNavigation) $('.slideshow').hammer().on( "swiperight": function () clearInterval(intervals[($(this).attr('id').split('-')[2])]); $(this).find('.slide:last').fadeIn("slow") .insertBefore($(this).find('.slide:first').fadeOut("slow")) , "swipeleft": function () clearInterval(intervals[($(this).attr('id').split('-')[2])]); $(this).find('.slide:first').fadeOut("slow") .next('.slide').fadeIn("slow") .end().appendTo($(this)); )

Finally, let’s take a quick look at some of the css.


assets/jQuery-slideshow-plugin/plugin.css


We want all the images for our slides to stack on one another, so we give them the “sliding” class. It also makes them fit the entire slideshow div.


.sliding position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-position: center; background-size: cover;

Then we set a z-index of 10 to the buttons to place them on top of the pictures.


.slideBtn position: absolute; z-index: 10; width: 50px; height: 100%; cursor: pointer;
.leftBtn left: 0px; background: linear-gradient(to right, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0));
.rightBtn right: 0px; background: linear-gradient(to left, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0));

Lastly, we make triangle arrows out of css borders and put them on top of everything with a z-index of 9001;


.pointer position: absolute; top: 50%; margin-top: -32px; z-index: 9001; left: 12px; opacity: 0.8;
.previous width: 0; height: 0; border-top: 20px solid transparent; border-bottom: 20px solid transparent; border-right: 20px solid white; .next width: 0; height: 0; border-top: 20px solid transparent; border-bottom: 20px solid transparent; border-left: 20px solid white; right: 12px; left: auto;

Using the plugin


To use the plugin, include assets/jQuery-slideshow-plugin/plugin.css to the head of your page, and assets/jQuery-slideshow-plugin/plugin.js after your copy of the jQuery and Hammer.js libraries.


To initialize the plugin use this piece of code and feel free to change the settings’ values.


(function ($) {
$('#activate').on('click', function () $('img').slideShow( timeOut: 2000, showNavigation: true, pauseOnHover: true, swipeNavigation: true );
(jQuery));

Done!


With this we round up the tutorial. We hope that you’ll give the plugin a go. We did our best to make it as easy and fun to use as possible, while still maintaining a reasonable functionality. For any recommendations, questions and opinions, feel free to leave a comment below.







Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

jQuery Image Slideshow Plugin

Geen opmerkingen:

Een reactie posten