No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dropdown-hover.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Add onHover event for dropdowns
  2. ;(function ($) {
  3. if (!$ || !$.fn) return
  4. const SELECTOR = '[data-bs-toggle=dropdown][data-trigger=hover]'
  5. const TIMEOUT = 150
  6. function openDropdown($i) {
  7. let t = $i.data('dd-timeout')
  8. if (t) {
  9. clearTimeout(t)
  10. t = null
  11. $i.data('dd-timeout', t)
  12. }
  13. if ($i.attr('aria-expanded') !== 'true') $i.dropdown('toggle')
  14. }
  15. function closeDropdown($i) {
  16. let t = $i.data('dd-timeout')
  17. if (t) clearTimeout(t)
  18. t = setTimeout(() => {
  19. let t2 = $i.data('dd-timeout')
  20. if (t2) {
  21. clearTimeout(t2)
  22. t2 = null
  23. $i.data('dd-timeout', t2)
  24. }
  25. if ($i.attr('aria-expanded') === 'true') $i.dropdown('toggle')
  26. }, TIMEOUT)
  27. $i.data('dd-timeout', t)
  28. }
  29. $(function () {
  30. $('body')
  31. .on('mouseenter', `${SELECTOR}, ${SELECTOR} ~ .dropdown-menu`, function () {
  32. const $toggle = $(this).hasClass('dropdown-toggle') ? $(this) : $(this).prev('.dropdown-toggle')
  33. const $dropdown = $(this).hasClass('dropdown-menu') ? $(this) : $(this).next('.dropdown-menu')
  34. if (window.getComputedStyle($dropdown[0], null).getPropertyValue('position') === 'static') return
  35. // Set hovered flag
  36. if ($(this).is(SELECTOR)) {
  37. $(this).data('hovered', true)
  38. }
  39. openDropdown($(this).hasClass('dropdown-toggle') ? $(this) : $(this).prev('.dropdown-toggle'))
  40. })
  41. .on('mouseleave', `${SELECTOR}, ${SELECTOR} ~ .dropdown-menu`, function () {
  42. const $toggle = $(this).hasClass('dropdown-toggle') ? $(this) : $(this).prev('.dropdown-toggle')
  43. const $dropdown = $(this).hasClass('dropdown-menu') ? $(this) : $(this).next('.dropdown-menu')
  44. if (window.getComputedStyle($dropdown[0], null).getPropertyValue('position') === 'static') return
  45. // Remove hovered flag
  46. if ($(this).is(SELECTOR)) {
  47. $(this).data('hovered', false)
  48. }
  49. closeDropdown($(this).hasClass('dropdown-toggle') ? $(this) : $(this).prev('.dropdown-toggle'))
  50. })
  51. .on('hide.bs.dropdown', function (e) {
  52. if ($(this).find(SELECTOR).data('hovered')) e.preventDefault()
  53. })
  54. })
  55. })(window.jQuery)