|
1 | 1 | /* |
2 | | - * Bootstrap Dropdown Hover - v1.0.3 |
| 2 | + * Bootstrap Dropdown Hover - v1.0.4 |
3 | 3 | * Open dropdown menus on mouse hover, the proper way. |
4 | 4 | * http://www.virtuosoft.eu/code/bootstrap-dropdown-hover/ |
5 | 5 | * |
|
18 | 18 | }, |
19 | 19 | _hideTimeoutHandler, |
20 | 20 | _hardOpened = false, |
21 | | - _mouseDetected = false, |
22 | | - _mouseEnterDetected = false; |
| 21 | + _touchstartDetected = false, |
| 22 | + _mouseDetected = false; |
23 | 23 |
|
24 | 24 | // The actual plugin constructor |
25 | 25 | function BootstrapDropdownHover(element, options) { |
|
31 | 31 | } |
32 | 32 |
|
33 | 33 | function bindEvents(dropdown) { |
34 | | - // we do not want to modify behavior if mouse is not present (touch screens), |
35 | | - // so we rely on mousemove to detect the mouse |
36 | | - $('body').one('mousemove.dropdownhover', function () { |
37 | | - // some touch devices are firing the mousemove event on the body, |
38 | | - // but only after the mouseenter event happens on the dropdown toggle |
39 | | - if (!_mouseEnterDetected) { |
| 34 | + $('body').one('touchstart.dropdownhover', function() { |
| 35 | + _touchstartDetected = true; |
| 36 | + }); |
| 37 | + |
| 38 | + $('body').one('mouseenter.dropdownhover', function() { |
| 39 | + // touchstart fires before mouseenter on touch devices |
| 40 | + if (!_touchstartDetected) { |
40 | 41 | _mouseDetected = true; |
41 | 42 | } |
42 | 43 | }); |
43 | 44 |
|
44 | 45 | $('.dropdown-toggle, .dropdown-menu', dropdown.element.parent()).on('mouseenter.dropdownhover', function () { |
45 | | - // we need that for proper mouse detection |
46 | | - _mouseEnterDetected = true; |
| 46 | + // seems to be a touch device |
| 47 | + if(_mouseDetected && !$(this).is(':hover')) { |
| 48 | + _mouseDetected = false; |
| 49 | + } |
47 | 50 |
|
48 | 51 | if (!_mouseDetected) { |
49 | 52 | return; |
|
104 | 107 | // seems that bootstrap binds the click handler twice after we reinitializing the plugin after a destroy... |
105 | 108 | $('.dropdown-toggle, .dropdown-menu', dropdown.element.parent()).off('.dropdown'); |
106 | 109 | dropdown.element.off('.dropdownhover'); |
| 110 | + $('body').off('.dropdownhover'); |
107 | 111 | } |
108 | 112 |
|
109 | 113 | BootstrapDropdownHover.prototype = { |
|
0 commit comments