简体   繁体   English

引导导航栏下拉菜单的纯 javascript 方式

[英]pure javascript way to bootstrap navbar dropdown

So I am trying to make the dropdown in the below code work with javascript as I noticed that the CSS code will only trigger the dropdown after a user first clicks on the dropdown.所以我试图让下面代码中的下拉菜单与 javascript 一起工作,因为我注意到 CSS 代码只会在用户第一次点击下拉菜单后触发下拉菜单。 after that, it will let the css hover code work as per normal.之后,它将让 css 悬停代码正常工作。

So needing a Javascript way to get this drop down to work.所以需要一种 Javascript 方法来让这个下拉菜单工作。

import React, { useState  } from "react";
import {Nav, Navbar, NavDropdown, ButtonToolbar, Button } from "react-bootstrap";
import { withRouter } from "react-router";

import '../App.css';

const Header = props => {
    const { location } = props;

    function changeBackground(e) {
        e.target.children('[data-toggle="dropdown"]').click();
    }

    return (
        <Navbar bg="transparent" variant="dark" expand="lg">
        <Navbar.Brand href="#home" className="App-logo">AdStichr</Navbar.Brand>
        <Navbar.Toggle aria-controls="basic-navbar-nav" />
        <Navbar.Collapse id="basic-navbar-nav">
            <Nav className="ml-auto" activeKey={location.pathname}>
            <Nav.Link href="/">Home</Nav.Link>
            <Nav.Link href="/advertisers">Advertisers</Nav.Link>
            <NavDropdown title="Publishers" id="basic-nav-dropdown" alignRight 
            onMouseOver={changeBackground}>

                <NavDropdown.Item href="/publishers/radio">Radio Stations</NavDropdown.Item>
                <NavDropdown.Divider />
                <NavDropdown.Item href="/publishers/podcasters">Audio Podcasters</NavDropdown.Item>
                <NavDropdown.Divider />
                <NavDropdown.Item href="/publishers/videopodcasters">Video Podcasters</NavDropdown.Item>

            </NavDropdown>
            <Nav.Link href="/case-studies">Case Studies</Nav.Link>
            <ButtonToolbar>
                <Button href="/contact" variant="success" size="lg" className="button-round">
                    Contact Us
                </Button>
            </ButtonToolbar>
            </Nav>

        </Navbar.Collapse>
        </Navbar>
    );
  };
  const HeaderWithRouter = withRouter(Header);
  export default HeaderWithRouter;

We need to change our changeBackground function like this, to auto click the dropdown button on onMouseOver event:我们需要像这样更改我们的changeBackground函数,以自动单击onMouseOver事件上的下拉按钮:

changeBackground = (e) => {
  e.target.click();
};

This will click the dropdown button and open the dropdown menu.这将单击下拉按钮并打开下拉菜单。 Please let me know if you need further information.如果您需要更多信息,请告诉我。

Opening the dropdown menu by hovering is not supported in React Bootstrap (here is an explanation: https://github.com/react-bootstrap/react-bootstrap/issues/4042 ). React Bootstrap 不支持通过悬停来打开下拉菜单(这里有一个解释: https : //github.com/react-bootstrap/react-bootstrap/issues/4042 )。

If you'd like to achieve that you have to use the classic dropdown menu (implemented using NavItem so it works the same as the usual NavDropdown).如果您想实现这一点,您必须使用经典的下拉菜单(使用 NavItem 实现,因此它的工作方式与通常的 NavDropdown 相同)。 You should be able to use the show property of Dropdown.Menu - but that doesn't seem to work perfectly either so I had to improvise a bit (hiding/removing the dropdown.menu based on state).您应该能够使用 Dropdown.Menu 的 show 属性 - 但这似乎也不能完美工作,所以我不得不即兴发挥(根据状态隐藏/删除 dropdown.menu)。

Here is a working example using Dropdown.Menu instead of NavDropdown (but with the same properties): https://codesandbox.io/s/falling-cookies-10joi这是一个使用 Dropdown.Menu 而不是 NavDropdown 的工作示例(但具有相同的属性): https ://codesandbox.io/s/falling-cookies-10joi

The main code difference is, as I explained above, using the Dropdown component instead of NavDropdown to be able to use the show property:主要的代码区别是,正如我上面所解释的,使用 Dropdown 组件而不是 NavDropdown 来能够使用 show 属性:

<Dropdown as={NavItem} alignRight onMouseLeave={closeMenu}>
  <Dropdown.Toggle
    id="basic-nav-dropdown"
    as={NavLink}
    onMouseEnter={openMenu}
  >
    Publishers
  </Dropdown.Toggle>

  {menuOpen && (
    <Dropdown.Menu show={true}>
      <NavDropdown.Item href="/publishers/radio">
        Radio Stations
      </NavDropdown.Item>
      <NavDropdown.Divider />
      <NavDropdown.Item href="/publishers/podcasters">
        Audio Podcasters
      </NavDropdown.Item>
      <NavDropdown.Divider />
      <NavDropdown.Item href="/publishers/videopodcasters">
        Video Podcasters
      </NavDropdown.Item>
    </Dropdown.Menu>
  )}
</Dropdown>

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

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