Difference between revisions of "Image slider"

From Organic Design wiki
m
 
(20 intermediate revisions by 2 users not shown)
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 into a slider like the one below. You can use your own CSS rules to give the next/prev links a nicer style and make a frame around the image. The images should all be the same size for it to work properly. The code is in our Subversion repository [http://svn.organicdesign.co.nz/filedetails.php?repname=extensions&path=%2FImageSlider.js here].
+
Here's a simple image slider written 100% in [[JavaScript]] and [[jQuery]]. It converts any ''div'' elements of class "image-slider" containing ''img'' elements into a slider like the one below. You can use your own CSS rules to give the next/prev links a nicer style and make a frame around the image. The images should all be the same size for it to work properly. The code is in our Git repository [https://code.organicdesign.co.nz/extensions/blob/master/jQuery/ImageSlider.js here] (Github [https://github.com/OrganicDesign/extensions/blob/master/jQuery/ImageSlider.js here]). Just copy the JavaScript into your wiki's [[MediaWiki:Common.js]] article, and any CSS rules into your wiki's [[MediaWiki:Common.css]] article.
  
There are two variables at the start of the code, '''delay''' and '''thumbWidth''', these determine the time in seconds before the next image change, and the width in pixels of the thumbnail images below the sliding image. Each image slider div element can be configured to have a row of clickable thumbnail images below the sliding image by adding "thumbs" to the ''class'' attribute as in the example code following.
+
== Configuration options ==
 +
There are a few configuration options which you can include in the slider element's data attributes, using the syntax shown in the usage section below. The available configuration options are as follows:
 +
{|class=od-info
 +
!Name!!Default!!Description
 +
|-
 +
|''thumbs''||0||The pixel size of the thumbnails shown below the sliding image (0 means don't show thumbnails)
 +
|-
 +
|''direction''||1||Specifies whether the images' automatically slide to the left (-1) or right (1)
 +
|-
 +
|''delay''||5||The number of seconds delay until the image automatically slides to the next image
 +
|-
 +
|''duration''||1000||The number of milliseconds that the transition effect takes (note that this must be less than the delay)
 +
|}
  
 +
== Usage ==
 
In the wiki you can use image links as follows:
 
In the wiki you can use image links as follows:
{{code|1=<xml><div class="image-slider thumbs">
+
<source lang="xml"><div class="image-slider" data-thumbs="100" data-direction="-1">
 
     [[File:Butterfly3.jpg|512px]]
 
     [[File:Butterfly3.jpg|512px]]
 
     [[File:YellowButterfly.jpg|512px]]
 
     [[File:YellowButterfly.jpg|512px]]
 
     [[File:MonarchOnPinkTree3.jpg|512px]]
 
     [[File:MonarchOnPinkTree3.jpg|512px]]
 
     [[File:Butterfly1.jpg|512px]]
 
     [[File:Butterfly1.jpg|512px]]
</div></xml>}}
+
</div></source>
  
  
 
Here's an example of a pure HTML page which has the links to the scripts and images included:
 
Here's an example of a pure HTML page which has the links to the scripts and images included:
{{code|1=<xml><!DOCTYPE html>
+
<source lang="xml"><!DOCTYPE html>
 
<html lang="en">
 
<html lang="en">
 
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Line 19: Line 32:
 
       <title>Image Slider</title>
 
       <title>Image Slider</title>
 
       <script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
 
       <script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
       <script src="http://svn.organicdesign.co.nz/dl.php?repname=extensions&path=%2FImageSlider.js" type="text/javascript"></script>
+
       <script src="https://code.organicdesign.co.nz/extensions/raw/master/jQuery/ImageSlider.js" type="text/javascript"></script>
 
   </head>
 
   </head>
 
   <body>
 
   <body>
       <div class="image-slider thumbs">
+
       <div class="image-slider" data-thumbs="100">
 
         <img src="http://www.organicdesign.co.nz/files/thumb/1/12/Yellow_caterpillar_with_red_face.jpg/500px-Yellow_caterpillar_with_red_face.jpg" />
 
         <img src="http://www.organicdesign.co.nz/files/thumb/1/12/Yellow_caterpillar_with_red_face.jpg/500px-Yellow_caterpillar_with_red_face.jpg" />
 
         <img src="http://www.organicdesign.co.nz/files/thumb/f/f2/Red_hairy_caterpillar.jpg/500px-Red_hairy_caterpillar.jpg" />
 
         <img src="http://www.organicdesign.co.nz/files/thumb/f/f2/Red_hairy_caterpillar.jpg/500px-Red_hairy_caterpillar.jpg" />
Line 30: Line 43:
 
       </div>
 
       </div>
 
   </body>
 
   </body>
</html></xml>}}
+
</html></source>
  
  
Here's a working example:
+
== Example ==
 +
Here's a working example which has the following syntax.
 +
<source lang="xml">
 +
<div class="image-slider" data-thumbs="100">
 +
  [[File:Butterfly3.jpg|520px]]
 +
  [[File:Orange and yellow butterfly 1.jpg|520px]]
 +
  [[File:Butterfly1.jpg|520px]]
 +
  [[File:Blue butterfly on chia.jpg|520px]]
 +
  [[File:2015 red butterfly 2.jpg|520px]]
 +
</div>
 +
</source>
  
<div class="image-slider thumbs">
+
 
 +
<div class="image-slider" data-thumbs="100">
 
[[File:Butterfly3.jpg|520px]]
 
[[File:Butterfly3.jpg|520px]]
 
[[File:Orange and yellow butterfly 1.jpg|520px]]
 
[[File:Orange and yellow butterfly 1.jpg|520px]]
Line 42: Line 66:
 
[[File:2015 red butterfly 2.jpg|520px]]
 
[[File:2015 red butterfly 2.jpg|520px]]
 
</div>
 
</div>
 +
<div style="clear:both"></div>
  
<html>
 
<script type="text/javascript">
 
// <![CDATA[
 
$(document).ready(function() {
 
 
"use strict";
 
 
var delay = 2;
 
var thumbWidth = 100;
 
 
/**
 
* Initialise all image-slider elements i the page and start them sliding
 
*/
 
$('div.image-slider').each(function() {
 
var div = $(this), w, h, src, thumb, img, prev, next;
 
 
// Initialise data structure in this slider element
 
div.data({
 
image: 1,
 
last: 0,
 
dir: 0,
 
images: [],
 
width: 0,
 
height: 0,
 
});
 
 
// If the slider has class "thumbs" then add another div that will have the thumbs in it
 
if(div.hasClass('thumbs')) thumb = $('<div />').addClass('thumbs');
 
 
// Store the image urls found in this slider div in its data and preload them, and add thumbs if set
 
$('img', div).css('display','none').each(function() {
 
src = $(this).attr('src');
 
img = $('<img />').attr('src', src);
 
div.data('images').push(src);
 
div.data('width', w = $(this).width());
 
div.data('height', h = $(this).height());
 
if(thumb) {
 
img.width(thumbWidth);
 
img.height(h*thumbWidth/w);
 
img.css({float: 'left', cursor: 'pointer'});
 
img.data('index',div.data('images').length - 1);
 
img.click(function() {
 
slide($('div.image-slider').has(this), 1, $(this).data('index'));
 
});
 
thumb.append(img);
 
}
 
});
 
 
// Restructure the content of this sliders div into layered divs with prev/next buttons
 
prev = '<a class="is-prev" href="javascript:">&lt; prev</a>';
 
next = '<a class="is-next" href="javascript:">next &gt;</a>';
 
div.html( '<div class="is-img1"><div class="is-img2">' + prev + next + '</div></div>' );
 
if(thumb) div.append(thumb);
 
$('.is-prev', div).click(function() { slide($('div.image-slider').has(this), -1); });
 
$('.is-next', div).click(function() { slide($('div.image-slider').has(this), 1); });
 
 
// Set the container size to the image size and other css styles
 
$('div',div).css({ padding: 0, width: w, height: h });
 
$('.is-prev',div).css({ float: 'left', 'margin-top': h/2 });
 
$('.is-next',div).css({ float: 'right', 'margin-top': h/2 });
 
 
// Start the sliding process
 
slide(div, 1);
 
});
 
 
/**
 
* Start animating the passed div
 
* - dir is -1 or +1 for the direction to animate (left or right)
 
* - n allows the new image to be specified rather than just next/prev (it will scroll upward)
 
*/
 
function slide(div, dir, n) {
 
var l = div.data('images').length,
 
w = div.data('width'),
 
h = div.data('height'),
 
img1, img2;
 
 
// Bail if already animating, else set animation to start
 
if(div.data('dir')) return; else div.data('dir', dir);
 
 
// Set the new image either to the next according to the passed direction, or to n if passed
 
div.data('last', div.data('image'));
 
div.data('image', n === undefined ? (div.data('image') + dir + l) % l : n);
 
img1 = div.data('images')[div.data('image')];
 
img2 = div.data('images')[div.data('last')];
 
 
// Show next image on regular interval
 
if(div.data('timer')) clearTimeout(div.data('timer'));
 
div.data('timer', setTimeout(function() { slide(div, 1); }, delay * 1000));
 
 
// Play an animation from the current image to the next
 
div.animate({ t: 1 }, {
 
duration: 1000,
 
step: function(now, fx) {
 
var div = $(fx.elem), offset, x1, y1, x2, y2;
 
 
// Set an offset in pixels for the transition between the current and last image
 
offset = -div.data('dir') * fx.pos * (n === undefined ? w : h);
 
 
// Calculate the positions of the current and last image (images specified with n scroll upward)
 
x1 = n === undefined ? offset + w * dir : 0;
 
x2 = n === undefined ? offset : 0;
 
y1 = n === undefined ? 0 : offset + h * dir;
 
y2 = n === undefined ? 0 : offset;
 
  
// Set the positions of the images with CSS
+
== CSS styles ==
$('.is-img1', div).css( 'background', 'url("' + img1 + '") no-repeat ' + x1 + 'px ' + y1 + 'px' );
+
If you want to use the same CSS as this working example, the rules are as follows. Or you can see the CSS class attributes used here to make your own rules.
$('.is-img2', div).css( 'background', 'url("' + img2 + '") no-repeat ' + x2 + 'px ' + y2 + 'px' );
+
<source lang="css">
},
+
/* style for next/prev links in "Image slider" page */
complete: function(now, fx) {
+
.image-slider .is-next, .image-slider .is-prev {
$(this).data('dir', 0); // mark current slider as no longer animating
+
    background-color: black;
}
+
    color: white;
});
+
    text-decoration: none;
};
+
    outline: 0;
});
+
    font-size: 15px;
// ]]>
+
    font-weight: bold;
</script>
+
    -moz-border-radius: 11px;
</html>
+
    -webkit-border-radius: 11px;
 +
    -khtml-border-radius: 11px;
 +
    border-radius: 11px;
 +
    padding: 5px 14px;
 +
    margin: 5px;
 +
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; /* IE 8 */
 +
    filter: alpha(opacity=50);  /* IE 5-7 */
 +
    -moz-opacity: 0.5;          /* Netscape */
 +
    -khtml-opacity: 0.5;        /* Safari 1.x */
 +
    opacity: 0.5;               /* Good browsers */
 +
}
 +
.image-slider .is-next:hover, .image-slider .is-prev:hover {
 +
    text-decoration: none;
 +
    outline: 0;
 +
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
 +
    filter: alpha(opacity=100);
 +
    -moz-opacity: 1;
 +
    -khtml-opacity: 1;
 +
    opacity: 1;
 +
}
 +
.image-slider .thumbs {
 +
    width: 525px !important;
 +
}
 +
.image-slider .thumbs img {
 +
    margin: 5px 5px 0 0;
 +
}
 +
</source>
 +
[[Category:JavaScript]][[Category:Examples]]

Latest revision as of 08:26, 10 April 2016

Here's a simple image slider written 100% in JavaScript and jQuery. It converts any div elements of class "image-slider" containing img elements into a slider like the one below. You can use your own CSS rules to give the next/prev links a nicer style and make a frame around the image. The images should all be the same size for it to work properly. The code is in our Git repository here (Github here). Just copy the JavaScript into your wiki's MediaWiki:Common.js article, and any CSS rules into your wiki's MediaWiki:Common.css article.

Configuration options

There are a few configuration options which you can include in the slider element's data attributes, using the syntax shown in the usage section below. The available configuration options are as follows:

Name Default Description
thumbs 0 The pixel size of the thumbnails shown below the sliding image (0 means don't show thumbnails)
direction 1 Specifies whether the images' automatically slide to the left (-1) or right (1)
delay 5 The number of seconds delay until the image automatically slides to the next image
duration 1000 The number of milliseconds that the transition effect takes (note that this must be less than the delay)

Usage

In the wiki you can use image links as follows:

<div class="image-slider" data-thumbs="100" data-direction="-1">
    [[File:Butterfly3.jpg|512px]]
    [[File:YellowButterfly.jpg|512px]]
    [[File:MonarchOnPinkTree3.jpg|512px]]
    [[File:Butterfly1.jpg|512px]]
</div>


Here's an example of a pure HTML page which has the links to the scripts and images included:

<!DOCTYPE html>
<html lang="en">
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <head>
      <title>Image Slider</title>
      <script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
      <script src="https://code.organicdesign.co.nz/extensions/raw/master/jQuery/ImageSlider.js" type="text/javascript"></script>
   </head>
   <body>
      <div class="image-slider" data-thumbs="100">
         <img src="http://www.organicdesign.co.nz/files/thumb/1/12/Yellow_caterpillar_with_red_face.jpg/500px-Yellow_caterpillar_with_red_face.jpg" />
         <img src="http://www.organicdesign.co.nz/files/thumb/f/f2/Red_hairy_caterpillar.jpg/500px-Red_hairy_caterpillar.jpg" />
         <img src="http://www.organicdesign.co.nz/files/thumb/1/17/Green_and_red_caterpillar_2.jpg/500px-Green_and_red_caterpillar_2.jpg" />
         <img src="http://www.organicdesign.co.nz/files/thumb/1/13/Black_caterpillar_with_yellow_stripes_1.jpg/500px-Black_caterpillar_with_yellow_stripes_1.jpg" />
         <img src="http://www.organicdesign.co.nz/files/thumb/3/39/Yellow_hairy_caterpillar_2.jpg/500px-Yellow_hairy_caterpillar_2.jpg" />
      </div>
   </body>
</html>


Example

Here's a working example which has the following syntax.

<div class="image-slider" data-thumbs="100">
  [[File:Butterfly3.jpg|520px]]
  [[File:Orange and yellow butterfly 1.jpg|520px]]
  [[File:Butterfly1.jpg|520px]]
  [[File:Blue butterfly on chia.jpg|520px]]
  [[File:2015 red butterfly 2.jpg|520px]]
</div>


Butterfly3.jpg Ethilla Longwing 1.jpg Butterfly1.jpg Blue butterfly on chia.jpg 2015 red butterfly 2.jpg


CSS styles

If you want to use the same CSS as this working example, the rules are as follows. Or you can see the CSS class attributes used here to make your own rules.

/* style for next/prev links in "Image slider" page */
.image-slider .is-next, .image-slider .is-prev {
    background-color: black;
    color: white;
    text-decoration: none;
    outline: 0;
    font-size: 15px;
    font-weight: bold;
    -moz-border-radius: 11px;
    -webkit-border-radius: 11px;
    -khtml-border-radius: 11px;
    border-radius: 11px;
    padding: 5px 14px;
    margin: 5px;
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; /* IE 8 */
    filter: alpha(opacity=50);  /* IE 5-7 */
    -moz-opacity: 0.5;          /* Netscape */
    -khtml-opacity: 0.5;        /* Safari 1.x */
    opacity: 0.5;               /* Good browsers */
}
.image-slider .is-next:hover, .image-slider .is-prev:hover {
    text-decoration: none;
    outline: 0;
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    filter: alpha(opacity=100);
    -moz-opacity: 1;
    -khtml-opacity: 1;
    opacity: 1;
}
.image-slider .thumbs {
    width: 525px !important;
}
.image-slider .thumbs img {
    margin: 5px 5px 0 0;
}