From 0be226b3163b94c1853d5d316b7b971eff07b11e Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Fri, 6 Feb 2015 17:51:48 -0600 Subject: [PATCH] Add optional delay before opening parent dropdown. - data-hover-delay="500" (Milliseconds) inizializes - Default is 0 --- bootstrap-hover-dropdown.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/bootstrap-hover-dropdown.js b/bootstrap-hover-dropdown.js index 0536f20..373c0ca 100644 --- a/bootstrap-hover-dropdown.js +++ b/bootstrap-hover-dropdown.js @@ -30,10 +30,12 @@ $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', @@ -41,7 +43,7 @@ // 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 @@ -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); @@ -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); } }); };