// outside the scope of the jQuery plugin to// keep track of all dropdownsvar $allDropdowns = $();
// if instantlyCloseOthers is true, then it will instantly// shut other nav items when a new one is hovered over$.fn.dropdownHover = function(options) {
// the element we really care about// is the dropdown-toggle's parent$allDropdowns = $allDropdowns.add(this.parent());
return this.each(function() {var $this = $(this).parent(),defaults = {delay: 500,instantlyCloseOthers: true},data = {delay: $(this).data('delay'),instantlyCloseOthers: $(this).data('close-others')},options = $.extend(true, {}, defaults, options, data),timeout;
$this.hover(function() {if(options.instantlyCloseOthers === true)$allDropdowns.removeClass('open');
window.clearTimeout(timeout);$(this).addClass('open');}, function() {timeout = window.setTimeout(function() {$this.removeClass('open');}, options.delay);});});};
;(function($, window, undefined) {// Outside the scope of the jQuery plugin to// keep track of all dropdownsvar $allDropdowns = $();
// If instantlyCloseOthers is true, then it will instantly// shut other nav items when a new one is hovered over$.fn.dropdownHover = function(options) {
// The element we really care about// is the dropdown-toggle's parent$allDropdowns = $allDropdowns.add(this.parent());
return this.each(function() {var $this = $(this),$parent = $this.parent(),defaults = {delay: 500,instantlyCloseOthers: true},data = {delay: $(this).data('delay'),instantlyCloseOthers: $(this).data('close-others')},settings = $.extend(true, {}, defaults, options, data),timeout;
$parent.hover(function(event) {
// So a neighbor can't open the dropdownif(!$parent.hasClass('open') && !$this.is(event.target)) {return true;}
if(settings.instantlyCloseOthers === true)$allDropdowns.removeClass('open');
window.clearTimeout(timeout);$parent.addClass('open');}, function() {timeout = window.setTimeout(function() {$parent.removeClass('open');}, settings.delay);});
// This helps with button groups!$this.hover(function() {if(settings.instantlyCloseOthers === true)$allDropdowns.removeClass('open');
window.clearTimeout(timeout);$parent.addClass('open');});
// Handle submenus$parent.find('.dropdown-submenu').each(function(){var $this = $(this);var subTimeout;$this.hover(function() {window.clearTimeout(subTimeout);$this.children('.dropdown-menu').show();
// Always close submenu siblings instantly$this.siblings().children('.dropdown-menu').hide();}, function() {var $submenu = $this.children('.dropdown-menu');subTimeout = window.setTimeout(function() {$submenu.hide();}, settings.delay);});});});};
$(document).ready(function() {// apply dropdownHover to all elements with the data-hover="dropdown" attribute$('[data-hover="dropdown"]').dropdownHover();});})(jQuery, this);
var isTouchDevice = (('ontouchstart' in window) ||(navigator.MaxTouchPoints > 0) ||(navigator.msMaxTouchPoints > 0));if(!isTouchDevice){// open dropdowns on hover on non mobile devices$(".dropdown").hover(function(e) {$('.dropdown-toggle', this).dropdown("toggle");e.stopPropagation();});// prevent blinkling$(".submenu-link").click(function(e) {e.stopPropagation();});}
export class BootstrapOpenMenuHover {
/*** Constructor.*/constructor() {this.windowWidth = window.innerWidth;this.mobileBreakPoint = 991; // Put your menu break point here, when it switches to a hamburger icon.this.dropdownNavItems = document.querySelectorAll(".dropdown-toggle.nav-link");this.dropdownMenuItems = document.querySelectorAll(".dropdown-menu");
this.setEventListeners();}
/*** Set all of our event listeners.*/setEventListeners() {const _self = this;
// To be safe set the width once the dom is loaded.window.addEventListener('load', function () {_self.windowWidth = window.innerWidth;});
// Keep track of the width in the event of a resize.window.addEventListener('resize', function () {_self.windowWidth = window.innerWidth;});
// Bind our hover events.if (_self.dropdownNavItems !== null) {for (let i = 0; i < _self.dropdownNavItems.length; i++) {
// On mouse enter._self.dropdownNavItems[i].addEventListener('mouseenter', function () {if (_self.windowWidth >= _self.mobileBreakPoint) {this.classList.add('show');this.ariaExpanded = true;this.dataset.bsToggle = null;
// Update the .dropdown-menuthis.nextElementSibling.classList.add('show');this.nextElementSibling.dataset.bsPopper = 'none';}});
// On mouse leave._self.dropdownNavItems[i].addEventListener('mouseleave', function () {if (_self.windowWidth >= _self.mobileBreakPoint) {this.classList.remove('show');this.ariaExpanded = false;this.dataset.bsToggle = 'dropdown';
// Update the .dropdown-menuthis.nextElementSibling.classList.remove('show');this.nextElementSibling.dataset.bsPopper = null;}});}}
// Bind events to .dropdown-menu items.if (_self.dropdownMenuItems !== null) {for (let i = 0; i < _self.dropdownMenuItems.length; i++) {// On mouse enter._self.dropdownMenuItems[i].addEventListener('mouseenter', function () {if (_self.windowWidth >= _self.mobileBreakPoint) {this.classList.add('show');this.dataset.bsPopper = 'none';
// Update the .dropdown-togglethis.previousElementSibling.classList.add('show');this.previousElementSibling.ariaExpanded = true;this.previousElementSibling.dataset.bsToggle = null;}});
// On mouse leave._self.dropdownMenuItems[i].addEventListener('mouseleave', function () {if (_self.windowWidth >= _self.mobileBreakPoint) {this.classList.remove('show');this.dataset.bsPopper = null;
// Update the .dropdown-togglethis.previousElementSibling.classList.remove('show');this.previousElementSibling.ariaExpanded = false;this.previousElementSibling.dataset.bsToggle = 'dropdown';}});}}}}
const bootstrapOpenMenuHover = new BootstrapOpenMenuHover();
Bootstrap v4解决方案
这将允许您跟踪顶级导航链接。
这是在考虑桌面和移动设备的情况下构建的。可以随意更改BREAK_POINT变量以满足您的需求:D。
jQuery
var WINDOW_WIDTH;var BREAK_POINT = 991;
(function ($) {
/** Set window width onload */WINDOW_WIDTH = $(window).width(); // Returns width of browser viewport/** Set window width if the browser is resized */$(window).resize(function () {WINDOW_WIDTH = $(window).width(); // Returns width of browser viewport});
/** Dropdown menu on mouseenter */$(".nav-item.dropdown").on('mouseenter', function () {console.log("mouseenter");if (WINDOW_WIDTH >= BREAK_POINT) {// Open up the dropdown$(this).addClass('show'); // add the class show to the li parent$(this).children('.nav-link').removeAttr('data-toggle'); // remove the data-toggle attribute so we can click and follow link$(this).children('.dropdown-menu').addClass('show'); // add the class show to the dropdown div sibling}});/** Dropdown menu on mouseleave */$(".nav-item.dropdown").on('mouseleave', function () {console.log("mouseleave");if (WINDOW_WIDTH >= BREAK_POINT) {// Close the dropdown$(this).removeClass('show'); // add the class show to the li parent$(this).children('.nav-link').attr('data-toggle', 'dropdown'); // remove the data-toggle attribute so we can click and follow link$(this).children('.dropdown-menu').removeClass('show'); // add the class show to the dropdown div sibling}});});