Mon. April 27, 12:45:54 PM
Don't forget Chrome! It, too, is based on WebKit. So Safari isn't the only good browser. :)
I've been working with JQuery a lot lately, both at work and at home, although my primary specialty is CSS, usually complex CSS. I have so far created several 'JQuery Sliders' as I like to call them. Then the thought occurred to me, could a JQuery Slider be created using only CSS?
If you want to skip the article and go straight to the final product view my CSS Only JQuery Emulation Slider Demo.
Note: you will need to use Safari in order to view the transition effect and I highly recommend that you do.
My mission was to create a working example without the aid of JavaScript, using layers in CSS and using CSS3 transitions to give the slider the necessary animation.
The HTML in it's most basic form consists of a container DIV and a simple unordered list. I also used P elements which could probably be removed depending on hardcore you want to be about extra elements. I did this because I needed to style the LIs differently than the text contained within the LIs such as floating, width etc.
<div id="slider">
<ul>
<li>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</li>
<li>
<p>Sed ut est. Curabitur molestie. Cras pellentesque.</p>
</li>
<li>
<p>Maecenas consequat consequat lectus.</p>
</li>
<li>
<p>In vel lorem quis turpis auctor volutpat.</p>
</li>
</ul>
</div>
I use the following CSS to style the above elements.
#slider, ul, li { height: 300px; }
#slider, li { position: relative; width: 500px; }
#slider
{
margin: auto;
overflow: hidden;
padding: 10px;
}
li { float: left; }
Next we need to set our UL which is the container of the slider's LIs. It needs a width set on it which is the width of the LIs X the total number of LIs. If each LI were 500px wide and we had 4 LIs then the total width would be 2000px. I should note that you will also most likely have a margin between each of the LIs which this would need to be accounted for in the above.
ul
{
list-style-type: none;
position: absolute;
left: 0px;
top: 0px;
width: 2000px;
}
Next we move our UL element left baed on what LI we want to show in our slider. Look at the example below to see how I accomplish this. Each color block represents a LI. The block positioned over the black area represents the current visible LI.

By far the most complicated part of this proof-of-concept is accomplishing all of this without the assistance of variables that JavaScript allows us to use. CSS variables are not yet a true reality (although one could argue that CSS Counters are a form of CSS Variables). To overcome this obstacle I used the CSS3 :target pseudo selector. The :target pseudo selector works with URL fragments like below:
<a href="#page-anchor">go to page anchor</a>
<div id="page-anchor"></div>
Using CSS we can actually style the page-anchor DIV if our page's URL contains #page-anchor like so: http://www.example.com/page.html#page-anchor.
#page-anchor:target { background: red; }
Since I do not have variables available to use I need to use the URL fragments to selectively show my LEFT and RIGHT arrows. If I didn't do this I wouldn't know which LI slide to move to next and I wouldn't know which slide LI I need to show. This would quickly defeat my ul { left: -500px } method. What I did was wrap my entire slider in 4 :target hooks which simulate variables:
<!-- :target hooks -->
<div id="a1">
<div id="a2">
<div id="a3">
<div id="a4">
<!-- 1 -->
<a href="#a2" class="up2 default">2</a>
<a href="#a2" class="up2">2</a>
<!-- 2 -->
<a href="#a1" class="down1">1</a>
<a href="#a3" class="up3">3</a>
<!-- 3 -->
<a href="#a2" class="down2">2</a>
<a href="#a4" class="up4">4</a>
<!-- 4 -->
<a href="#a3" class="down3">3</a>
</div> <!-- End #a4 -->
</div> <!-- End #a3 -->
</div> <!-- End #a2 -->
</div> <!-- End #a1 -->
I then use the following CSS to move our UL left 0px, -500px etc.
#a1:target ul { left: 0px; }
#a2:target ul { left: -500px; }
#a3:target ul { left: -1000px; }
#a4:target ul { left: -1500px; }
I next use these same hooks to hide/show the appropriate LEFT/RIGHT arrows:
#a1:target .up2,
#a2:target .down1,
#a2:target .up3,
#a3:target .down2,
#a3:target .up4,
#a4:target .down3
{ display: block; }
If you were to test out the demo up to this point you would notice that when you click the LEFT/RIGHT arrows, it would automatically switch to the next slide rather abruptly. To fully achieve our JQuery-like slider we need to use CSS3 Transitions which was originally proposed by the Webkit team and has since been adopted into canon by the W3C. What this means is, at the moment the animations will only work in Safari, but Opera and Firefox will not be far behind to implement this exciting new CSS feature.
To achieve the CSS animation we need to first define what CSS property we want to animate. We can do this by stating '-webkit-transition: left'. Next we state the amount of milliseconds we would like the transition to occur over by adding '.3s'. Finally we add linear to the end stating that it is a linear transition. Here is the fully property statement:
ul { -webkit-transition: left .3s linear; }
To view the final product (which I recommend you use Safari which supports CSS Transitions) view my CSS Only JQuery Slider Emulation Demo >>
Before you comment stating why on earth anybody would ever use this when it can be accomplished so much easier in JavaScript please remember this is just a proof of concept and a test to see if it could be done using only CSS.
Tags: css, css3, target ( Add Tag )
Don't forget Chrome! It, too, is based on WebKit. So Safari isn't the only good browser. :)
This is a pretty cool concept.
You might want to look into your RSS feed, though; the link throws a 404 error when followed...
Good work!
@Ben: Does the transition work in Google Chrome? I hadn't even thought of that, if it does, even better!
@Jason: Sorry about that, I have a pretty poor hosting package and it think the server is getting hit a bit harder than normal. You can alternately try this link: http://feeds2.feedburner.com/ng-tech
Jason, thanks for pointing out that my feeds wern't working. Apparently Feedburner was having some sort of meltdown with my account. I have it fixed now but some annoying things came out of it all.
This is some real clever stuff right here :-) Thanks for sharing!
Why would anynbody want to use css transitions instead of jQuery {js} ?. The iphone of course!
Web apps on iphone need to be light and snappy. They don't support flash and Jquery can be a bit heavy sometimes. We will see a lot of the css3 translations/transitions/animations on this platform.
Thanks for the good work
just found a problem that i don't know how to overcome. when you have more content on the page and a scrollbar the slider anchors to the top of page on click. Is there a way to prevent this?
@Maak
Haven't tried this, but could you prevent the jump by putting the slider in an iframe? Can someone test this out?
nice..keep it
The left and right arrows dont appear in Internet Explorer? is this a bug?
Site Coded And Designed By Neal Grosskopf Using No Crappy CMS Or Themes