Last active
May 15, 2021 06:58
-
-
Save AnatoliyAkhmatov/2235e8ea3f8341c4d586ddd041940860 to your computer and use it in GitHub Desktop.
This file contains 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
window.onLeave = (f, single = false) => { | |
var showed = false | |
var fn = () => { | |
if (single ? !showed : true) { | |
if (single) showed = true; | |
f() | |
} | |
} | |
document.body.addEventListener('mouseout', e => { | |
let y = window.pageYOffset | |
let x = window.pageXOffset | |
if ((e.pageX < 1 || e.pageY < 50)) fn(); | |
setTimeout(() => { | |
if ((e.pageX >= window.innerWidth - 50 && x == window.pageXOffset) || (e.pageY <= 50 && y == window.pageYOffset)) fn(); | |
}, 200) | |
}) | |
window.addEventListener('beforeunload', e => { | |
fn() | |
return false | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment