One of the sweet user interface enhancements provided by Apple’s iPhone is their checkbox-slider functionality. Thomas Reynolds recently released a jQuery plugin that allows you to make your checkboxes look like iPhone sliders. Here’s how to implement that functionality using the beloved MooTools javascript framework.
<h2>Off by default</h2> <p><input type="checkbox" /></p> <h2>On by default</h2> <p><input type="checkbox" checked="checked" /></p>
This is how the XHTML code is initially embedded within the page. MooTools will change this drastically.
.iPhoneCheckContainer{ position:relative; width:85px; height:27px; cursor:pointer; overflow:hidden; }
.iPhoneCheckContainer input{ position:absolute; top:5px; left:30px; }
.iPhoneCheckHandle{ display:block; height:27px; width:39px; cursor:pointer; position:absolute; top:0; left:0; }
.iPhoneCheckHandle .iPhoneCheckHandleBG{ position:absolute; width:5px; height:100%; top:0; left:0; z-index:1; }
.iPhoneCheckHandle .iPhoneCheckHandleSlider{ position:absolute; top:0; left:0; height:27px; width:39px; background:url(iphone-slider.png) no-repeat; z-index:2; }
label.iPhoneCheckLabelOn,label.iPhoneCheckLabelOff{ font-size:17px; line-height:17px; font-weight:bold; font-family:Helvetica Neue,Arial,Helvetica,sans-serif; text-transform:uppercase; cursor:pointer; display:block; height:22px; position:absolute; width:52px; top:0; }
label.iPhoneCheckLabelOn{ color:#fff; background:url(iphone-on.png) no-repeat; text-shadow:0px 0px 2px rgba(0,0,0,0.6); left:0; padding:5px 0 0 8px; }
label.iPhoneCheckLabelOff{ color:#8B8B8B; background:url(iphone-off.png) no-repeat right 0; text-shadow:0px 0px 2px rgba(255,255,255,0.6); text-align:right; right:0; padding:5px 8px 0 0; }
The CSS is pretty simple but notice we have to define classes for each state.
window.addEvent('domready',function(){
(function($) {
this.IPhoneCheckboxes = new Class({
//implements
Implements: [Options],
//options
options: {
checkedLabel: 'ON',
uncheckedLabel: 'OFF',
background: '#fff',
containerClass: 'iPhoneCheckContainer',
labelOnClass: 'iPhoneCheckLabelOn',
labelOffClass: 'iPhoneCheckLabelOff',
handleClass: 'iPhoneCheckHandle',
handleBGClass: 'iPhoneCheckHandleBG',
handleSliderClass: 'iPhoneCheckHandleSlider',
elements: 'input[type=checkbox]'
},
//initialization
initialize: function(options) {
//set options
this.setOptions(options);
//elements
this.elements = $$(this.options.elements);
//observe checkboxes
this.elements.each(function(el) {
this.observe(el);
},this);
},
//a method that does whatever you want
observe: function(el) {
//turn off opacity
el.set('opacity',0);
//create wrapper div
var wrap = new Element('div',{
'class': this.options.containerClass
}).inject(el.getParent());
//inject this checkbox into it
el.inject(wrap);
//now create subsquent divs and labels
var handle = new Element('div',{'class':this.options.handleClass}).inject(wrap);
var handlebg = new Element('div',{'class':this.options.handleBGClass,'style':this.options.background}).inject(handle);
var handleSlider = new Element('div',{'class':this.options.handleSliderClass}).inject(handle);
var offLabel = new Element('label',{'class':this.options.labelOffClass,text:this.options.uncheckedLabel}).inject(wrap);
var onLabel = new Element('label',{'class':this.options.labelOnClass,text:this.options.checkedLabel}).inject(wrap);
var rightSide = wrap.getSize().x - 39;
//fx instances
el.offFx = new Fx.Tween(offLabel,{'property':'opacity','duration':200});
el.onFx = new Fx.Tween(onLabel,{'property':'opacity','duration':200});
//mouseup / event listening
wrap.addEvent('mouseup',function() {
var is_onstate = !el.checked; //originally 0
var new_left = (is_onstate ? rightSide : 0);
var bg_left = (is_onstate ? 34 : 0);
handlebg.hide();
new Fx.Tween(handle,{
duration: 100,
'property': 'left',
onComplete: function() {
handlebg.setStyle('left',bg_left).show();
}
}).start(new_left);
//label animations
if(is_onstate) {
el.offFx.start(0);
el.onFx.start(1);
}
else {
el.offFx.start(1);
el.onFx.start(0);
}
//set checked
el.set('checked',is_onstate);
});
//initial load
if(el.checked){
offLabel.set('opacity',0);
onLabel.set('opacity',1);
handle.setStyle('left',rightSide);
handlebg.setStyle('left',34);
} else {
onLabel.set('opacity',0);
handlebg.setStyle('left',0);
}
}
});
})(document.id);
/* usage */
var chx = new IPhoneCheckboxes();
});
The class allows for customization of which events get iPhone-ized, the label text statements, and what each CSS class is called. Besides those given options, you have nothing else to do unless you plan on hacking the class completely.
Demo: http://davidwalsh.name/dw-content/iphone-checkboxes.php
Source: http://davidwalsh.name/iphone-checkboxes-mootools

Related Listings:
No comments yet.
RSS feed for comments on this post. TrackBack URL