This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jQuery.hoverable.js
93 lines (77 loc) · 2.4 KB
/
jQuery.hoverable.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(function($){
var plugin = {
name: 'hoverable'
};
var default_settings = {
overEvent: 'mouseenter', // jQuery event to register on
outEvent: 'mouseleave', // Idem
overClass: 'hoverable-over', // Class to be added when the item is hovered
outClass: 'hoverable-out' // Idem
};
function trigger_event(wrapper, event, options) {
wrapper.trigger(plugin.name + '.' + event, options);
}
function toggle_over(is_over) {
var settings = this.data(plugin.name).settings;
this
.toggleClass(settings.overClass, is_over)
.toggleClass(settings.outClass, !is_over);
trigger_event(this, is_over ? 'over' : 'out');
}
var methods = {
init: function(options) {
var settings = $.extend({}, default_settings, options);
var data = {
settings: settings
};
this.data(plugin.name, data);
methods.bind.call(this);
},
bind: function() {
// In case init is called multiple times
methods.unbind.call(this);
var settings = this.data(plugin.name).settings;
this
.removeClass(settings.overClass)
.addClass(settings.outClass)
.bind(settings.overEvent, methods.over)
.bind(settings.outEvent, methods.out);
},
unbind: function() {
var settings = this.data(plugin.name).settings;
this
.removeClass(settings.overClass)
.removeClass(settings.outClass)
.unbind(settings.overEvent, methods.over)
.unbind(settings.outEvent, methods.out);
},
destroy: function() {
methods.unbind.call(this);
this.removeData(plugin.name);
this.off(plugin.name);
},
over: function() {
toggle_over.call($(this), true);
},
out: function() {
toggle_over.call($(this), false);
}
};
$.fn[plugin.name] = function(method) {
var args = false;
if ( typeof method === 'object' || ! method ) {
// Constructor, method will hold its options
args = [method];
method = 'init';
} else if ( methods[method] ) {
// Method, shift method name to get its arguments
args = Array.prototype.slice.call(arguments, 1);
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.' + plugin.name );
return this;
}
return this.each(function(){
methods[method].apply($(this), args);
});
};
})(jQuery);