简体   繁体   English

如何在悬停而不是单击时制作网站菜单下拉菜单

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

Looking for some help.寻求帮助。 I'm helping to upgrade a website from Drupal 7 to 8. One of the problems I cannot resolve is how to make the drop-down menus display when you hover over them.我正在帮助将网站从 Drupal 7 升级到 8。我无法解决的问题之一是如何在您将鼠标悬停在下拉菜单上时显示它们。

You can see how it works on the live site here: https://eerr.org.uk/您可以在此处的实时站点上查看它的工作原理: https : //eerr.org.uk/

And how it does not work here: https://www.drupal8.eerr.org.uk/以及它如何在这里不起作用: https : //www.drupal8.eerr.org.uk/

I wonder if someone can help me out, I am not good with CSS and HTML so any advice welcome.我想知道是否有人可以帮助我,我不擅长 CSS 和 HTML,所以欢迎提出任何建议。

Thanks Mark谢谢马克

EDIT编辑

I am really hoping someone can help me edit the existing code not just send me a link to creating dropdown menus in stand-alone code.我真的希望有人可以帮助我编辑现有代码,而不仅仅是向我发送一个链接,以在独立代码中创建下拉菜单。

The existing code seems to be generated in the following .twig file (Again I have no experience in developing in this)现有代码似乎是在以下 .twig 文件中生成的(我再次没有开发这方面的经验)

themes/contrib/bootstrap_barrio/templates/navigation/menu--main.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 %}

''' '''

Learn how to create a hoverable dropdown menu with CSS.了解如何使用 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>

This is the minimum code neeeded for it to work.这是它工作所需的最少代码。 You can see more styling on the source.您可以在源上看到更多样式。

Keep in mind that hover is not mobile friendly.请记住, hover不适合移动设备。

This is just an advice ;这只是一个建议; there's no hover event on mobiles and touch pads.手机和触摸板上没有hover事件。 So if your website might be used on these devices, you should either keep click or implement the touch event.因此,如果您的网站可能在这些设备上使用,您应该保持click或实现touch事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM