-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.hoverplay.js
60 lines (60 loc) · 1.47 KB
/
jquery.hoverplay.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
/**
* jQuery.hoverPlay
* @version 1.0
* @author biohzrdmx <github.com/biohzrdmx>
* @requires jQuery 1.8+
* @license MIT
* @copyright Copyright © 2018 biohzrdmx. All rights reserved.
*/
;(function($) {
$.hoverPlay = {};
$.fn.hoverPlay = function(options) {
if (!this.length) { return this; }
var opts = $.extend(true, {}, $.hoverPlay.defaults, options);
this.each(function() {
var el = $(this),
video = el.get(0);
if (typeof video['play'] === 'function') {
video.controls = false;
video.loop = true;
el.on('mouseover', function() {
var timeout = el.data('hoverPlayTimeout');
if (!timeout) {
timeout = setTimeout(function() {
el.data('hoverPlayTimeout', null);
opts.callbacks.play(el, video);
el.trigger('hoverPlay');
}, opts.playDelay);
el.data('hoverPlayTimeout', timeout);
}
}).on('mouseout', function() {
var timeout = el.data('hoverPlayTimeout');
if (timeout) {
clearTimeout(timeout);
el.data('hoverPlayTimeout', null);
}
setTimeout(function() {
opts.callbacks.pause(el, video);
el.trigger('hoverPause');
}, opts.pauseDelay);
});
}
});
return this;
};
$.hoverPlay.defaults = {
playDelay: 350,
pauseDelay: 0,
callbacks: {
play: function(el, video) {
video.play();
},
pause: function(el, video) {
video.pause();
}
}
};
jQuery(document).ready(function($) {
$('[data-play=hover]').hoverPlay();
});
})(jQuery);