Difference between revisions of "Image slider"
From Organic Design wiki
m |
(update code and move into subversion) |
||
Line 1: | Line 1: | ||
− | Here's a simple image slider written 100% in [[JavaScript]] and [[jQuery]]. In converts any ''div'' elements of class "image-slider" containing ''img'' elements in a slider like the one below. The code is | + | Here's a simple image slider written 100% in [[JavaScript]] and [[jQuery]]. In converts any ''div'' elements of class "image-slider" containing ''img'' elements in a slider like the one below. The code is in our Subversion repository [http://svn.organicdesign.co.nz/filedetails.php?repname=extensions&path=%2FImageSlider.js here]. |
<div class="image-slider"> | <div class="image-slider"> | ||
Line 14: | Line 14: | ||
<script type="text/javascript"> | <script type="text/javascript"> | ||
// <![CDATA[ | // <![CDATA[ | ||
− | |||
− | |||
$(document).ready( function() { | $(document).ready( function() { | ||
− | + | var delay = 5; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
$('div.image-slider').each( function() { | $('div.image-slider').each( function() { | ||
var div = $(this); | var div = $(this); | ||
− | // Create | + | // Create default data for this slider and store it in the element |
− | + | div.data('image', 0); | |
− | div. | + | div.data('dir', 0); |
− | + | div.data('images', []); | |
− | |||
− | // Store the image urls found in this slider in | + | // Store the image urls found in this slider div in its data and preload them |
− | $('img', div).css('display','none').each( function() { | + | $('img', div).css('display','none').each(function() { |
var src = $(this).attr('src'); | var src = $(this).attr('src'); | ||
var image = $('<img />').attr('src', src); | var image = $('<img />').attr('src', src); | ||
− | + | div.data('images').push(src); | |
− | + | div.data('w', $(this).attr('width')); | |
− | + | div.data('h', $(this).attr('height')); | |
}); | }); | ||
// Restructure the content of this sliders div into a table with prev/next buttons | // Restructure the content of this sliders div into a table with prev/next buttons | ||
− | var prev = '<a class="is-prev" href=" | + | var prev = '<a class="is-prev" href="#">< prev</a>'; |
− | var next = '<a class="is-next" href=" | + | var next = '<a class="is-next" href="#">next ></a>'; |
div.html( '<table><tr><th><table><tr><td>' + prev + next + '</td></tr></table></th></tr></table>' ); | div.html( '<table><tr><th><table><tr><td>' + prev + next + '</td></tr></table></th></tr></table>' ); | ||
+ | $('.is-prev', div).click(function(e,d) { slide($('div').has(this), -1 ); }); | ||
+ | $('.is-next', div).click(function(e) { slide($('div').has(this), 1 ); }); | ||
// Set the cell size to the image size and other css styles | // Set the cell size to the image size and other css styles | ||
− | $('table',div).css({ background: 'transparent', width: | + | $('table',div).css({ background: 'transparent', width: div.data('w'), height: div.data('h'), 'border-collapse': 'collapse' }); |
$('td,th',div).css({ padding: 0, 'vertical-align': 'middle' }); | $('td,th',div).css({ padding: 0, 'vertical-align': 'middle' }); | ||
$('.is-prev',div).css({ float: 'left' }); | $('.is-prev',div).css({ float: 'left' }); | ||
Line 142: | Line 49: | ||
// Initialise the table's images to first image with zero offset | // Initialise the table's images to first image with zero offset | ||
− | + | slide(div, 0); | |
}); | }); | ||
− | function | + | function slide(div, dir) { |
// Set the new image and animate to it (bail if already animating) | // Set the new image and animate to it (bail if already animating) | ||
− | + | if(div.data('dir')) return; | |
− | if( data | + | div.data('image', div.data('image') + dir); |
− | data.image + | + | div.data('dir', dir); |
− | data | ||
// Show next image on regular interval | // Show next image on regular interval | ||
− | if( 'timer' | + | if(div.data('timer')) clearTimeout(div.data('timer')); |
− | data | + | div.data('timer', setTimeout(function() { slide(div,1); }, delay * 1000)); |
// Play an animation from the current image to the next | // Play an animation from the current image to the next | ||
− | + | div.animate({ t: 1 }, { | |
duration: 1000, | duration: 1000, | ||
step: function(now, fx) { | step: function(now, fx) { | ||
var div = $(fx.elem); | var div = $(fx.elem); | ||
− | |||
// Get the URLs for the current and next image | // Get the URLs for the current and next image | ||
− | var l = data | + | var l = div.data('images').length; |
− | var image = data | + | var image = div.data('image'); |
image += l * 1000000; | image += l * 1000000; | ||
var next = ( image - dir ) % l; | var next = ( image - dir ) % l; | ||
image %= l; | image %= l; | ||
− | var offset = -data | + | var offset = -(div.data('dir') * fx.pos * div.data('w')); |
// Set the URL and position for the current image | // Set the URL and position for the current image | ||
$('th', div).css('background', 'transparent url("' | $('th', div).css('background', 'transparent url("' | ||
− | + data | + | + div.data('images')[image] |
+ '") no-repeat ' | + '") no-repeat ' | ||
− | + (offset + data | + | + (offset + div.data('w') * dir) |
+ 'px center' | + 'px center' | ||
); | ); | ||
Line 182: | Line 87: | ||
// Set the URL and position for the next image | // Set the URL and position for the next image | ||
$('td', div).css('background', 'transparent url("' | $('td', div).css('background', 'transparent url("' | ||
− | + data | + | + div.data('images')[next] + '") no-repeat ' |
+ offset | + offset | ||
+ 'px center' | + 'px center' | ||
Line 188: | Line 93: | ||
}, | }, | ||
complete: function(now, fx) { | complete: function(now, fx) { | ||
− | + | $(this).data('dir', 0); // mark current slider as no longer animating | |
− | |||
} | } | ||
}); | }); | ||
}; | }; | ||
}); | }); | ||
− | </ | + | // ]]> |
+ | </script> | ||
+ | </html> |
Revision as of 22:11, 27 March 2015
Here's a simple image slider written 100% in JavaScript and jQuery. In converts any div elements of class "image-slider" containing img elements in a slider like the one below. The code is in our Subversion repository here.