-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
Copy pathmouse-timeout.js
67 lines (58 loc) · 1.89 KB
/
mouse-timeout.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
/**
* Mouse timeout plugin
*
* After 3 seconds of mouse inactivity, add the css class
* `body.impress-mouse-timeout`. On `mousemove`, `click` or `touch`, remove the
* class.
*
* The use case for this plugin is to use CSS to hide elements from the screen
* and only make them visible when the mouse is moved. Examples where this
* might be used are: the toolbar from the toolbar plugin, and the mouse cursor
* itself.
*
* Example CSS:
*
* body.impress-mouse-timeout {
* cursor: none;
* }
* body.impress-mouse-timeout div#impress-toolbar {
* display: none;
* }
*
*
* Copyright 2016 Henrik Ingo (@henrikingo)
* Released under the MIT license.
*/
/* global window, document */
( function( document, window ) {
"use strict";
var timeout = 3;
var timeoutHandle;
var hide = function() {
// Mouse is now inactive
document.body.classList.add( "impress-mouse-timeout" );
};
var show = function() {
if ( timeoutHandle ) {
window.clearTimeout( timeoutHandle );
}
// Mouse is now active
document.body.classList.remove( "impress-mouse-timeout" );
// Then set new timeout after which it is considered inactive again
timeoutHandle = window.setTimeout( hide, timeout * 1000 );
};
document.addEventListener( "impress:init", function( event ) {
var api = event.detail.api;
var gc = api.lib.gc;
gc.addEventListener( document, "mousemove", show );
gc.addEventListener( document, "click", show );
gc.addEventListener( document, "touch", show );
// Set first timeout
show();
// Unset all this on teardown
gc.pushCallback( function() {
window.clearTimeout( timeoutHandle );
document.body.classList.remove( "impress-mouse-timeout" );
} );
}, false );
} )( document, window );