forked from jquerypageleave/jquery-pageleave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.pageleave.js
37 lines (34 loc) · 1008 Bytes
/
jquery.pageleave.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
// Create closure.
(function( $ ) {
var opts = times = start = null;
// Plugin definition.
$.fn.pageleave = function( options ) {
opts = $.extend( {}, $.fn.pageleave.defaultOptions, options );
times = opts.times;
start = new Date().getTime();
init();
};
function init() {
$(opts.container).on('mousemove.pageleave', function(evt) {
var elapsed = new Date().getTime() - start;
if ((evt.clientY <= opts.limitY) && (evt.clientX <= opts.limitX) && (elapsed >= opts.timeTillActive)) {
if (times > 0) times--;
if (typeof opts.callback == 'function') opts.callback.call(this);
else triggerEvent();
}
});
}
function triggerEvent() {
$(opts.container).trigger('pageleave');
if (times == 0) $(opts.container).off('mousemove.pageleave');
}
})( jQuery );
// Plugin defaults – added as a property on our plugin function.
$.fn.pageleave.defaultOptions = {
container: document,
limitX: screen.width,
limitY: 15,
timeTillActive: 5000,
times: 3,
callback: null
};