Skip to content

Instantly share code, notes, and snippets.

@jenshedqvist
Forked from 140bytes/LICENSE.txt
Created January 13, 2012 09:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenshedqvist/1605351 to your computer and use it in GitHub Desktop.
Save jenshedqvist/1605351 to your computer and use it in GitHub Desktop.
A slideshow with interval for image carousels and more!

Slideshow and more

A tweet-sized slideshow and DOM show/hide iteration utility.

Features

  • Shows/Hides a collection of DOM-elements with a certain interval
  • Interval delay can be set in ms
  • Index where the slideshow should start can be set

See examples below in test.html. The example uses JS 1.6 filter function, so don't expect it to run in IE<9.

IMPORTANT NOTICE: you shouldn't actually use this god-awful (wonderfully geeky rainbow) piece of crap code. Always prefer JSlinted, well formed and secure code over unstable, hard-to-read error-prone ninja code.

function(
e, // a collection of DOM elements
s, // speed of the slideshow in ms
c, // where to start the show (optional)
m, // define variable m ("max value")
i // define variable i ("iteration")
){
m = e.length - 1, // store the number of slides based on provided elements (@e)
c = c || 0; // an index of the currently visible item, will start from item one (0) in collection if not provided by @c
function f() { // a callback function to be called with interval
for(i = m; ~i; i--) // loop through the elements provided with @e backwards with a tilde "guard" that i "is not -1"
e[i].style.display = i == c ? '' : 'none'; // hide items that isn't current, show the one that is
c = ++c%(m+1); // add one (1) to current item index, if c becomes m+1 it set to 0 (no remainder)
}
f(); // call function right away to prevent FOUC when waiting for interval (@s) to kick in
setInterval(f, s); // start the show with the defined interval (@s)
}
function(e,s,m,c,p){m=e.length-1,c=c||0;function f(){for(i=m;~i;i--)e[i].style.display=i==c?"":"none";c=++c%(m+1);}f();setInterval(f,s);}
{
"name": "SlideshowAndMore",
"description": "A slideshow with interval for image carousels and more!",
"keywords": [
"slideshow",
"timer",
"images",
"spinner"
]
}
<!DOCTYPE html>
<title>Carousel</title>
<h2>Image Slideshow</h2>
<ul id="slideshow" style="list-style:none;">
<li><img src="http://www.placekitten.com/160/100" height="100" width="160"></li>
<li><img src="http://www.placekitten.com/g/160/100" height="100" width="160"></li>
<li><img src="http://www.placekitten.com/170/100" height="100" width="160"></li>
</ul>
<h2>Qoute Carousel <small>(start at 2:nd qoute)</small></h2>
<div id="steven_seagal_qoutes">
<blockqoute>"I am hoping that I can be known as a great writer and actor some day, rather than a sex symbol."</blockqoute>
<blockqoute>"Trust your doubt. Always fight for your beliefs. That is the path beyond thought."</blockqoute>
<blockqoute>"I was born with a serious spiritual consciousness and for many years studied different paths."</blockqoute>
- Steven Seagal
</div>
<h2>Spinner</h2>
<abbr title="Loading, please wait..." id="spinner" style="font-size: 25px; font-weight: bold; border: 0;">
<span>|</span>
<span>/</span>
<span>-</span>
<span>\</span>
<span>|</span>
<span>/</span>
<span>-</span>
<span>\</span>
</abbr>
<script>
var myFunction = function(e,s,c,m,i){m=e.length-1,c=c||0;function f(){for(i=m;~i;i--)e[i].style.display=i==c?"":"none";c=++c%(m+1);}f();setInterval(f,s);}
var SlideShow = myFunction;
var slides = getChildrenOf('slideshow');
SlideShow(slides, 1500);
var Carousel = myFunction;
var qoutes = getChildrenOf('steven_seagal_qoutes');
Carousel(qoutes, 3200, 1);
var Spinner = myFunction;
var chars = getChildrenOf('spinner');
Spinner(chars, 150);
function getChildrenOf(id) { return [].filter.call(document.getElementById(id).childNodes, function(y){return (y.nodeType === 1)}); };
</script>
@tsaniel
Copy link

tsaniel commented Jan 14, 2012

Save some bytes - not sure if it works correctly.
function(a,b,c){c|=function f(d,e){for(e=d=a.length;d--;)a[d].style.display=d^c%e?'none':'';c++;setTimeout(f,b)}()}

@atk
Copy link

atk commented Jan 16, 2012

Nice usage of |= there, @tsaniel!

@tsaniel
Copy link

tsaniel commented Jan 16, 2012

You are welcome :)
In fact, I originally want a function expression instead of function declaration, and I just combine it with the bitwise trick.

@maettig
Copy link

maettig commented Jan 16, 2012

@jenshedqvist, in your index.js and test.html the parameters c and m are switched.
@tsaniel, very good, but the first iteration is displayed twice. We can call this a feature (I like it) but if you want to fix it you need to add two bytes, I think.

function(a,b,c){function f(d,e){for(e=d=a.length;d--;)a[d].style.display=d^c%e?'none':'';c++;setTimeout(f,b)}f(c|=0)}

@tsaniel
Copy link

tsaniel commented Jan 17, 2012

My bad :(
What about this?
function(a,b,c){~function f(d,e){for(e=d=a.length;d--;)a[d].style.display=d^c%e?'none':'';c=-~c;setTimeout(f,b)}()}

@jenshedqvist
Copy link
Author

@maettig, fixed the order of the arguments. Also a typo that led to that i was leaked to the global scope in test.html.

@maettig
Copy link

maettig commented Jan 17, 2012

@tsaniel, wow, down to 115 bytes and still works. Can you explain why? I don't understand why adding ~ in front of function makes a difference.

I thought about using the remaining bytes for an opacity transition effect but transition support is insanely broken in Firefox (I start to think that Firefox becomes the new Internet Explorer, also see the problems I had with my dumpGlobalLeaks). It kind of works in Opera when adding some CSS like * { -o-transition: all .4s; }.

function(a,b,c){~function f(d,e){for(e=d=a.length;d--;)with(a[d].style)display=(opacity=d^c%e?0:1)?'':'none';c=-~c;setTimeout(f,b)}()}

@tsaniel
Copy link

tsaniel commented Jan 17, 2012

Unary operators (~, !, + ... etc) have to be followed by an expression, and thus the function there becomes a function expression instead of function declaration. For example, (function(){})()(expression) works while function identifier(){}()(declaration) makes an error.

http://kangax.github.com/nfe/

@jenshedqvist
Copy link
Author

If you want animations the easiest way is to modify the script so it adds and removes a class instead, and use CSS for the animations. I like that approach as it's modern progressive enhancement; if a slideshow doesn't support animation with CSS it get's a simple but functional show/hide "animation".

I thought I could use them nifty bitwise operators in some way but I've never been able to figure them out...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment