diff options
author | JinweiClarkChao <[email protected]> | 2014-09-23 22:34:14 +0800 |
---|---|---|
committer | Jinwei Zhao <[email protected]> | 2017-01-13 15:07:44 +0800 |
commit | b214f38d2c24a890ce36512a0c8bab965e27a508 (patch) | |
tree | 1522b651444407cbf120798e79be4657f1b472ab /Code/ZJW/javascripts/jquery.swatchbook.js | |
parent | e13882d1656b143533cec307a19425e0a56ec999 (diff) | |
download | jinwei.me-b214f38d2c24a890ce36512a0c8bab965e27a508.tar.gz |
README
Diffstat (limited to 'Code/ZJW/javascripts/jquery.swatchbook.js')
-rw-r--r-- | Code/ZJW/javascripts/jquery.swatchbook.js | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/Code/ZJW/javascripts/jquery.swatchbook.js b/Code/ZJW/javascripts/jquery.swatchbook.js new file mode 100644 index 0000000..dc6ce43 --- /dev/null +++ b/Code/ZJW/javascripts/jquery.swatchbook.js | |||
@@ -0,0 +1,255 @@ | |||
1 | /** | ||
2 | * jquery.swatchbook.js v1.1.0 | ||
3 | * http://www.codrops.com | ||
4 | * | ||
5 | * Licensed under the MIT license. | ||
6 | * http://www.opensource.org/licenses/mit-license.php | ||
7 | * | ||
8 | * Copyright 2012, Codrops | ||
9 | * http://www.codrops.com | ||
10 | */ | ||
11 | |||
12 | ; | ||
13 | (function($, window, undefined) { | ||
14 | |||
15 | 'use strict'; | ||
16 | |||
17 | // global | ||
18 | var Modernizr = window.Modernizr; | ||
19 | |||
20 | jQuery.fn.reverse = [].reverse; | ||
21 | |||
22 | $.SwatchBook = function(options, element) { | ||
23 | |||
24 | this.$el = $(element); | ||
25 | this._init(options); | ||
26 | |||
27 | }; | ||
28 | |||
29 | $.SwatchBook.defaults = { | ||
30 | // index of initial centered item | ||
31 | center: 6, | ||
32 | // number of degrees that is between each item | ||
33 | angleInc: 8, | ||
34 | speed: 700, | ||
35 | easing: 'ease', | ||
36 | // amount in degrees for the opened item's next sibling | ||
37 | proximity: 45, | ||
38 | // amount in degrees between the opened item's next siblings | ||
39 | neighbor: 4, | ||
40 | // animate on load | ||
41 | onLoadAnim: true, | ||
42 | // if it should be closed by default | ||
43 | initclosed: false, | ||
44 | // index of the element that when clicked, triggers the open/close function | ||
45 | // by default there is no such element | ||
46 | closeIdx: -1, | ||
47 | // open one specific item initially (overrides initclosed) | ||
48 | openAt: -1 | ||
49 | }; | ||
50 | |||
51 | $.SwatchBook.prototype = { | ||
52 | |||
53 | _init: function(options) { | ||
54 | |||
55 | this.options = $.extend(true, {}, $.SwatchBook.defaults, options); | ||
56 | |||
57 | this.$items = this.$el.children('div'); | ||
58 | this.itemsCount = this.$items.length; | ||
59 | this.current = -1; | ||
60 | this.support = Modernizr.csstransitions; | ||
61 | this.cache = []; | ||
62 | |||
63 | if (this.options.onLoadAnim) { | ||
64 | this._setTransition(); | ||
65 | } | ||
66 | |||
67 | if (!this.options.initclosed) { | ||
68 | this._center(this.options.center, this.options.onLoadAnim); | ||
69 | } else { | ||
70 | |||
71 | this.isClosed = true; | ||
72 | if (!this.options.onLoadAnim) { | ||
73 | this._setTransition(); | ||
74 | } | ||
75 | |||
76 | } | ||
77 | |||
78 | if (this.options.openAt >= 0 && this.options.openAt < this.itemsCount) { | ||
79 | this._openItem(this.$items.eq(this.options.openAt)); | ||
80 | } | ||
81 | |||
82 | this._initEvents(); | ||
83 | |||
84 | }, | ||
85 | _setTransition: function() { | ||
86 | |||
87 | if (this.support) { | ||
88 | this.$items.css({ | ||
89 | 'transition': 'all ' + this.options.speed + 'ms ' + this.options.easing | ||
90 | }); | ||
91 | } | ||
92 | |||
93 | }, | ||
94 | _openclose: function() { | ||
95 | |||
96 | this.isClosed ? this._center(this.options.center, true) : this.$items.css({ | ||
97 | 'transform': 'rotate(0deg)' | ||
98 | }); | ||
99 | this.isClosed = !this.isClosed; | ||
100 | |||
101 | }, | ||
102 | _center: function(idx, anim) { | ||
103 | |||
104 | var self = this; | ||
105 | |||
106 | this.$items.each(function(i) { | ||
107 | |||
108 | var transformStr = 'rotate(' + (self.options.angleInc * (i - idx)) + 'deg)'; | ||
109 | $(this).css({ | ||
110 | 'transform': transformStr | ||
111 | }); | ||
112 | |||
113 | }); | ||
114 | |||
115 | }, | ||
116 | _openItem: function($item) { | ||
117 | |||
118 | var itmIdx = $item.index(); | ||
119 | |||
120 | if (itmIdx !== this.current) { | ||
121 | |||
122 | if (this.options.closeIdx !== -1 && itmIdx === this.options.closeIdx) { | ||
123 | |||
124 | this._openclose(); | ||
125 | this._setCurrent(); | ||
126 | |||
127 | } else { | ||
128 | |||
129 | this._setCurrent($item); | ||
130 | $item.css({ | ||
131 | 'transform': 'rotate(0deg)' | ||
132 | }); | ||
133 | this._rotateSiblings($item); | ||
134 | |||
135 | } | ||
136 | |||
137 | } | ||
138 | |||
139 | }, | ||
140 | _initEvents: function() { | ||
141 | |||
142 | var self = this; | ||
143 | |||
144 | this.$items.on('click.swatchbook', function(event) { | ||
145 | self._openItem($(this)); | ||
146 | }); | ||
147 | |||
148 | }, | ||
149 | _rotateSiblings: function($item) { | ||
150 | |||
151 | var self = this, | ||
152 | idx = $item.index(), | ||
153 | $cached = this.cache[idx], | ||
154 | $siblings; | ||
155 | |||
156 | if ($cached) { | ||
157 | $siblings = $cached; | ||
158 | } else { | ||
159 | |||
160 | $siblings = $item.siblings(); | ||
161 | this.cache[idx] = $siblings; | ||
162 | |||
163 | } | ||
164 | |||
165 | $siblings.each(function(i) { | ||
166 | |||
167 | var rotateVal = i < idx ? | ||
168 | self.options.angleInc * (i - idx) : | ||
169 | i - idx === 1 ? | ||
170 | self.options.proximity : | ||
171 | self.options.proximity + (i - idx - 1) * self.options.neighbor; | ||
172 | |||
173 | var transformStr = 'rotate(' + rotateVal + 'deg)'; | ||
174 | |||
175 | $(this).css({ | ||
176 | 'transform': transformStr | ||
177 | }); | ||
178 | |||
179 | }); | ||
180 | |||
181 | }, | ||
182 | _setCurrent: function($el) { | ||
183 | |||
184 | this.current = $el ? $el.index() : -1; | ||
185 | this.$items.removeClass('ff-active'); | ||
186 | if ($el) { | ||
187 | $el.addClass('ff-active'); | ||
188 | } | ||
189 | |||
190 | } | ||
191 | |||
192 | }; | ||
193 | |||
194 | var logError = function(message) { | ||
195 | |||
196 | if (window.console) { | ||
197 | |||
198 | window.console.error(message); | ||
199 | |||
200 | } | ||
201 | |||
202 | }; | ||
203 | |||
204 | $.fn.swatchbook = function(options) { | ||
205 | |||
206 | var instance = $.data(this, 'swatchbook'); | ||
207 | |||
208 | if (typeof options === 'string') { | ||
209 | |||
210 | var args = Array.prototype.slice.call(arguments, 1); | ||
211 | |||
212 | this.each(function() { | ||
213 | |||
214 | if (!instance) { | ||
215 | |||
216 | logError("cannot call methods on swatchbook prior to initialization; " + | ||
217 | "attempted to call method '" + options + "'"); | ||
218 | return; | ||
219 | |||
220 | } | ||
221 | |||
222 | if (!$.isFunction(instance[options]) || options.charAt(0) === "_") { | ||
223 | |||
224 | logError("no such method '" + options + "' for swatchbook instance"); | ||
225 | return; | ||
226 | |||
227 | } | ||
228 | |||
229 | instance[options].apply(instance, args); | ||
230 | |||
231 | }); | ||
232 | |||
233 | } else { | ||
234 | |||
235 | this.each(function() { | ||
236 | |||
237 | if (instance) { | ||
238 | |||
239 | instance._init(); | ||
240 | |||
241 | } else { | ||
242 | |||
243 | instance = $.data(this, 'swatchbook', new $.SwatchBook(options, this)); | ||
244 | |||
245 | } | ||
246 | |||
247 | }); | ||
248 | |||
249 | } | ||
250 | |||
251 | return instance; | ||
252 | |||
253 | }; | ||
254 | |||
255 | })(jQuery, window); \ No newline at end of file | ||