Skip to content

Commit 0be226b

Browse files
committed
Add optional delay before opening parent dropdown.
- data-hover-delay="500" (Milliseconds) inizializes - Default is 0
1 parent ace10bf commit 0be226b

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

bootstrap-hover-dropdown.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@
3030
$parent = $this.parent(),
3131
defaults = {
3232
delay: 500,
33+
hoverDelay: 0,
3334
instantlyCloseOthers: true
3435
},
3536
data = {
3637
delay: $(this).data('delay'),
38+
hoverDelay: $(this).data('hover-delay'),
3739
instantlyCloseOthers: $(this).data('close-others')
3840
},
3941
showEvent = 'show.bs.dropdown',
4042
hideEvent = 'hide.bs.dropdown',
4143
// shownEvent = 'shown.bs.dropdown',
4244
// hiddenEvent = 'hidden.bs.dropdown',
4345
settings = $.extend(true, {}, defaults, options, data),
44-
timeout;
46+
timeout, timeoutHover;
4547

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

5456
openDropdown(event);
5557
}, function () {
58+
// clear timer for hover event
59+
window.clearTimeout(timeoutHover)
5660
timeout = window.setTimeout(function () {
5761
$parent.removeClass('open');
5862
$this.trigger(hideEvent);
@@ -90,14 +94,22 @@
9094
});
9195

9296
function openDropdown(event) {
93-
$allDropdowns.find(':focus').blur();
94-
95-
if(settings.instantlyCloseOthers === true)
96-
$allDropdowns.removeClass('open');
97-
97+
// clear dropdown timeout here so it doesnt close before it should
9898
window.clearTimeout(timeout);
99-
$parent.addClass('open');
100-
$this.trigger(showEvent);
99+
// restart hover timer
100+
window.clearTimeout(timeoutHover);
101+
// delay for hover event.
102+
timeoutHover = window.setTimeout(function () {
103+
$allDropdowns.find(':focus').blur();
104+
105+
if(settings.instantlyCloseOthers === true)
106+
$allDropdowns.removeClass('open');
107+
108+
// clear timer for hover event
109+
window.clearTimeout(timeoutHover);
110+
$parent.addClass('open');
111+
$this.trigger(showEvent);
112+
}, settings.hoverDelay);
101113
}
102114
});
103115
};

0 commit comments

Comments
 (0)