index.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. $(document).ready(function () {
  2. $('.header').on('mouseenter mouseleave', '.navbar', function (e) {
  3. const $brandImg = $(this).find('.custom-navbar-brand-img');
  4. const $facebookSvg = $(this).find('.facebook-svg');
  5. if (window.matchMedia("(min-width: 1200px)").matches) {
  6. if (e.type === 'mouseenter') {
  7. $brandImg.attr('src', '/static/image/index/logo-2.png'); // 替换为悬停时的图片路径
  8. $facebookSvg.attr('src', '/static/svg/facebook-2.svg'); // 替换为悬停时的图片路径
  9. } else if (e.type === 'mouseleave') {
  10. $brandImg.attr('src', '/static/image/index/logo.png'); // 回复为默认图片路径
  11. $facebookSvg.attr('src', '/static/svg/facebook-1.svg'); // 替换为悬停时的图片路径
  12. }
  13. }
  14. });
  15. $('.dropdown-submenu .dropdown-item').on('click', function (e) {
  16. e.stopPropagation();
  17. const $submenu = $(this).next('.dropdown-3rd-level-container'); // 获取对应的三级菜单容器
  18. // 切换显示/隐藏状态,并更新aria-expanded属性
  19. const isExpanded = this.getAttribute('aria-expanded') === 'true';
  20. console.log(isExpanded)
  21. this.setAttribute('aria-expanded', !isExpanded);
  22. if (isExpanded) {
  23. $submenu.hide();
  24. } else {
  25. $submenu.show();
  26. }
  27. const otherSubmenus = $('.dropdown-submenu > a[aria-expanded="true"]').not(this);
  28. otherSubmenus.each(function () {
  29. const $otherSubmenu = $(this).next('.dropdown-3rd-level-container');
  30. this.setAttribute('aria-expanded', 'false');
  31. $otherSubmenu.hide();
  32. });
  33. });
  34. $('.dropdown').on('hidden.bs.dropdown', function() {
  35. $('.dropdown-3rd-level-container').hide();
  36. const otherSubmenus = $('.dropdown-submenu > a[aria-expanded="true"]');
  37. otherSubmenus.each(function () {
  38. const $otherSubmenu = $(this).next('.dropdown-3rd-level-container');
  39. this.setAttribute('aria-expanded', 'false');
  40. });
  41. });
  42. });
  43. $(function () {
  44. dropdownOpen();
  45. });
  46. function dropdownOpen() {
  47. if (window.matchMedia("(min-width: 1200px)").matches) {
  48. $('.dropdown').mouseover(function () {
  49. $(this).addClass('show');
  50. //查找当前元素子节点中的对象,修改其class,这样
  51. //这样当有多个下拉菜单时,可以分别独立处理,而不会出现都下拉的bug
  52. $(this).find(".dropdown-menu").addClass('show');
  53. }).mouseout(function () {
  54. $(this).find(".dropdown-menu").removeClass('show');
  55. $(this).removeClass('show');
  56. });
  57. $('.dropdown-submenu').on({
  58. mouseenter: function () {
  59. const $submenu = $(this).children('.dropdown-menu');
  60. const $container = $(this).find('.dropdown-3rd-level-container');
  61. $submenu.addClass('show');
  62. $container.show();
  63. },
  64. mouseleave: function () {
  65. const $submenu = $(this).children('.dropdown-menu');
  66. const $container = $(this).find('.dropdown-3rd-level-container');
  67. $submenu.removeClass('show');
  68. $container.hide();
  69. }
  70. });
  71. }
  72. }