Responsive CSS3 Any Content Slider (with JavaScript auto-play, simple controls and nav pips)
This slider is the next step on from the CSS-only version, using a very small amount of JavaScript to create an "auto-play" feature. Please read the Notes section for details about JavaScript enhancement.
Click on the circular ◄ and ► arrow controls to stop auto-play and manually move between slides, and use the play/pause toggle to start/stop auto-play...
Features
A CSS3, responsive content slider that;
- Can contain anything - images, text, whatever!
- Cycles continuously - doesn't stop at the last slide
- Uses CSS3 transitions to animate the slide effect (no JavaScript)
- Uses the "advanced checkbox hack" to trigger slides (for Android/iOS)
- Uses "translate3d" to force hardware acceleration in WebKit (no flicker)
- "Auto-play" capabilities are provided with a small amount of JavaScript
- Simple controls to stop and start "auto-play"
- Degrades gracefully with JavaScript turned off
Please view the source of this web page to grab the JS, CSS and HTML markup.
Notes (JavaScript auto-play and simple controls)
The first difference to the CSS-only version of the slider is that arrow labels invoke the stopSlider() function;
var arrow = document.getElementById('slider-outer').getElementsByTagName('label');
for(var i = 0; i < arrow.length; i++){
arrow[i].onclick = function(){
stopSlider();
stopSliderCtrl.style.display = 'none';
startSliderCtrl.style.display = 'block';
}
}
This is to stop the slider once the user interacts with it, which is just good manners because it would be bad for UX to have it wandering off, doing it's own thing, in the middle of a conversation... You know what I mean.
And extra play/pause control toggle markup that invokes the same stopSlider() function, and a startSlider() function;
var stopSliderCtrl = document.getElementById('stop-slider');
var startSliderCtrl = document.getElementById('start-slider');
stopSliderCtrl.onclick = function(){
stopSlider();
stopSliderCtrl.style.display = 'none';
startSliderCtrl.style.display = 'block';
}
startSliderCtrl.onclick = function(){
startSlider();
stopSliderCtrl.style.display = 'block';
startSliderCtrl.style.display = 'none';
}
View the source of this web page for those JS functions. The fun stuff - the "auto-play" feature - is provided courtesy of the JavaScript below. I've also included an extra line that adds a class of "js" to the <html> element when JavaScript is enabled which, when coupled with the appropriate CSS, makes the slider degrade back to the CSS-only version when JavaScript is turned off;
document.documentElement.className = 'js'; // adds class="js" to <html> element
function autoSlider(slider){
var slides = document.getElementsByName(slider);
for(var i = 1; i < slides.length; ++i){
if(slides[i].checked == true){
(navigator.appVersion.indexOf("MSIE 9") == -1) ? jump = 2 : jump = 3;
(i == slides.length - jump) ? slides[1].checked = true : slides[i+1].checked = true;
break;
}
}
}
...
This JSFiddle demonstrates the gist of what the JavaScript is doing to "auto-play" the slides - you can't see the radios activate in sequence in the demo above because they're hidden with CSS! And here's the demo with visible radios and slides.
This leaves us with a few tweaks to make sure things look good when JavaScript is disabled. We'll call on good ol' mister CSS for that - this will hide the play/pause control toggle. Note the ".js" selector that only applies when JavaScript is enabled;
#slider-outer .ctrl { display:none }
.js #slider-outer .ctrl { display:inline }
Compatibility
- IE7+ (content accessible via horizontal scrollbar in IE7/8 (no auto-play), controls but no slide effect in IE9) *
- Chrome
- Safari
- Firefox
- Opera
- Android
- iOS
* Conditional styles have been provided to replace the arrow controls with a horizontal scrollbar in IE7/8.
I haven't been able to test in other browsers/devices but feedback is always welcome. Contact me if you spot anything hinky.
Related
- Also see Responsive CSS3 Any Content Slider (no JavaScript) - text and images, but no nav pips
- Also see Responsive CSS3 Any Content Slider (no JavaScript) - text, images and nav pips
- Also see Responsive CSS3 Any Content Slider (no JavaScript) - images and nav pips
- Also see Responsive CSS3 Any Content Slider (with JavaScript auto-play and simple controls)
- Also see Responsive CSS3 Any Content Slider (with JavaScript auto-play and large touch-toggle)
- Also see Responsive CSS3 Any Content Slider (with JavaScript auto-play, large touch-toggle and swipe support)
Freebies
Looking for more freebies for your website? Grab a bunch of free PHP, CSS and JavaScript goodies, from flat file CMS' to RSS managers to responsive CSS menus, galleries and sliders.