Skip to content

Instantly share code, notes, and snippets.

@juanbrujo
Created January 30, 2015 15:10
Show Gist options
  • Select an option

  • Save juanbrujo/c18215fedd7ec400ccf7 to your computer and use it in GitHub Desktop.

Select an option

Save juanbrujo/c18215fedd7ec400ccf7 to your computer and use it in GitHub Desktop.
Detect window blur/focus
.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;
}
<html>
<head></head>
<body class="focus">
<div id="focus">
FOCUSED
</div>
<div id="blur">
BLURRED
</div>
</body>
</html>
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