質問編集履歴

1

コードを追記しました。

2018/05/05 01:12

投稿

wpwbs585
wpwbs585

スコア19

test CHANGED
File without changes
test CHANGED
@@ -7,3 +7,549 @@
7
7
  のせたら丸投げに思われるので、回答してくれる方がいらっしゃったところで、
8
8
 
9
9
  backgroundあたりをのせてご教授願います。
10
+
11
+ 追記。最もレスポンシブ化したいjfancytile.jsのコードを載せませす。
12
+
13
+ (function($){
14
+
15
+ $.jfancytile = function(el, options){
16
+
17
+ var base = this;
18
+
19
+
20
+
21
+ base.$el = $(el);
22
+
23
+ base.el = el;
24
+
25
+
26
+
27
+ base.$el.data("jfancytile", base);
28
+
29
+
30
+
31
+ base.init = function(){
32
+
33
+ base.options = $.extend({},$.jfancytile.defaultOptions, options);
34
+
35
+
36
+
37
+ // Safari and Chrome load JS and CSS parallel. Therefor, image size can be wrong
38
+
39
+ // since the JS can be finished, while the CSS isn't. The following lines waits
40
+
41
+ // until the CSS is ready, so we can retrieve the correct data.
42
+
43
+ // via StackOverflow: http://tinyurl.com/kp6lqj
44
+
45
+ if (jQuery.browser.safari && document.readyState != "complete"){
46
+
47
+ setTimeout( arguments.callee, 100 );
48
+
49
+ return;
50
+
51
+ }
52
+
53
+
54
+
55
+ // Calculate the max container dimensions
56
+
57
+ // and save the image data
58
+
59
+ var imageData = { };
60
+
61
+ var container = { width : 0, height : 0 };
62
+
63
+ var imgSize = $("#demo1 img:first-of-type"); //カスタマイズした所
64
+
65
+
66
+
67
+ base.imageCounter = 0;
68
+
69
+ $(imgSize, base.$el).each(function() {
70
+
71
+ // Retrieve the image data
72
+
73
+ imageData[base.imageCounter] = {
74
+
75
+ src : $(this).attr("src"),
76
+
77
+ title : $(this).attr("alt"),
78
+
79
+
80
+
81
+ width : $(this).width(),
82
+
83
+
84
+
85
+ height : $(this).height()
86
+
87
+ };
88
+
89
+ base.imageCounter++;
90
+
91
+
92
+
93
+ // Get the max container dimensions
94
+
95
+ if( $(this).width() > container.width ) {
96
+
97
+ container.width = $(this).width();
98
+
99
+ }
100
+
101
+
102
+
103
+ if( $(this).height() > container.height ) {
104
+
105
+ container.height = $(this).height();
106
+
107
+ }
108
+
109
+ });//これより上記を直すべきと考えていますが、
110
+
111
+ //$(imgSize,base.$el)のところだと思います。
112
+
113
+
114
+
115
+ // Remove all list items
116
+
117
+ $("ul", base.$el).remove();
118
+
119
+
120
+
121
+ // Create backwards navigation
122
+
123
+ var navBack = $("<div />")
124
+
125
+ .attr("class", "jfancytilenav jfancytileBack")
126
+
127
+ .css({
128
+
129
+ height : container.height
130
+
131
+ })
132
+
133
+ .click(function(){
134
+
135
+ navigate("back");
136
+
137
+ })
138
+
139
+ .appendTo(base.$el);
140
+
141
+
142
+
143
+ // Create forwards navigation
144
+
145
+ var navForward = $("<div />")
146
+
147
+ .attr("class", "jfancytilenav jfancytileForward")
148
+
149
+ .css({
150
+
151
+ "height" : container.height,
152
+
153
+ "margin-left" : container.width - 50,
154
+
155
+ })
156
+
157
+ .click(function(){
158
+
159
+ navigate("forward");
160
+
161
+ })
162
+
163
+ .appendTo(base.$el);
164
+
165
+
166
+
167
+ // Create host container
168
+
169
+ var hostContainer = $("<div/>")
170
+
171
+ .attr("class", "jfancytileContainer")
172
+
173
+ .css({
174
+
175
+ width : container.width,
176
+
177
+ height : container.height,
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+ })
186
+
187
+ .appendTo(base.$el);
188
+
189
+
190
+
191
+ // Calculate the number of tiles needed
192
+
193
+ var totalNrOfTiles = base.options.rowCount * base.options.columnCount;
194
+
195
+
196
+
197
+ // Create the tiles
198
+
199
+
200
+
201
+ var first = true;
202
+
203
+ for(var img in imageData) {
204
+
205
+
206
+
207
+ // Create image container
208
+
209
+ var imageContainer = $("<div />")
210
+
211
+ .css({
212
+
213
+ "width" : imageData[img].width,
214
+
215
+ "height" : imageData[img].height,
216
+
217
+ "position" : "absolute",
218
+
219
+
220
+
221
+
222
+
223
+ });
224
+
225
+
226
+
227
+ // Position it in the center of the container
228
+
229
+ if(imageData[img].width < container.width) {
230
+
231
+ var margin = Math.floor((container.width - imageData[img].width) / 2);
232
+
233
+ imageContainer.css({ "margin-left" : margin + "px" });
234
+
235
+ }
236
+
237
+
238
+
239
+ if(imageData[img].height < container.height) {
240
+
241
+ var margin = Math.floor((container.height - imageData[img].height) / 2);
242
+
243
+ imageContainer.css({ "margin-top" : margin + "px" });
244
+
245
+ }
246
+
247
+
248
+
249
+
250
+
251
+ // Easy handler for the first, since we need to hide the others
252
+
253
+ if(first) {
254
+
255
+ imageContainer.attr("class", "jfancyfirst");
256
+
257
+ first = false;
258
+
259
+ }
260
+
261
+ imageContainer.appendTo($(hostContainer));
262
+
263
+
264
+
265
+ // Append the title
266
+
267
+ var titleContainer = $("<h3 />")
268
+
269
+ .html(imageData[img].title)
270
+
271
+ .attr("class", "jfancytileTitle")
272
+
273
+ .appendTo($(imageContainer));
274
+
275
+
276
+
277
+ // Calculate tile size
278
+
279
+ var tileDimension = { width : Math.floor((imageData[img].width / base.options.columnCount)), height : Math.floor((imageData[img].height / base.options.rowCount)) };
280
+
281
+
282
+
283
+ // Create the tiles
284
+
285
+ // Take note we record at which row and column we are, since we need to position the background position for each tile seperately
286
+
287
+ var tilePosition = { x : 0, y : 0 };
288
+
289
+ for(var i = 0; i < totalNrOfTiles; i++) {
290
+
291
+ var tile = $("<div />")
292
+
293
+ .css({
294
+
295
+ "float" : "left",
296
+
297
+ "position" : "relative",
298
+
299
+ "width" : tileDimension.width,
300
+
301
+ "height" : tileDimension.height,
302
+
303
+ "background-image" : "url(" + imageData[img].src + ")",
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+ "background-repeat":"no-repeat",  //カスタマイズしました。
312
+
313
+ "background-position" : "-" + (tilePosition.x * tileDimension.width) + "px -" + (tilePosition.y * tileDimension.height) + "px"
314
+
315
+ })
316
+
317
+ .appendTo($(imageContainer));
318
+
319
+
320
+
321
+ tilePosition.x++;
322
+
323
+ if(tilePosition.x > base.options.columnCount - 1) { // Minus one, since tilePosition is zero based
324
+
325
+ tilePosition.x = 0;
326
+
327
+ tilePosition.y++;
328
+
329
+ }
330
+
331
+ }
332
+
333
+ };
334
+
335
+
336
+
337
+ // Hide all the images, except the first one
338
+
339
+ base.$el.children().not("div.jfancytilenav").children().not(".jfancyfirst").children().not("h3").each(function() {
340
+
341
+ // Place on random position
342
+
343
+ var amount = Math.floor(Math.random() * base.options.maxTileShift+1);
344
+
345
+ var tileDimension = { width : $(this).width() * amount, height : $(this).height() * amount };
346
+
347
+
348
+
349
+ // Place on a random direction
350
+
351
+ var direction = Math.floor(Math.random() * 4);
352
+
353
+ switch (direction)
354
+
355
+ {
356
+
357
+ case 0:
358
+
359
+ $(this).css({ top: tileDimension.height, opacity : 0 });
360
+
361
+ break;
362
+
363
+ case 1:
364
+
365
+ $(this).css({ left: tileDimension.width, opacity : 0 });
366
+
367
+ break;
368
+
369
+ case 2:
370
+
371
+ $(this).css({ top: '-' + tileDimension.height + 'px', opacity : 0 });
372
+
373
+ break;
374
+
375
+ case 3:
376
+
377
+ $(this).css({ left: '-' + tileDimension.width + 'px', opacity : 0 });
378
+
379
+ break;
380
+
381
+ }
382
+
383
+ });
384
+
385
+ base.$el.children().not("div.jfancytilenav").children().not(".jfancyfirst").children().not("div").fadeOut(0);
386
+
387
+ };
388
+
389
+
390
+
391
+ var currentZindex = 1;
392
+
393
+ var currentImageIndex = 0;
394
+
395
+ var navigate = function(direction){
396
+
397
+
398
+
399
+ // Search for the tiles we need to animate, by searching from the root
400
+
401
+ base.$el.children().not("div.jfancytilenav").children().eq(currentImageIndex).children().not("h3").each(function() {
402
+
403
+
404
+
405
+ // The tiles should only move a maximum of 'maxTileShift' times their dimension
406
+
407
+ var amount = Math.floor(Math.random() * base.options.maxTileShift+1);
408
+
409
+ var tileDimension = { width : $(this).width() * amount, height : $(this).height() * amount };
410
+
411
+
412
+
413
+ // Animate to a random direction
414
+
415
+ var direction = Math.floor(Math.random() * 4);
416
+
417
+ switch (direction)
418
+
419
+ {
420
+
421
+ case 0:
422
+
423
+ $(this).animate({ top: '+=' + tileDimension.height, opacity:0 }, base.options.outSpeed, base.options.outEasing);
424
+
425
+ break;
426
+
427
+ case 1:
428
+
429
+ $(this).animate({ left: '+=' + tileDimension.width, opacity:0 }, base.options.outSpeed, base.options.outEasing);
430
+
431
+ break;
432
+
433
+ case 2:
434
+
435
+ $(this).animate({ top: '-=' + tileDimension.height, opacity:0 }, base.options.outSpeed, base.options.outEasing);
436
+
437
+ break;
438
+
439
+ case 3:
440
+
441
+ $(this).animate({ left: '-=' + tileDimension.width, opacity:0 }, base.options.outSpeed, base.options.outEasing);
442
+
443
+ break;
444
+
445
+ }
446
+
447
+ });
448
+
449
+
450
+
451
+ // Fade out the title
452
+
453
+ base.$el.children().not("div.jfancytilenav").children().eq(currentImageIndex).children().not("div").fadeOut(base.options.outSpeed);
454
+
455
+
456
+
457
+ // Show the next image based on the direction
458
+
459
+ // We also check if we're at the end of the slideshow and show the first one
460
+
461
+ // if we're at the first one and navigating back, we show the last one etc.
462
+
463
+ if(direction == "back") {
464
+
465
+ if(currentImageIndex != 0) {
466
+
467
+ currentImageIndex--;
468
+
469
+ } else {
470
+
471
+ currentImageIndex = base.imageCounter -1;
472
+
473
+ }
474
+
475
+ } else if (direction == "forward") {
476
+
477
+ if(currentImageIndex != base.imageCounter - 1) { // Minus one, zero based
478
+
479
+ currentImageIndex++;
480
+
481
+ } else {
482
+
483
+ currentImageIndex = 0;
484
+
485
+ }
486
+
487
+ }
488
+
489
+
490
+
491
+ // Bring the container to the foreground
492
+
493
+ currentZindex++;
494
+
495
+ base.$el.children().not("div.jfancytilenav").children().eq(currentImageIndex).css({ "z-index" : currentZindex });
496
+
497
+
498
+
499
+ base.$el.children().not("div.jfancytilenav").children().eq(currentImageIndex).children().not("h3").each(function() {
500
+
501
+ // Animate them back
502
+
503
+ $(this).animate({ top : 0, left:0, opacity:1 }, base.options.inSpeed, base.options.inEasing);
504
+
505
+ });
506
+
507
+
508
+
509
+ base.$el.children().not("div.jfancytilenav").children().eq(currentImageIndex).children().not("div").fadeIn(base.options.inSpeed);
510
+
511
+ };
512
+
513
+
514
+
515
+ // Run initializer
516
+
517
+ base.init();
518
+
519
+ };
520
+
521
+
522
+
523
+ $.jfancytile.defaultOptions = {
524
+
525
+ inEasing: "swing",
526
+
527
+ outEasing: "swing",
528
+
529
+ inSpeed: 1000,
530
+
531
+ outSpeed: 1000,
532
+
533
+ rowCount: 8,
534
+
535
+ columnCount: 13,
536
+
537
+ maxTileShift: 3
538
+
539
+ };
540
+
541
+
542
+
543
+ $.fn.jfancytile = function(options){
544
+
545
+ return this.each(function(){
546
+
547
+ (new $.jfancytile(this, options));
548
+
549
+ });
550
+
551
+ };
552
+
553
+
554
+
555
+ })(jQuery);