-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.iphone-switch.js
70 lines (56 loc) · 2.33 KB
/
jquery.iphone-switch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/************************************************
* jQuery iphoneSwitch plugin *
* *
* Author: Daniel LaBare *
* Date: 2/4/2008 *
************************************************/
jQuery.fn.iphoneSwitch = function(start_state, switched_on_callback, switched_off_callback, options) {
var state = start_state == 'on' ? start_state : 'off';
// define default settings
var settings = {
mouse_over: 'pointer',
mouse_out: 'default',
switch_on_container_path: 'iphone_switch_container_on.png',
switch_off_container_path: 'iphone_switch_container_off.png',
switch_path: 'iphone_switch.png',
switch_height: 27,
switch_width: 94
};
if(options) {
jQuery.extend(settings, options);
}
// create the switch
return this.each(function() {
var container;
var image;
// make the container
container = jQuery('<div class="iphone_switch_container" style="height:'+settings.switch_height+'px; width:'+settings.switch_width+'px; position: relative; overflow: hidden"></div>');
// make the switch image based on starting state
image = jQuery('<img class="iphone_switch" style="height:'+settings.switch_height+'px; width:'+settings.switch_width+'px; background-image:url('+settings.switch_path+'); background-repeat:none; background-position:'+(state == 'on' ? 0 : -53)+'px" src="'+(state == 'on' ? settings.switch_on_container_path : settings.switch_off_container_path)+'" /></div>');
// insert into placeholder
jQuery(this).html(jQuery(container).html(jQuery(image)));
jQuery(this).mouseover(function(){
jQuery(this).css("cursor", settings.mouse_over);
});
jQuery(this).mouseout(function(){
jQuery(this).css("background", settings.mouse_out);
});
// click handling
jQuery(this).click(function() {
if(state == 'on') {
jQuery(this).find('.iphone_switch').animate({backgroundPosition: -53}, "slow", function() {
jQuery(this).attr('src', settings.switch_off_container_path);
switched_off_callback();
});
state = 'off';
}
else {
jQuery(this).find('.iphone_switch').animate({backgroundPosition: 0}, "slow", function() {
switched_on_callback();
});
jQuery(this).find('.iphone_switch').attr('src', settings.switch_on_container_path);
state = 'on';
}
});
});
};