Skip to content

Commit

Permalink
Add optional delay before opening parent dropdown.
Browse files Browse the repository at this point in the history
- data-hover-delay="500" (Milliseconds) inizializes
- Default is 0
  • Loading branch information
redavis committed Feb 6, 2015
1 parent ace10bf commit 0be226b
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions bootstrap-hover-dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@
$parent = $this.parent(),
defaults = {
delay: 500,
hoverDelay: 0,
instantlyCloseOthers: true
},
data = {
delay: $(this).data('delay'),
hoverDelay: $(this).data('hover-delay'),
instantlyCloseOthers: $(this).data('close-others')
},
showEvent = 'show.bs.dropdown',
hideEvent = 'hide.bs.dropdown',
// shownEvent = 'shown.bs.dropdown',
// hiddenEvent = 'hidden.bs.dropdown',
settings = $.extend(true, {}, defaults, options, data),
timeout;
timeout, timeoutHover;

$parent.hover(function (event) {
// so a neighbor can't open the dropdown
Expand All @@ -53,6 +55,8 @@

openDropdown(event);
}, function () {
// clear timer for hover event
window.clearTimeout(timeoutHover)
timeout = window.setTimeout(function () {
$parent.removeClass('open');
$this.trigger(hideEvent);
Expand Down Expand Up @@ -90,14 +94,22 @@
});

function openDropdown(event) {
$allDropdowns.find(':focus').blur();

if(settings.instantlyCloseOthers === true)
$allDropdowns.removeClass('open');

// clear dropdown timeout here so it doesnt close before it should
window.clearTimeout(timeout);
$parent.addClass('open');
$this.trigger(showEvent);
// restart hover timer
window.clearTimeout(timeoutHover);
// delay for hover event.
timeoutHover = window.setTimeout(function () {
$allDropdowns.find(':focus').blur();

if(settings.instantlyCloseOthers === true)
$allDropdowns.removeClass('open');

// clear timer for hover event
window.clearTimeout(timeoutHover);
$parent.addClass('open');
$this.trigger(showEvent);
}, settings.hoverDelay);
}
});
};
Expand Down

0 comments on commit 0be226b

Please sign in to comment.