欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

如何在Bootstrap v4中實現(xiàn)導(dǎo)航條下拉懸停?

黃文隆1年前8瀏覽0評論

我對新的引導(dǎo)版本有點(diǎn)困惑,因為他們把下拉菜單改成了div:

<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle"  id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

你們有沒有辦法在不添加額外腳本代碼的情況下(只有css和來自bootstrap的腳本)在該代碼片段的下拉鏈接中獲得一個懸停下拉列表?我已經(jīng)看到了bootstrap css類,但我無法與bootstrap V3中的類聯(lián)系起來(我在V3中沒有添加jquery就實現(xiàn)了這一點(diǎn))。

簡單的純CSS解決方案:

.dropdown:hover>.dropdown-menu {
  display: block;
}

當(dāng)點(diǎn)擊時,它仍然會切換到類顯示(當(dāng)不再懸停時,它將保持打開)。

要正確解決這個問題,可以使用為基于指針的設(shè)備保留的事件和屬性:jQuery的mouseenter、mouseleave和:hover。應(yīng)該能夠流暢、直觀地工作,同時完全不影響下拉菜單在基于觸摸的設(shè)備上的工作方式。嘗試一下,讓我知道它是否適合你:

完整的jQuery解決方案(未觸摸):

4.1.2版之前的解決方案(已棄用):

$('body').on('mouseenter mouseleave','.dropdown',function(e){
  var _d=$(e.target).closest('.dropdown');
  if (e.type === 'mouseenter')_d.addClass('show');
  setTimeout(function(){
    _d.toggleClass('show', _d.is(':hover'));
    $('[data-toggle="dropdown"]', _d).attr('aria-expanded',_d.is(':hover'));
  },300);
});

$('body').on('mouseenter mouseleave','.dropdown',function(e){
  var _d=$(e.target).closest('.dropdown');
  if (e.type === 'mouseenter')_d.addClass('show');
  setTimeout(function(){
    _d.toggleClass('show', _d.is(':hover'));
    $('[data-toggle="dropdown"]', _d).attr('aria-expanded',_d.is(':hover'));
  },300);
});

/* this is not needed, just prevents page reload when a dd link is clicked */
$('.dropdown a').on('click tap', e => e.preventDefault())

<link  rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>

<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href>Navbar</a>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href>Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href>Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href>Pricing</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href>Action</a>
          <a class="dropdown-item" href>Another action</a>
          <a class="dropdown-item" href>Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

只需將這個簡單的css代碼添加到樣式表中,就可以開始了。

.dropdown:hover > .dropdown-menu {
    display: block;
}
.dropdown > .dropdown-toggle:active {
    /*Without this, clicking will make it sticky*/
    pointer-events: none;
}

沒有一個CSS完全只回答工作。單擊后下拉菜單保持打開,或者在您到達(dá)要單擊的菜單鏈接之前有一個間隙使下拉菜單隱藏起來。

下面是簡單的僅使用CSS的解決方案:

.navbar-nav li:hover .dropdown-menu {
    display: block;
}

從HTML標(biāo)記中移除data-toggle=dropdown,以防止下拉列表在單擊時保持打開狀態(tài)。使用mt-0 (margin-top:0)來消除菜單上方的間隙,并使懸停菜單項成為可能。

演示https://www.codeply.com/go/awyU7VTIJf

完整代碼:

.navbar-nav li:hover .dropdown-menu {
        display: block;
    } 

   <nav class="navbar navbar-expand-lg navbar-light bg-light">
      ..
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown">
              Dropdown
            </a>
            <div class="dropdown-menu mt-0" aria-labelledby="navbarDropdown">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
              <div class="dropdown-divider"></div>
              <a class="dropdown-item" href="#">Something else here</a>
            </div>
          </li>
        </ul>
      </div>
    </nav>

安德烈的& quot完成& quotjQuery+CSS解決方案的意圖是正確的,但是它很冗長并且仍然不完整。不完整,因為雖然它可能涵蓋了所有必要的DOM更改,但它缺少自定義事件的觸發(fā)。冗長是因為Bootstrap已經(jīng)提供了dropdown()方法,該方法可以完成所有工作,這是一種重復(fù)勞動。

因此,正確、干巴巴的解決方案是jQuery,它不依賴于其他答案中經(jīng)常重復(fù)的CSS技巧:

$('body').on('mouseover mouseout', '.dropdown', function () {
  $(this).dropdown('toggle');
});

自v4發(fā)布以來,Bootstrap的功能似乎略有變化。的。下拉菜單項現(xiàn)在似乎也獲得了。除了. dropdown之外還顯示類。我修改了Andrei的回答,也在。下拉菜單。請注意,CSS不再是必要的,HTML是相同的,除了我更新了當(dāng)前版本的鏈接,nav類改為navbar-expand-md。

$('body').on('mouseenter mouseleave', '.dropdown', function (e) {
    var dropdown = $(e.target).closest('.dropdown');
    var menu = $('.dropdown-menu', dropdown);
    dropdown.addClass('show');
    menu.addClass('show');
    setTimeout(function () {
        dropdown[dropdown.is(':hover') ? 'addClass' : 'removeClass']('show');
        menu[dropdown.is(':hover') ? 'addClass' : 'removeClass']('show');
    }, 300);
});

<link rel="stylesheet"  integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle"  id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

當(dāng)我被要求將其改為懸停交互時,我已經(jīng)使用并設(shè)計了一個導(dǎo)航條,所以最終使用jQuery進(jìn)行了修復(fù)。

function bootstrapHoverMenu (bp = 768) {

  // close all dropdowns that are open
    $('body').click( function (e) {
    $('.dropdown-menu.show').removeClass('show');
  });

  // show dropdown for the link clicked
  $('.nav-item').hover(function (e) {
    $('.dropdown-menu.show').removeClass('show');
    if(( $(window).width() >= bp )) {
      $dd = $(this).find('.dropdown-menu');
      $dd.addClass('show');
    }
  });

  // get href for top level link if clicked and open
  $('.dropdown').click(function (e) {
    if( $(window).width() < bp ) {
      $('.dropdown-menu').css({'display': 'none'});
    }
    $href = $(this).find('.nav-link').attr('href');
    window.open($href, '_self');
  });
}

$(document).ready( function() {
   // when page ready run the fix
   bootstrapHoverMenu();
});

缺點(diǎn)是手機(jī)只有頂級鏈接。

這可以確保您仍然可以跟蹤頂級鏈接的點(diǎn)擊 兼容手機(jī)。

這是專為臺式機(jī)和移動設(shè)備設(shè)計的。自由地用一個檢查窗口寬度是否大于768px的條件來包裝jQuery。

jQuery

/** Dropdown on hover */
$(".nav-link.dropdown-toggle").hover( function () {
    // Open up the dropdown
    $(this).removeAttr('data-toggle'); // remove the data-toggle attribute so we can click and follow link
    $(this).parent().addClass('show'); // add the class show to the li parent
    $(this).next().addClass('show'); // add the class show to the dropdown div sibling
}, function () {
    // on mouseout check to see if hovering over the dropdown or the link still
    var isDropdownHovered = $(this).next().filter(":hover").length; // check the dropdown for hover - returns true of false
    var isThisHovered = $(this).filter(":hover").length;  // check the top level item for hover
    if(isDropdownHovered || isThisHovered) {
        // still hovering over the link or the dropdown
    } else {
        // no longer hovering over either - lets remove the 'show' classes
        $(this).attr('data-toggle', 'dropdown'); // put back the data-toggle attr
        $(this).parent().removeClass('show');
        $(this).next().removeClass('show');
    }
});
// Check the dropdown on hover
$(".dropdown-menu").hover( function () {
}, function() {
    var isDropdownHovered = $(this).prev().filter(":hover").length; // check the dropdown for hover - returns true of false
    var isThisHovered= $(this).filter(":hover").length;  // check the top level item for hover
    if(isDropdownHovered || isThisHovered) {
        // do nothing - hovering over the dropdown of the top level link
    } else {
        // get rid of the classes showing it
        $(this).parent().removeClass('show');
        $(this).removeClass('show');
    }
});

半鑄鋼?鋼性鑄鐵(Cast Semi-Steel)

@media(min-width:  768px) {
  .dropdown-menu {
    margin-top: 0; // fixes closing on slow mouse transition
  }
}

僅對于bootstrap 4,可懸停下拉菜單不會失去popper.js的功能

java描述語言

$('.dropdown-hoverable').hover(function(){
    $(this).children('[data-toggle="dropdown"]').click();
}, function(){
    $(this).children('[data-toggle="dropdown"]').click();
});

超文本標(biāo)記語言

<nav class="nav">
  <li class="nav-item dropdown dropdown-hoverable">
    <a class="nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" href="#">Menu link</a>
    <ul class="dropdown-menu">
    </ul>
  </li>
</nav>

我在這里找不到完整的解決方案。因此,這是我的一個與Bootstrap v4.4.1一起工作的產(chǎn)品,它具有以下優(yōu)點(diǎn):

點(diǎn)擊下拉開關(guān)就像一個普通的導(dǎo)航鏈接。

支持任何嵌套層次的下拉菜單。

bootstrap 4 { show/showed/hide/hidden } . bs . drop down事件工作良好。

// Toggles a B4 dropdown-menu to a given state.
 const toggleDropdownElement = ($dropdown, shouldOpen = false) => {
   const $dropdownToggle = $dropdown.children('[data-toggle="dropdown"], a');
   const $dropdownMenu = $dropdown.children('.dropdown-menu');

   // Change the dropdown menu. It's similar to B4 Dropdown.show()/.hide(), see /bootstrap/js/src/dropdown.js.
   if (shouldOpen) {
     $dropdown.trigger('show.bs.dropdown');
     $dropdownToggle.attr('aria-expanded', true).focus();
     $dropdownMenu.addClass('show');
     $dropdown.addClass('show').trigger($.Event('shown.bs.dropdown', $dropdownMenu[0]));
   } else {
     $dropdown.trigger('hide.bs.dropdown');
     $dropdownToggle.attr('aria-expanded', false);
     $dropdownMenu.removeClass('show');
     $dropdown.removeClass('show').trigger($.Event('hidden.bs.dropdown', $dropdownMenu[0]));
   }
 };

 // Toggles a B4 dropdown-menu with any nesting level.
 const toggleDropdown = (event) => {
   const $dropdown = $(event.target).closest('.dropdown');
   const $parentDropdownMenu = $dropdown.closest('.dropdown-menu');
   const shouldOpen = event.type !== 'click' && $dropdown.is(':hover');

   // If the dropdown was closed already, break the 'mouseleave' event cascade.
   if (!shouldOpen && !$dropdown.hasClass('show')) return;

   // Change the current dropdown menu (last nested).
   toggleDropdownElement($dropdown, shouldOpen);

   // We have to close the dropdown menu tree if it was a click or the menu was leave at all.
   if (event.type === 'click' || $parentDropdownMenu.length && !$parentDropdownMenu.is(':hover')) {
     $dropdown.parents('.dropdown').each((index, element) => {
       toggleDropdownElement($(element), false);
     });
   }
 };

 if (viewport && viewport.is('>=xl')) {
   $('body')
     .on('mouseenter mouseleave', '.dropdown', toggleDropdown)
     .on('click', '.dropdown-menu a', toggleDropdown);

   // Disable the default B4's click. Other words, change a dropdown-toggle to a normal nav link.
   $(document).off('click.bs.dropdown', '[data-toggle="dropdown"]');
   $(document).off('click.bs.dropdown.data-api', '[data-toggle="dropdown"]'); // Not sure about it.
 }

如果不使用ES6,只需將箭頭功能更改為舊的功能樣式。

謝謝陶的示范,對我很有幫助。

代碼相關(guān)鏈接:B4下拉事件,viewport(響應(yīng)引導(dǎo)工具包),WP引導(dǎo)Navwalker。

這個解決方案打開和關(guān)閉

<script>
$(document).ready(function() {
  // close all dropdowns that are open
  $('body').click(function(e) {
      $('.nav-item.show').removeClass('show');
      //$('.nav-item.clicked').removeClass('clicked');
      $('.dropdown-menu.show').removeClass('show');
  });

  $('.nav-item').click( function(e) {
    $(this).addClass('clicked')
  });

  // show dropdown for the link clicked
  $('.nav-item').hover(function(e) {
      if ($('.nav-item.show').length < 1) {
        $('.nav-item.clicked').removeClass('clicked');
      }
      if ($('.nav-item.clicked').length < 1) {
          $('.nav-item.show').removeClass('show');
          $('.dropdown-menu.show').removeClass('show');
          $dd = $(this).find('.dropdown-menu');
          $dd.parent().addClass('show');
          $dd.addClass('show');
      }
  });
});</script>

要禁用lg大小折疊菜單的懸停,請?zhí)砑?/p>

if(( $(window).width() >= 992 )) {

CSS解決方案在觸摸設(shè)備上無法正常工作

我發(fā)現(xiàn)任何CSS解決方案都能讓菜單在觸摸設(shè)備上保持打開,不再折疊。 所以我讀了這篇文章:https://www . brianshim . com/web tricks/drop-down-menus-on-IOs-and-Android/(Brian Shim) 非常有用!它聲明觸摸設(shè)備總是首先檢查元素上的懸停類的存在。 但是:通過使用jQuery。show()你引入一個樣式屬性(display:block;)這使得菜單在第一次觸摸時打開。現(xiàn)在菜單已經(jīng)打開,但沒有啟動“show”類。如果用戶從下拉菜單中選擇一個鏈接,它會完美地工作。但是如果用戶決定關(guān)閉菜單而不使用它,他必須輕擊兩次來關(guān)閉菜單:在第一次輕擊時,原始的bootstrap“show”類被附加,因此菜單再次打開,在第二次輕擊時,菜單由于正常的引導(dǎo)行為而關(guān)閉(移除“show”類)。

為了防止這種情況,我使用了文章:https://code burst . io/the-only-way-to-detect-touch-with-JavaScript-7791 a 3346685(作者David Gilbertson)

他有一些非常方便的方法來檢測觸摸或懸停設(shè)備。

所以,結(jié)合兩位作者和我自己的jQuery:

$(window).one('mouseover', function(){
      window.USER_CAN_HOVER = true;
      if(USER_CAN_HOVER){
          jQuery('#navbarNavDropdown ul li.dropdown').on("mouseover", function() {
             var $parent = jQuery(this);
             var $dropdown = $parent.children('ul');

             $dropdown.show(200,function() { 
               $parent.mouseleave(function() {
                 var $this = jQuery(this);
                 $this.children('ul').fadeOut(200);
               });
             });
          });
      };

}); 檢查一次設(shè)備是否允許懸停事件。如果是的話,引入懸停使用的可能性。顯示()。如果設(shè)備不允許懸停事件,則。show()從未被引入,所以你在觸摸設(shè)備上得到自然的引導(dǎo)行為。

確保刪除任何關(guān)于菜單懸停類的CSS。

花了我三天:)所以希望對你們有些人有幫助。

1.移除data-toggle="dropdown "屬性(因此單擊不會打開下拉菜單)

2.添加:懸停偽類以顯示下拉菜單

.dropdown:hover .dropdown-menu {display: block;}

<link  rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle"  id="navbarDropdownMenuLink" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
    </ul>
  </div>
</nav>

我正在使用bootstrap 5,上面的解決方案看起來很大,我只是刪除了data-bs-toggle='dropdown ',因為它阻止了導(dǎo)航的父鏈接和切換腳本以單擊打開下拉菜單。

并添加了CSS

.dropdown:hover > .dropdown-menu {
    display: block;
    margin-top: 0;
    // removes the gap so it doesn't close ;
}

對我來說很好

我認(rèn)為這只是簡單地與bootstrap 4一起工作,我內(nèi)聯(lián)添加,但你總是可以從腳本綁定事件。

<a 
onmouseover="$('#navbarDropdownMenuLink').dropdown('toggle')"
class="nav-link dropdown-toggle" 
 
id="navbarDropdownMenuLink" 
data-toggle="dropdown" 
aria-haspopup="true" 
aria-expanded="false">
      Dropdown link
    </a>

我使用bootstrap 4.0.0 因為我們想模擬。顯示懸停事件,這很簡單。只需在. dropdown.show中添加所有樣式。下拉菜單到:懸停。像這樣:

.dropdown:hover>.dropdown-menu {
  opacity: 1;
  visibility: visible;
  transform: translate3d(0px, 0px, 0px);
}

$('body').on('mouseenter mouseleave','.dropdown',function(e){
  var _d=$(e.target).closest('.dropdown');
  if (e.type === 'mouseenter')_d.addClass('show');
  setTimeout(function(){
    _d.toggleClass('show', _d.is(':hover'));
    $('[data-toggle="dropdown"]', _d).attr('aria-expanded',_d.is(':hover'));
  },300);
});

/* this is not needed, just prevents page reload when a dd link is clicked */
$('.dropdown a').on('click tap', e => e.preventDefault())

<link  rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>

<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href>Navbar</a>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href>Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href>Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href>Pricing</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href>Action</a>
          <a class="dropdown-item" href>Another action</a>
          <a class="dropdown-item" href>Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

CSS和僅桌面解決方案

@media (min-width: 992px) { 
.dropdown:hover>.dropdown-menu {
  display: block;
}
}

這兩個最佳解決方案都不適合我。

這很好地工作,在瀏覽時保持子菜單打開,add使用本地引導(dǎo)javascript。

// Mouse over
$('body').on('mouseover', '.dropdown', function(e) { 
    $(this).children('.dropdown-toggle').dropdown('show');
});

// Mouse leave
$('body').on('mouseleave', '.dropdown', function(e) { 
    $(this).children('.dropdown-toggle').dropdown('hide');
});

谷歌把我?guī)У竭@里,但是...如果下拉菜單在顯示時與其父菜單重疊(至少重疊1px ),提供的示例也可以工作。否則,它會失去焦點(diǎn),一切都無法按預(yù)期進(jìn)行。

下面是jQuery和Bootstrap 4.5.2的一個工作解決方案:

$('li.nav-item').mouseenter(function (e) {

        e.stopImmediatePropagation();

        if ($(this).hasClass('dropdown')) {

            // target element containing dropdowns, show it
            $(this).addClass('show');
            $(this).find('.dropdown-menu').addClass('show');

            // Close dropdown on mouseleave
            $('.dropdown-menu').mouseleave(function (e) {
                e.stopImmediatePropagation();
                $(this).removeClass('show');
            });

            // If you have a prenav above, this clears open dropdowns (since you probably will hover the nav-item going up and it will reopen its dropdown otherwise)
            $('#prenav').off().mouseenter(function (e) {
                e.stopImmediatePropagation();
                $('.dropdown-menu').removeClass('show');
            });

        } else {
            // unset open dropdowns if hover is on simple nav element
            $('.dropdown-menu').removeClass('show');
        }
    });

就用陶的CSS代碼:

.dropdown:hover > .dropdown-menu {
   display: block;
}

并消除2px之間的差距。下拉開關(guān)和。將下拉菜單的上邊距設(shè)置為零:

.dropdown-menu { margin-top: 0!important }

(2020年6月)我發(fā)現(xiàn)了這個解決方案,我想我應(yīng)該把它貼在這里:

引導(dǎo)版本:4.3.1

CSS部分:

.navbar .nav-item:not(:last-child) {
  margin-right: 35px;
}

.dropdown-toggle::after {
   transition: transform 0.15s linear;
}

.show.dropdown .dropdown-toggle::after {
  transform: translateY(3px);
}

.dropdown-menu {
  margin-top: 0;
}

jQuery部分:

const $dropdown = $(".dropdown");
const $dropdownToggle = $(".dropdown-toggle");
const $dropdownMenu = $(".dropdown-menu");
const showClass = "show";

$(window).on("load resize", function() {
  if (this.matchMedia("(min-width: 768px)").matches) {
    $dropdown.hover(
      function() {
        const $this = $(this);
        $this.addClass(showClass);
        $this.find($dropdownToggle).attr("aria-expanded", "true");
        $this.children($dropdownMenu).addClass(showClass);
      },
      function() {
        const $this = $(this);
        $this.removeClass(showClass);
        $this.find($dropdownToggle).attr("aria-expanded", "false");
        $this.children($dropdownMenu).removeClass(showClass);
      }
    );
  } else {
    $dropdown.off("mouseenter mouseleave");
  }
});

來源:https://web design . tuts plus . com/tutorials/how-to-make-the-bootstrap-navbar-drop down-work-on-hover-CMS-33840

& ltdiv style = " width:100%;溢出:滾動;"& gt& lttable class = " table table-striped table-bordered " style = " font-size:12px " >