Responsive CSS3 Any Content Slider (with JavaScript auto-play and large touch-toggle)
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, with a large touch-toggle in the slides' centre for easier tap activation on touch-screen devices. 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 click/tap inside the slides' active area 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
- A large touch-toggle area 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 large touch-toggle)
The first difference to the CSS-only version of the slider is that arrow labels invoke a stopSlider() function;
var arrow = document.getElementById('slider-outer').getElementsByTagName('label');
for(var i = 0; i < arrow.length; i++){
arrow[i].onclick = function(){stopSlider();}
}
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 the opening #slider div now invokes a toggleSlider() function;
var toggleControl = document.getElementById('toggle');
function toggleSlider(){
if(sliderOn){
stopSlider();
toggleControl.className = '';
setTimeout(function(){toggleControl.className = 'pause zoom';}, 1);
} else {
startSlider();
sliderOn = true;
toggleControl.className = '';
setTimeout(function(){toggleControl.className = 'play zoom';}, 1);
setTimeout(function(){toggleControl.className = '';}, 3000); // remove icon for IE9
}
}
document.getElementById('slider').onclick = function(){toggleSlider();}
View the source of this web page to snag the scripts and notice the "className" references to 'pause zoom' and 'play zoom' in the toggleSlider() function - they work on the additional #toggle div (placed after the slides), which acts as the large touch-toggle area for easy tap activation. The 'pause zoom' and 'play zoom' class-switch activates the CSS3 play/pause zoom animations, providing a nice visual indicator to show the user that they've interacted with the slides.
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 menu 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;
#toggle { display:none }
.js #toggle { display:block }
.js #slider { cursor:pointer }
Compatibility
- IE8+ (content accessible via horizontal scrollbar in IE8 (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 IE8. In IE7, only the first slide is visible, so you can either fiddle the CSS to make content lay vertically, or, avoid putting anything critical in slide 2 and onwards.
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, simple controls and nav pips)
- 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.