-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (59 loc) · 2.05 KB
/
script.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
(function($) {
var play = function (gifPlayerWrapper) {
var image = $('<img/>');
image.attr('src', gifPlayerWrapper.data('url'));
image.width(gifPlayerWrapper.width());
image.height(gifPlayerWrapper.height());
image.addClass('gif-player');
gifPlayerWrapper.children('canvas').remove();
gifPlayerWrapper.append(image);
gifPlayerWrapper.addClass('play');
};
var pause = function (gifPlayerWrapper) {
var image = gifPlayerWrapper.children('img');
image.remove();
var canvas = $('<canvas>fail</canvas>');
canvas[0].width = gifPlayerWrapper.width();
canvas[0].height = gifPlayerWrapper.height();
canvas.width(gifPlayerWrapper.width());
canvas.height(gifPlayerWrapper.height());
if (gifPlayerWrapper.data('no-first-access') !== ''){
canvas.css('opacity', 0);
$.get(gifPlayerWrapper.data('url'), function () {
canvas[0].getContext("2d").drawImage(image[0], 0, 0, gifPlayerWrapper.width(), gifPlayerWrapper.height());
canvas.css('opacity', 1);
gifPlayerWrapper.data('no-first-access', '');
});
} else {
canvas[0].getContext("2d").drawImage(image[0], 0, 0, gifPlayerWrapper.width(), gifPlayerWrapper.height());
}
gifPlayerWrapper.append(canvas);
gifPlayerWrapper.removeClass('play');
};
window.gifPlayerInit = function() {
$(document).ready(function() {
console.log('two');
$('.gif-player').each(function () {
var $this = $(this);
$this.wrap("<div class='gif-player__wrapper'></div>");
var wrapper = $this.parent();
wrapper.data('url', $this.attr('src'));
wrapper.width($this.width());
wrapper.height($this.height());
if ($this.data('autoplay') === '') {
wrapper.addClass('play');
} else {
pause(wrapper);
}
});
$('.gif-player__wrapper').on('click', function () {
if (!$(this).hasClass('play')) {
play($(this));
} else {
pause($(this));
}
});
});
};
gifPlayerInit();
})(jQuery);