Created
January 30, 2015 15:10
-
-
Save juanbrujo/c18215fedd7ec400ccf7 to your computer and use it in GitHub Desktop.
Detect window blur/focus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .focus, | |
| .blur { | |
| transition: 1s; | |
| } | |
| .focus #focus, | |
| .blur #blur { | |
| display: inline-block; | |
| } | |
| .focus #blur, | |
| .blur #focus { | |
| display: none; | |
| } | |
| .focus { | |
| background: white; | |
| color: black; | |
| } | |
| .blur { | |
| background: black; | |
| color: white; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head></head> | |
| <body class="focus"> | |
| <div id="focus"> | |
| FOCUSED | |
| </div> | |
| <div id="blur"> | |
| BLURRED | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function windowBlurFocus(){ | |
| var focused = true; | |
| window.onfocus = function() { | |
| focused = true; | |
| onFocus(); | |
| }; | |
| window.onblur = function() { | |
| focused = false; | |
| onBlur(); | |
| }; | |
| } | |
| function onBlur() { | |
| document.title = "Blur"; | |
| document.body.className = 'blur'; | |
| }; | |
| function onFocus(){ | |
| document.title = "Focus"; | |
| document.body.className = 'focus'; | |
| }; | |
| windowBlurFocus(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment