Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added README and HOWTO explaining the plugin as deprecated. Removed m…

…inified version.
  • Loading branch information...
commit ca0b3f9b2a795ca6d7b9f5e05a470f12ec154817 1 parent cd2ae04
Tobia Conforto authored
140 HOWTO.md
Source Rendered
... ... @@ -0,0 +1,140 @@
  1 +# CrossSlide
  2 +
  3 +CrossSlide is a jQuery plugin that can create a few different slideshow effects, depending on how it's called, without using any binary browser plugins.
  4 +
  5 +Please notice that **this code is unmaintained and unsupported.**
  6 +See the [README](README.md) file for details.
  7 +
  8 +## Static cross-fade
  9 +
  10 +The simplest effect, a cross-fade between static pictures, is just as simple to set up:
  11 +
  12 + $('#placeholder').crossSlide({
  13 + sleep: 2,
  14 + fade: 1
  15 + }, [
  16 + { src: 'sand-castle.jpeg' },
  17 + { src: 'sunflower.jpeg' },
  18 + { src: 'flip-flops.jpeg' },
  19 + { src: 'rubber-ring.jpeg' }
  20 + ]);
  21 +
  22 +
  23 +`#placeholder` is the block-level element (such as a `div`) whose contents will be replaced with the animation. This element should have its width and height fixed in the CSS, even when emptied of all contents. What you put inside it in your HTML is only shown while the images are being preloaded, or if the user-agent has JavaScript turned off. The current version of CrossSlide waits until all the images are preloaded before starting the slideshow, so I usually put the first image as a background-image of the div, so that users with a slow connection won't see a big hole in the website while all the images are being loaded.
  24 +
  25 +The first parameter to the `crossSlide()` function call is a dictionary of options. The second parameter is an array of objects, defining the sequence of pictures, each with its source path and various attributes.
  26 +
  27 +To get the static cross-fade effect you must specify the `sleep` option, which is the time every image will take on its own, and the `fade` option, which is the duration of each cross-fade animation between images. Both are expressed in seconds and can accept decimal values.
  28 +
  29 +## Slide and cross-fade
  30 +
  31 +This is the kind of animation from which the plugin takes its name. It shows a sequence of pictures moving at a constant speed in alternating directions, with a cross-fade effect between any two pictures.
  32 +
  33 +Here is the jQuery code to set it up:
  34 +
  35 + $('#placeholder').crossSlide({
  36 + speed: 45,
  37 + fade: 1
  38 + }, [
  39 + { src: 'sand-castle.jpeg', dir: 'up' },
  40 + { src: 'sunflower.jpeg', dir: 'down' },
  41 + { src: 'flip-flops.jpeg', dir: 'up' },
  42 + { src: 'rubber-ring.jpeg', dir: 'down' }
  43 + ]);
  44 +
  45 +
  46 +Notice how the `speed` parameter, expressed in pixels/second, has taken the place of `sleep`, as the images aren't static anymore, but move with constant speed. `fade` is still required and still expressed in seconds. You cannot use both `speed` and `sleep` at the same time, because they trigger different effects.
  47 +
  48 +Additionally you have to specify the direction in which each image should be moving. The plugin computes the rest, panning each image edge-to-egde at the desired speed, in the desired direction. `dir` must be one of `up`, `down`, `left` or `right`. For best results I recommend using an even number of pictures and alternating directions, as in the example.
  49 +
  50 +## Ken Burns effect
  51 +
  52 +Finally, CrossSlide can be brought up to the full visual power of a so-called Ken Burns effect: panning, zooming and fading each image to specific points, to guide the eye of the viewer and convey meaning.
  53 +
  54 +In this case the jQuery code is a bit more complex, because it shows a number of features:
  55 +
  56 + $('#placeholder').crossSlide({
  57 + fade: 1
  58 + }, [
  59 + {
  60 + src: 'sand-castle.jpeg',
  61 + alt: 'Sand Castle',
  62 + from: '100% 80% 1x',
  63 + to: '100% 0% 1.7x',
  64 + time: 3
  65 + }, {
  66 + src: 'sunflower.jpeg',
  67 + alt: 'Sunflower',
  68 + from: 'top left',
  69 + to: 'bottom right 1.5x',
  70 + time: 2
  71 + }, {
  72 + src: 'flip-flops.jpeg',
  73 + alt: 'Flip Flops',
  74 + from: '100% 80% 1.5x',
  75 + to: '80% 0% 1.1x',
  76 + time: 2
  77 + }, {
  78 + src: 'rubber-ring.jpeg',
  79 + alt: 'Rubber Ring',
  80 + from: '100% 50%',
  81 + to: '30% 50% 1.5x',
  82 + time: 2
  83 + }
  84 + ], function(idx, img, idxOut, imgOut) {
  85 + if (idxOut == undefined) {
  86 + // starting single image phase, put up caption
  87 + $('div.caption').text(img.alt).animate({opacity: .7})
  88 + } else {
  89 + // starting cross-fade phase, take out caption
  90 + $('div.caption').fadeOut()
  91 + }
  92 + });
  93 +
  94 +Every picture's pan & zoom effect will last for `time` seconds plus the two cross-fades, each taking an additional `fade` seconds. `from` and `to` define the starting and ending points of the effect, including the cross-fade part. They are expressed as a background-position value, following the syntax of the CSS property of the same name, plus an optional zoom factor. The zoom defaults to 1x if not provided. The background-position part only accepts keywords and percent values, lengths are not supported. As in CSS, the percentages are interpreted as horizontal followed by vertical, while the keywords can be put in any order.
  95 +
  96 +Every picture can be made a hyperlink, by adding a `href` parameter with a relative or absolute URI to the option dictionary of the single picture. You can also add an `onclick` parameter, pointing to a function that will handle click events; `alt` to supply the alternate text; and `target` to specify the target frame.
  97 +
  98 +Other options you can put in the global dictionary are: `loop` (numeric) to have the animation loop just once, or a fixed number of times, and then stop; `shuffle` (boolean) to have CrossSlide automatically shuffle the images before starting the slideshow; and `doubleFade` (boolean) to have both a fade-out and a fade-in effect when switching images (this is useful with transparent images.)
  99 +
  100 +As shown in this example, CrossSlide accepts a callback function as a third argument. This callback will be invoked at every keyframe, meaning when an image starts to be cross-faded with another, and when the cross-fade ends and a single image remains on screen. The callback will be passed either 2 or 4 arguments, depending on the phase of the animation. If we are at the beginning of a single image-phase, the callback will be passed 2 arguments: the index of the image in the array and the object currently being animated. If we are at the beginning of a cross-fade, the callback will be passed 4 arguments: index and img element of the incoming image, plus index and img element of the outgoing image. You can see how the example exploits this fact to show a textual caption.
  101 +
  102 +Finally, there are 5 methods you can invoke at runtime on the same object you invoked `crossSlide()` on, to control the animation. The first 3 use jQuery standard functions. The pause and resume methods, on the other hand, require an extension to jQuery's animation facility, in the form of my own [jQuery Pause plugin][1].
  103 +
  104 +* `crossSlideFreeze()` will freeze the slideshow, using jQuery's `stop()` method, leaving the images in the position they where when you called it.
  105 +* `crossSlideStop()` will stop the slideshow and empty the container `div`; if the container was assigned a static background image or color by css, it will show through.
  106 +* `crossSlideRestart()` will stop the slideshow, if needed, and restart it from the beginning.
  107 +* `crossSlidePause()` will pause the slideshow.
  108 +* `crossSlideResume()` will resume a paused slideshow.
  109 +
  110 +## Ken Burns variant
  111 +
  112 +There is a variant to the Ken Burns effect that does away with the cross-fade between moving images, which is the phase most demanding on the browser's rendering engine.
  113 +
  114 +This effect is turned on with the `variant` option. To get a pleasing effect in this variant, a linear animation is not appropriate for the single-image phase, so CrossSlide defaults to jQuery's built-in `swing` mode. You can choose a different easing effect with the `easing` option, which will be applied to single-image phases only: cross-fade phases are still rendered with a linear animation. In this example I'm using `easeInOutQuad` from George McGinley Smith's [jQuery Easing Plugin][2].
  115 +
  116 + $('#placeholder').crossSlide({
  117 + fade: 1,
  118 + variant: true,
  119 + easing: 'easeInOutQuad'
  120 + }, [
  121 + // as above
  122 + ]);
  123 +
  124 +## How to use it
  125 +
  126 +Here are basic instructions on how to get CrossSlide to work:
  127 +
  128 +1. Download the jQuery library and include it in your page along with my plugin.
  129 +2. Put a block element somewhere in your page and give it a fixed size, even when emptied of its contents, by setting its width and height in the CSS stylesheet.
  130 +4. Open a script tag, define a jQuery *document ready handler* and activate my plugin on the element you created in step 2.
  131 +
  132 +## Things to keep in mind:
  133 +
  134 +* Keep an eye on the Error console, as that's where my script will post any error messages you are supposed to read, for example when the options you use don't make sense together.
  135 +* Make sure the browser can find the images you reference in the `src` attributes, relative to the path of the current page, because if the browser cannot load them, my plugin has no way to know and will just hang there, without any error messages.
  136 +* Don't put a comma after the last element in an array or object (this means don't put a comma just before a closing brace or bracket, even if it's on the previous line) because Internet Explorer won't like it and will refuse to run the script, again without any error message.
  137 +* Don't call `crossSlide()` on an empty set or on a set of more than one element. This is not supported and will raise an exception.
  138 +
  139 + [1]: http://tobia.github.com/Pause/
  140 + [2]: http://gsgd.co.uk/sandbox/jquery/easing/
0  LICENSE.txt → LICENSE
File renamed without changes
13 README.md
Source Rendered
... ... @@ -0,0 +1,13 @@
  1 +# CrossSlide
  2 +
  3 +CrossSlide was a jQuery plugin I developed in 2007 to create slideshow animations using plain Javascript, doing away with the need to use Adobe Flash™ or other binary plugins.
  4 +
  5 +It did so by creating a `<div style="overflow:hidden">` container that would hold several `<img style="position:absolute">` elements, so that the images could be moved around inside the container while staying clipped to the container's edges. It would then build a chain of *closures* that would call jQuery's `animate()` method for each animation phase, between each pair of keyframes, passing around the callback for the next phase. This was before jQuery's support for animation queues.
  6 +
  7 +**This code is obsolete** in more that one way. There are much better alternatives around to build slideshows without using binary plugins, including CSS3 animations and the Canvas element. Therefore **this plugin is unmaintained and unsupported**.
  8 +
  9 +In other words, **2007 called and wants its Javascript back**. CrossSlide users are encouraged to switch to one of the current slideshow packages, built on modern technologies.
  10 +
  11 +That being said, this project was released under the GPLv2 license, so anyone is free to fork it and continue development as they see fit. You can see any existing forks in the upper right corner of this project page.
  12 +
  13 +_–Tobia_
20 jquery.cross-slide.min.js
... ... @@ -1,20 +0,0 @@
1   -/*
2   - * CrossSlide jQuery plugin v0.6.2
3   - *
4   - * Copyright 2007-2010 by Tobia Conforto <tobia.conforto@gmail.com>
5   - *
6   - * This program is free software; you can redistribute it and/or modify it
7   - * under the terms of the GNU General Public License as published by the Free
8   - * Software Foundation; either version 2 of the License, or (at your option)
9   - * any later version.
10   - *
11   - * This program is distributed in the hope that it will be useful, but WITHOUT
12   - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14   - * more details.
15   - *
16   - * You should have received a copy of the GNU General Public License along with
17   - * this program; if not, write to the Free Software Foundation, Inc., 51
18   - * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   - */
20   -(function(){var d=jQuery,a=(d.fn.startAnimation?"startAnimation":"animate"),c="pause plugin missing.";function e(h){for(var g=1;g<arguments.length;g++){h=h.replace(new RegExp("\\{"+(g-1)+"}","g"),arguments[g])}return h}function f(){arguments[0]="CrossSlide: "+arguments[0];throw new Error(e.apply(null,arguments))}function b(i){var g=1;var h=i.replace(/^\s*|\s*$/g,"").split(/\s+/);if(h.length>3){throw new Error()}if(h[0]=="center"){if(h.length==1){h=["center","center"]}else{if(h.length==2&&h[1].match(/^[\d.]+x$/i)){h=["center","center",h[1]]}}}if(h.length==3){g=parseFloat(h[2].match(/^([\d.]+)x$/i)[1])}var j=h[0]+" "+h[1];if(j=="left top"||j=="top left"){return{xrel:0,yrel:0,zoom:g}}if(j=="left center"||j=="center left"){return{xrel:0,yrel:0.5,zoom:g}}if(j=="left bottom"||j=="bottom left"){return{xrel:0,yrel:1,zoom:g}}if(j=="center top"||j=="top center"){return{xrel:0.5,yrel:0,zoom:g}}if(j=="center center"){return{xrel:0.5,yrel:0.5,zoom:g}}if(j=="center bottom"||j=="bottom center"){return{xrel:0.5,yrel:1,zoom:g}}if(j=="right top"||j=="top right"){return{xrel:1,yrel:0,zoom:g}}if(j=="right center"||j=="center right"){return{xrel:1,yrel:0.5,zoom:g}}if(j=="right bottom"||j=="bottom right"){return{xrel:1,yrel:1,zoom:g}}return{xrel:parseInt(h[0].match(/^(\d+)%$/)[1])/100,yrel:parseInt(h[1].match(/^(\d+)%$/)[1])/100,zoom:g}}d.fn.crossSlide=function(i,k,l){var g=this,j=this.width(),h=this.height();if(g.length!=1){f("crossSlide() must be called on exactly 1 element")}g.get(0).crossSlideArgs=[i,k,l];k=d.map(k,function(m){return d.extend({},m)});if(!i.easing){i.easing=i.variant?"swing":"linear"}if(!l){l=function(){}}(function(o){var m=0;function n(q,p){p.onload=function(r){m++;k[q].width=p.width;k[q].height=p.height;if(m==k.length){o()}};p.src=k[q].src;if(q+1<k.length){n(q+1,new Image())}}n(0,new Image())})(function(){if(!i.fade){f("missing fade parameter.")}if(i.speed&&i.sleep){f("you cannot set both speed and sleep at the same time.")}var A=Math.round(i.fade*1000);if(i.sleep){var z=Math.round(i.sleep*1000)}if(i.speed){var o=i.speed/1000,v=Math.round(A*o)}g.empty().css({overflow:"hidden",padding:0});if(!/^(absolute|relative|fixed)$/.test(g.css("position"))){g.css({position:"relative"})}if(!g.width()||!g.height()){f("container element does not have its own width and height")}if(i.shuffle){k.sort(function(){return Math.random()-0.5})}for(var t=0;t<k.length;++t){var m=k[t];if(!m.src){f("missing src parameter in picture {0}.",t+1)}if(o){switch(m.dir){case"up":m.from={xrel:0.5,yrel:0,zoom:1};m.to={xrel:0.5,yrel:1,zoom:1};var x=m.height-h-2*v;break;case"down":m.from={xrel:0.5,yrel:1,zoom:1};m.to={xrel:0.5,yrel:0,zoom:1};var x=m.height-h-2*v;break;case"left":m.from={xrel:0,yrel:0.5,zoom:1};m.to={xrel:1,yrel:0.5,zoom:1};var x=m.width-j-2*v;break;case"right":m.from={xrel:1,yrel:0.5,zoom:1};m.to={xrel:0,yrel:0.5,zoom:1};var x=m.width-j-2*v;break;default:f("missing or malformed dir parameter in picture {0}.",t+1)}if(x<=0){f("impossible animation: either picture {0} is too small or div is too large or fade duration too long.",t+1)}m.time_ms=Math.round(x/o)}else{if(!z){if(!m.from||!m.to||!m.time){f("missing either speed/sleep option, or from/to/time params in picture {0}.",t+1)}try{m.from=b(m.from)}catch(w){f('malformed "from" parameter in picture {0}.',t+1)}try{m.to=b(m.to)}catch(w){f('malformed "to" parameter in picture {0}.',t+1)}if(!m.time){f('missing "time" parameter in picture {0}.',t+1)}m.time_ms=Math.round(m.time*1000)}}if(m.from){d.each([m.from,m.to],function(p,C){C.width=Math.round(m.width*C.zoom);C.height=Math.round(m.height*C.zoom);C.left=Math.round((j-C.width)*C.xrel);C.top=Math.round((h-C.height)*C.yrel)})}var s,y;y=s=d(e('<img src="{0}"/>',m.src));if(m.href){y=d(e('<a href="{0}"></a>',m.href)).append(s)}if(m.onclick){y.click(m.onclick)}if(m.alt){s.attr("alt",m.alt)}if(m.rel){y.attr("rel",m.rel)}if(m.href&&m.target){y.attr("target",m.target)}y.appendTo(g)}delete o;function n(D,C){var E=[0,A/(D.time_ms+2*A),1-A/(D.time_ms+2*A),1][C];return{left:Math.round(D.from.left+E*(D.to.left-D.from.left)),top:Math.round(D.from.top+E*(D.to.top-D.from.top)),width:Math.round(D.from.width+E*(D.to.width-D.from.width)),height:Math.round(D.from.height+E*(D.to.height-D.from.height))}}var u=g.find("img").css({position:"absolute",visibility:"hidden",top:0,left:0,border:0});u.eq(0).css({visibility:"visible"});if(!z){u.eq(0).css(n(k[0],i.variant?0:1))}var B=i.loop;function q(O,p){if(O%2==0){if(z){var E=O/2,S=(E-1+k.length)%k.length,P=u.eq(E),M=u.eq(S);var L=function(){l(E,P.get(0));M.css("visibility","hidden");setTimeout(p,z)}}else{var H=O/2,S=(H-1+k.length)%k.length,R=u.eq(H),M=u.eq(S),F=k[H].time_ms,N=n(k[H],i.variant?3:2);var L=function(){l(H,R.get(0));M.css("visibility","hidden");R[a](N,F,i.easing,p)}}}else{var D=Math.floor(O/2),G=Math.ceil(O/2)%k.length,Q=u.eq(D),C=u.eq(G),T={},K={visibility:"visible"},J={};if(G>D){K.opacity=0;J.opacity=1;if(i.doubleFade){T.opacity=0}}else{T.opacity=0;if(i.doubleFade){K.opacity=0;J.opacity=1}}if(!z){d.extend(K,n(k[G],0));if(!i.variant){d.extend(T,n(k[D],3));d.extend(J,n(k[G],1))}}if(d.isEmptyObject(J)){var L=function(){l(G,C.get(0),D,Q.get(0));C.css(K);Q[a](T,A,"linear",p)}}else{if(d.isEmptyObject(T)){var L=function(){l(G,C.get(0),D,Q.get(0));C.css(K);C[a](J,A,"linear",p)}}else{var L=function(){l(G,C.get(0),D,Q.get(0));C.css(K);C[a](J,A,"linear");Q[a](T,A,"linear",p)}}}}if(i.loop&&O==k.length*2-2){var I=L;L=function(){if(--B){I()}}}if(O>0){return q(O-1,L)}else{return L}}var r=q(k.length*2-1,function(){return r()});r()});return g};d.fn.crossSlideFreeze=function(){this.find("img").stop()};d.fn.crossSlideStop=function(){this.find("img").stop().remove()};d.fn.crossSlideRestart=function(){this.find("img").stop().remove();d.fn.crossSlide.apply(this,this.get(0).crossSlideArgs)};d.fn.crossSlidePause=function(){if(!d.fn.pause){f(c)}this.find("img").pause()};d.fn.crossSlideResume=function(){if(!d.fn.pause){f(c)}this.find("img").resume()}})();

0 comments on commit ca0b3f9

Please sign in to comment.
Something went wrong with that request. Please try again.