-
Notifications
You must be signed in to change notification settings - Fork 9
/
jquery.swapimage.js
150 lines (130 loc) · 4.7 KB
/
jquery.swapimage.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* swapImage - $ plugin for swapping image(s)
*
* Copyright (c) 2010 tszming (tszming@gmail.com)
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
/**
* Enable image swapping, requires metadata plugin.
*
* @example $.swapImage(".swapImage");
* @desc Enable image swapping for all images with CSS class name equal to "swapImage", e.g.
* <img class="swapImage {src: 'images/new.gif'}" src="images/old.gif" />
*
* @param i Images to be selected.
* @param preload Preload the image, default = true.
* @param repeat Repeat the effect, default = true.
* @param swapInEvent Event for swap In.
* @param swapOutEvent Event for swap Out.
*
* @name $.swapImage
* @cat Plugins/SwapImage
* @author tszming (tszming@gmail.com)
* @version 1.0.5
*/
(function($) {
$.swapImage = function(i, preload, repeat, swapInEvent, swapOutEvent) {
$.swapImage.files = {};
$.swapImage.data = {};
$.swapImage.uuid = 0;
$.swapImage.init = function() {
var id = ++$.swapImage.uuid;
$(this).attr("swapImageId", id);
var data = $(this).metadata();
$.swapImage.data[id] = $.swapImage.data[id] || {};
if (typeof data.src != "undefined") {
$.swapImage.data[id]["src"] = data.src;
$.swapImage.files[data.src] = false;
}
$.each(
$.grep([[data.sin, "sin"], [data.sout, "sout"]],
function(n) {
return (typeof n[0] != "undefined" && n[0].length > 0);
}),
function() {
var arr = this[0];
var vname = this[1];
for (var i = 0; i < arr.length; i++) {
var idx = data[vname][i].indexOf(":");
var selection = data[vname][i].substring(0, idx);
var file = data[vname][i].substring(idx + 1);
$.swapImage.data[id][vname] = $.swapImage.data[id][vname] || [];
if (idx > 1) {
$.swapImage.data[id][vname].push([selection, file]);
$.swapImage.files[file] = false;
} else {
$.swapImage.data[id][vname].push([file]);
}
}
});
};
$.swapImage.preload = function() {
$.each($.swapImage.files,
function(k, v) {
if (v == false) {
$.swapImage.files[k] = true;
var img = new Image();
img.src = k;
}
});
};
$.swapImage.swapIn = function() {
$.swapImage.swap(this, "sin");
};
$.swapImage.swapOut = function() {
$.swapImage.swap(this, "sout");
};
$.swapImage.swap = function(obj, a) {
var id = $(obj).attr("swapImageId");
if (typeof $.swapImage.data[id][a] != "undefined") {
for (var i = 0; i < $.swapImage.data[id][a].length; i++) {
if ($.swapImage.data[id][a][i].length > 1) {
$($.swapImage.data[id][a][i][0]).attr("src", $.swapImage.data[id][a][i][1]);
} else {
$($.swapImage.data[id][a][i][0]).each($.swapImage._swap);
}
}
} else {
$.swapImage._swap.call(obj);
}
};
$.swapImage._swap = function(obj) {
var id = $(this).attr("swapImageId");
var data = $.swapImage.data[id];
if (typeof data.src != "undefined") {
var tmp = data.src;
data.src = this.src;
this.src = tmp;
}
};
$(document).ready(function() {
if (typeof repeat == "undefined") {
repeat = true;
}
if (typeof preload == "undefined") {
preload = true;
}
$(i).each($.swapImage.init);
if (typeof swapInEvent == "undefined" && typeof swapInEvent == "undefined") {
swapInEvent = "mouseenter";
swapOutEvent = "mouseleave";
}
if (repeat) {
if (typeof swapOutEvent != "undefined") {
$(i).bind(swapInEvent, $.swapImage.swapIn).bind(swapOutEvent, $.swapImage.swapOut);
} else {
$(i).bind(swapInEvent, $.swapImage.swapIn);
}
} else {
$(i).one(swapInEvent, $.swapImage.swapIn);
}
if (preload) {
$(i).each($.swapImage.preload);
}
});
};
})(jQuery);