Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何将此下拉菜单组件在react中导航栏的特定位置下方对齐?_Html_Css_Reactjs_Navbar - Fatal编程技术网

Html 如何将此下拉菜单组件在react中导航栏的特定位置下方对齐?

Html 如何将此下拉菜单组件在react中导航栏的特定位置下方对齐?,html,css,reactjs,navbar,Html,Css,Reactjs,Navbar,我在React中编写简单的导航栏和下拉菜单时遇到问题。我在nav中为一个帐户创建了一个分区,当鼠标悬停在该分区上时,将在其下方生成一个下拉菜单。 我尝试直接从w3站点复制代码,但遇到了更多问题,决定采用自己的方法 我创建了一个下拉功能组件,由Navbar组件呈现。下拉组件的可见/隐藏状态在名为hideDropdown的useState变量中跟踪 到目前为止,一切都正常运行。我的问题出现在尝试在导航栏中的account部分下面对齐此下拉组件时。无论我尝试什么,我都无法将它放在屏幕上的正确位置。即使

我在React中编写简单的导航栏和下拉菜单时遇到问题。我在nav中为一个帐户创建了一个分区,当鼠标悬停在该分区上时,将在其下方生成一个下拉菜单。 我尝试直接从w3站点复制代码,但遇到了更多问题,决定采用自己的方法

我创建了一个下拉功能组件,由Navbar组件呈现。下拉组件的可见/隐藏状态在名为hideDropdown的useState变量中跟踪

到目前为止,一切都正常运行。我的问题出现在尝试在导航栏中的account部分下面对齐此下拉组件时。无论我尝试什么,我都无法将它放在屏幕上的正确位置。即使我硬编码组件应该从左侧开始的像素数量,调整屏幕大小也会破坏这一点。默认情况下,下拉组件在左侧生成。此外,只有left属性对其位置有任何影响。权利是看不见的。此外,百分比似乎并不基于页面的宽度,因为为了将下拉列表一直对齐到右侧,left属性必须为800%

Navbar.js

import React, { useState, useContext, useEffect } from "react";
import { Redirect } from "react-router-dom";
import jsonwebtoken from "jsonwebtoken";
import Dropdown from "./Modals/Dropdown";
import "./CSS/navbar.css";
import NewPostModal from "./Modals/NewPostModal";
import { CategoryContext } from "./Context/CategoryContext";

function Navbar(props) {
  const { categories, setCategories, user } = useContext(CategoryContext);
  const [modalOpen, updateModal] = useState(false);
  const [hideDropdown, setHideDropdown] = useState(false); //change to true when done
  const [isLoading, setIsLoading] = useState(true);
  const jwtDecoded = jsonwebtoken.decode(localStorage.getItem("jwt"));

  console.log(
    "is token valid? " +
      (jwtDecoded.exp > (Date.now() - (Date.now() % 1000)) / 1000)
  );
  console.log(jwtDecoded);

  if (jwtDecoded.exp > (Date.now() - (Date.now() % 1000)) / 1000) {
    return (
      <div style={{ zIndex: "1", position: "fixed", display: "flex" }}>
        <ul className="navinator">
          <li className="navElement">
            <a className="anchor" href="/home">
              Home
            </a>
          </li>
          <li className="navElement">
            <a className="anchor" href="/profile">
              Profile
            </a>
          </li>
          <li className="active">
            <a
              className="anchor"
              // href="#"
              onClick={e => updateModal(true)}
            >
              + New Post
            </a>
          </li>
          <li style={{ float: "right" }}>
            <div
              id="profile"
              onMouseOver={e => setHideDropdown(false)}
              onMouseLeave={e => setHideDropdown(true)}
            >
              <div style={{ float: "left", paddingRight: "10px" }}>
                <img
                  src={user.profileImage}
                  alt="error"
                  className="profile-image"
                />
                {/* need profileImage field for user */}
              </div>
              <div style={{ float: "left", paddingRight: "5px" }}>
                {jwtDecoded.sub}
              </div>
              <div style={{ float: "left" }} className="arrow-down"></div>
            </div>
          </li>
        </ul>
        <NewPostModal show={modalOpen} updateModal={updateModal} />
        <div style={{float:"right"}}><Dropdown visible={hideDropdown} setHideDropdown={setHideDropdown}/></div>
      </div>
    );
  } else {
    console.log("redirecting");
    return <Redirect to={{ pathname: "/login", state: { from: "/home" } }} />;
  }
} //class

export default Navbar;

我对网络开发还很陌生,而且玩得很开心。然而,这一点是相当令人沮丧的,因为我觉得它应该是一个非常简单的解决方案。任何帮助都将不胜感激

我想这就是你想要的

我改变了一些CSS和JS

CSS

JS

const导航栏=()=>(
  • 无论什么
) ReactDOM.render(,document.getElementById('root'))
我解决的是,当
悬停
在导航中的任何对象上时打开
下拉列表
,并将其位置设置在其悬停的父对象下方


欢迎来到这里,是否可以在stack blitz或codepeni中创建一个最小的工作示例?我已经尝试了JSFIDLE、codepen和JsBin,但都没有成功。今晚似乎不是我的节目之夜。。。下面是链接:这个链接有效吗?我指的是任何输出,因为我没有看到你的代码的任何输出:(我花了一分钟,但我找到了一种方法使它呈现出来工作链接。我省略了用于打开和关闭下拉列表的useState变量;它无论如何都不应该相关,因为我认为这更像是一个面向布局的问题。
import React from "react";
import "../CSS/navbar.css";

export default function Dropdown(props) {
  return (
    <div
      hidden={props.visible}
      className="dropdown"
      onMouseOver={e => props.setHideDropdown(false)}
      onMouseLeave={e => props.setHideDropdown(true)}
    >
      <li>
        <a className="anchor" style={{ textAlign: "left" }} href="/profile">
          Profile
        </a>
      </li>
    </div>
  );
}

.navinator {
  z-index: 1;
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: rgb(218, 10, 10);
  position: fixed;
  top: 0;
  width: 100%;
  height: 52px;
}
.navElement {
  float: left;
}
.anchor{
  display: block;
  color: white !important;
  text-align: center;
  padding: 14px 16px; 
  text-decoration: none !important;
  cursor: pointer;
}
.anchor:hover {
  background-color: rgb(255, 23, 15) !important;
}
.active {
  background-color: rgb(28, 13, 110) !important;
  float: right;
  width: 120px;
}
.active .anchor{
  align-items: center;
  justify-content: center;
}
.active .anchor:hover {
  background-color: rgb(32, 11, 156) !important;
}
.profile-image {
  width: 30px;
  height: 30px;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}
#profile {
  color: white !important;
  text-align: center;
  padding: 14px 16px; 
  text-decoration: none !important;
  /* height: 52px; */
  overflow: auto;
}
#profile:hover {
  background-color: rgb(255, 23, 15) !important;
  cursor: pointer;
}
.arrow-down {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid white;
  margin-top: 10px;
}
.dropdown{
  top: 52px;
  width: 222px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  display: inline-block;
  background-color: rgb(218, 10, 10);
}
.dropdown li{
  list-style-type: none;
}

.navinator {
  z-index: 1;
  list-style-type: none;
  margin: 0;
  padding: 0;
  background-color: rgb(218, 10, 10);
  position: fixed;
  top: 0;
  width: 100%;
  height: 47px;
}
.navElement {
  float: left;
}
.anchor{
  display: block;
  color: white !important;
  text-align: center;
  padding: 14px 16px; 
  text-decoration: none !important;
  cursor: pointer;
}
.anchor:hover {
  background-color: rgb(255, 23, 15) !important;
}
.active {
  background-color: rgb(28, 13, 110) !important;
  float: right;
  width: 120px;
}
.active .anchor{
  align-items: center;
  justify-content: center;
}
.active .anchor:hover {
  background-color: rgb(32, 11, 156) !important;
}
.profile-image {
  width: 30px;
  height: 30px;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}
#profile {
  color: white !important;
  text-align: center;
  padding: 11px 16px; 
  text-decoration: none !important;
  /* height: 52px; */
  overflow: auto;
}
#profile:hover {
  background-color: rgb(255, 23, 15) !important;
  cursor: pointer;
}
.arrow-down {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid white;
  margin-top: 10px;
}
.dropdown {
  opacity:0;
  visibility: hidden;
    top: 47px;
    width: 222px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    /* display: inline-block; */
    background-color: rgb(6, 6, 6);
    position: absolute;
}
.drop-item:hover .dropdown{
  opacity:1;
  visibility: visible;
  transition: all .3s;
}

.dropdown li{
  list-style-type: none;
}
const Navbar = () => (
  <div style={{ zIndex: "1", position: "fixed", display: "flex" }}>
        <ul className="navinator">
          <li className="navElement">
            <a className="anchor" href="/home">
              Home
            </a>
          </li>
          <li className="navElement">
            <a className="anchor" href="/profile">
              Profile
            </a>
          </li>
          <li className="active">
            <a
              className="anchor"
              // href="#"

            >
              + New Post
            </a>
          </li>
          <li class="drop-item" style={{ float: "right" }}>
            <div
              id="profile"
              >
              <div style={{ float: "left", paddingRight: "10px" }}>
                <img
                  src=""
                  alt="error"
                  className="profile-image"
                />

              </div>
              <div style={{ float: "left", paddingRight: "5px" }}>
                whatever
              </div>
              <div style={{ float: "left" }} className="arrow-down"></div>
            </div>
            <div style={{float:"right"}} className="dropdown">

     <li>
        <a className="anchor" style={{ textAlign: "left" }} href="/profile">
          Profile
        </a>
      </li>
      <li>
        <a className="anchor" style={{ textAlign: "left" }} href="/profile">
          Profile
        </a>
      </li>

    </div>
          </li>
        </ul>

      </div>
)

ReactDOM.render(<Navbar />, document.getElementById('root'))