Difference between revisions of "Image slider"

From Organic Design wiki
m (show some source)
 
(27 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.
  
 +
== 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">
+
<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>
  
  
Or in a pure HTML environment you just use image links like this, and add the code into the ''head'' element of the HTML (of course this assumes that the images are in the same location as the HTML):
+
Here's an example of a pure HTML page which has the links to the scripts and images included:
{{code|1=<xml><div class="image-slider">
+
<source lang="xml"><!DOCTYPE html>
    <img src="Butterfly3.jpg" />
+
<html lang="en">
    <img src="YellowButterfly.jpg" />
+
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <img src="MonarchOnPinkTree3.jpg" />
+
  <head>
    <img src="Butterfly1.jpg" />
+
      <title>Image Slider</title>
</div></xml>}}
+
      <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></source>
  
  
Here's a working example:
+
== Example ==
 
+
Here's a working example which has the following syntax.
<div class="image-slider">
+
<source lang="xml">
[[File:Butterfly3.jpg|512px]]
+
<div class="image-slider" data-thumbs="100">
[[File:YellowButterfly.jpg|512px]]
+
  [[File:Butterfly3.jpg|520px]]
[[File:MonarchOnPinkTree3.jpg|512px]]
+
  [[File:Orange and yellow butterfly 1.jpg|520px]]
[[File:Butterfly1.jpg|512px]]
+
  [[File:Butterfly1.jpg|520px]]
 +
  [[File:Blue butterfly on chia.jpg|520px]]
 +
  [[File:2015 red butterfly 2.jpg|520px]]
 
</div>
 
</div>
 +
</source>
  
[[File:Butterfly3.jpg|125px]] [[File:YellowButterfly.jpg|125px]] [[File:MonarchOnPinkTree3.jpg|125px]] [[File:Butterfly1.jpg|125px]]
 
  
 +
<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>
 +
<div style="clear:both"></div>
  
<html>
 
<script type="text/javascript">
 
// <![CDATA[
 
$(document).ready(function() {
 
 
"use strict";
 
 
var delay = 5;
 
 
$('div.image-slider').each(function() {
 
var div = $(this), w, h;
 
 
// Create default data for this slider and store it in the element
 
div.data('image', 0);
 
div.data('dir', 0);
 
div.data('images', []);
 
 
// Store the image urls found in this slider div in its data and preload them
 
$('img', div).css('display','none').each(function() {
 
var src = $(this).attr('src');
 
var image = $('<img />').attr('src', src);
 
div.data('images').push(src);
 
w = $(this).width();
 
h = $(this).height();
 
});
 
 
// Restructure the content of this sliders div into layered divs with prev/next buttons
 
var prev = '<a class="is-prev" href="javascript:">&lt; prev</a>';
 
var next = '<a class="is-next" href="javascript:">next &gt;</a>';
 
div.html( '<div class="is-img1"><div class="is-img2">' + prev + next + '</div></div>' );
 
$('.is-prev', div).click(function() { slide($('div.image-slider').has(this), -1, w); });
 
$('.is-next', div).click(function() { slide($('div.image-slider').has(this), 1, w); });
 
 
// Set the cell 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 });
 
 
// Initialise the table's images to first image with zero offset
 
slide(div, 0, w);
 
});
 
 
function slide(div, dir, w) {
 
 
// Set the new image and animate to it (bail if already animating)
 
if(div.data('dir')) return;
 
div.data('image', div.data('image') + dir);
 
div.data('dir', dir);
 
 
// Show next image on regular interval
 
if(div.data('timer')) clearTimeout(div.data('timer'));
 
div.data('timer', setTimeout(function() { slide(div, 1, w); }, 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);
 
 
// Get the URLs for the current and next image
 
var l = div.data('images').length;
 
var image = div.data('image');
 
image += l * 1000000;
 
var next = ( image - dir ) % l;
 
image %= l;
 
var offset = -(div.data('dir') * fx.pos * w);
 
 
// Set the URL and position for the current image
 
$('.is-img1', div).css( 'background', 'transparent url("' + div.data('images')[image]
 
+ '") no-repeat ' + (offset + w * dir) + 'px center' );
 
  
// Set the URL and position for the next image
+
== CSS styles ==
$('.is-img2', div).css( 'background', 'transparent url("'
+
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.
+ div.data('images')[next] + '") no-repeat ' + offset + 'px center' );
+
<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;
}