Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow timed delay which is cancelled by the mouse re-entering. Fixes #48. #55

Merged
merged 1 commit into from
Aug 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Ouibounce offers a few options, such as:
- [Sensitivity](#sensitivity)
- [Aggressive mode](#aggressive-mode)
- [Timer](#set-a-min-time-before-ouibounce-fires)
- [Delay](#delay)
- [Callback](#callback)
- [Cookie expiration](#cookie-expiration)
- [Cookie domain](#cookie-domain)
Expand Down Expand Up @@ -101,6 +102,16 @@ _Example:_
ouibounce(document.getElementById('ouibounce-modal'), { timer: 0 });
```

##### Delay
By default, Ouibounce will show the modal immediately. You could instead configure it to wait `x` milliseconds before showing the modal. If the user's mouse re-enters the body before `delay` ms have passed, the modal will not appear. This can be used to provide a "grace period" for visitors instead of immediately presenting the modal window.

_Example:_

```js
// Wait 100 ms
ouibounce(document.getElementById('ouibounce-modal'), { delay: 100 });
```

##### Callback
You can add a callback, which is a function that will run once Ouibounce has been triggered, by using the `callback` option.

Expand Down
23 changes: 19 additions & 4 deletions source/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ function ouibounce(el, config) {
aggressive = config.aggressive || false,
sensitivity = setDefault(config.sensitivity, 20),
timer = setDefault(config.timer, 1000),
delay = setDefault(config.delay, 0),
callback = config.callback || function() {},
cookieExpire = setDefaultCookieExpire(config.cookieExpire) || '',
cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '',
sitewide = config.sitewide === true ? ';path=/' : '',
_delayTimer = null,
_html = document.getElementsByTagName('html')[0];

function setDefault(_property, _default) {
Expand All @@ -26,13 +28,21 @@ function ouibounce(el, config) {
setTimeout(attachOuiBounce, timer);
function attachOuiBounce() {
_html.addEventListener('mouseleave', handleMouseleave);
_html.addEventListener('mouseenter', handleMouseenter);
_html.addEventListener('keydown', handleKeydown);
}

function handleMouseleave(e) {
if (e.clientY > sensitivity || (checkCookieValue('viewedOuibounceModal', 'true') && !aggressive)) return;
fire();
callback();

_delayTimer = setTimeout(_fireAndCallback, delay);
}

function handleMouseenter(e) {
if (_delayTimer) {
clearTimeout(_delayTimer);
_delayTimer = null;
}
}

var disableKeydown = false;
Expand All @@ -41,8 +51,7 @@ function ouibounce(el, config) {
else if(!e.metaKey || e.keyCode != 76) return;

disableKeydown = true;
fire();
callback();
_delayTimer = setTimeout(_fireAndCallback, delay);
}

function checkCookieValue(cookieName, value) {
Expand All @@ -60,6 +69,11 @@ function ouibounce(el, config) {
return cookies[cookieName] === value;
}

function _fireAndCallback() {
fire();
callback();
}

function fire() {
// You can use ouibounce without passing an element
// https://github.com/carlsednaoui/ouibounce/issues/30
Expand Down Expand Up @@ -92,6 +106,7 @@ function ouibounce(el, config) {

// remove listeners
_html.removeEventListener('mouseleave', handleMouseleave);
_html.removeEventListener('mouseenter', handleMouseenter);
_html.removeEventListener('keydown', handleKeydown);
}

Expand Down