簡體   English   中英

如何在懸停而不是單擊時制作網站菜單下拉菜單

[英]How to make website menu dropdown on hover rather than click

尋求幫助。 我正在幫助將網站從 Drupal 7 升級到 8。我無法解決的問題之一是如何在您將鼠標懸停在下拉菜單上時顯示它們。

您可以在此處的實時站點上查看它的工作原理: https : //eerr.org.uk/

以及它如何在這里不起作用: https : //www.drupal8.eerr.org.uk/

我想知道是否有人可以幫助我,我不擅長 CSS 和 HTML,所以歡迎提出任何建議。

謝謝馬克

編輯

我真的希望有人可以幫助我編輯現有代碼,而不僅僅是向我發送一個鏈接,以在獨立代碼中創建下拉菜單。

現有代碼似乎是在以下 .twig 文件中生成的(我再次沒有開發這方面的經驗)

主題/contrib/bootstrap_barrio/templates/navigation/menu--main.html.twig

'''

{#
/**
 * @file
 * Bootstrap Barrio's override to display a menu.
 *
 * Available variables:
 * - menu_name: The machine name of the menu.
 * - items: A nested list of menu items. Each menu item contains:
 *   - attributes: HTML attributes for the menu item.
 *   - below: The menu item child items.
 *   - title: The menu link title.
 *   - url: The menu link url, instance of \Drupal\Core\Url
 *   - localized_options: Menu link localized options.
 *   - is_expanded: TRUE if the link has visible children within the current
 *     menu tree.
 *   - is_collapsed: TRUE if the link has children within the current menu tree
 *     that are not currently visible.
 *   - in_active_trail: TRUE if the link is in the active trail.
 */
#}
{% import _self as menus %}

{#
  We call a macro which calls itself to render the full tree.
  @see http://twig.sensiolabs.org/doc/tags/macro.html
#}
{{ menus.menu_links(items, attributes, 0) }}

{% macro menu_links(items, attributes, menu_level) %}
  {% import _self as menus %}
  {% if items %}
    {% if menu_level == 0 %}
      <ul{{ attributes.addClass('nav navbar-nav') }}>
    {% else %}
      <ul class="dropdown-menu">
    {% endif %}
    {% for item in items %}
      {%
        set classes = [
          menu_level ? 'dropdown-item' : 'nav-item',
          item.is_expanded ? 'menu-item--expanded',
          item.is_collapsed ? 'menu-item--collapsed',
          item.in_active_trail ? 'active',
          item.below ? 'dropdown',
        ]
      %}
      <li{{ item.attributes.addClass(classes) }}>
        {%
          set link_classes = [
            not menu_level ? 'nav-link',
            item.in_active_trail ? 'active',
            item.below ? 'dropdown-toggle',
            item.url.getOption('attributes').class ? item.url.getOption('attributes').class | join(' '),
            'nav-link-' ~ item.url.toString() | clean_class,
          ]
        %}
        {% if item.below %}
          {{ link(item.title, item.url, {'class': link_classes, 'data-toggle': 'dropdown', 'aria-expanded': 'false', 'aria-haspopup': 'true' }) }}
          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
        {% else %}
          {{ link(item.title, item.url, {'class': link_classes}) }}
        {% endif %}
      </li>
    {% endfor %}
    </ul>
  {% endif %}
{% endmacro %}

'''

了解如何使用 CSS 創建可懸停的下拉菜單。

 .dropdown-content { display: none; } .dropdown-content a { display: block; } .dropdown:hover .dropdown-content { display: block; }
 <div class="dropdown"> <button class="dropbtn">Dropdown</button> <div class="dropdown-content"> <a href="#">This</a> <a href="#">Shows </a> <a href="#">On</a> <a href="#">HOVER</a> </div> </div>

這是它工作所需的最少代碼。 您可以在源上看到更多樣式。

請記住, hover不適合移動設備。

這只是一個建議; 手機和觸摸板上沒有hover事件。 因此,如果您的網站可能在這些設備上使用,您應該保持click或實現touch事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM