我在React中编写一个简单的导航栏和下拉菜单时遇到问题。我在导航中为帐户创建了一个部分,将其悬停在该部分后,将在下面立即生成一个下拉菜单。
我尝试直接从w3网站复制此代码,但遇到更多问题,因此决定采用自己的方法...
我创建了一个由Navbar组件呈现的Dropdown功能组件。在名为hideDropdown的useState变量中跟踪Dropdown组件的可见/隐藏状态。
到目前为止,一切正常。尝试在导航栏中的“帐户”部分下方对齐此“下拉”组件时,出现了我的问题。无论我尝试什么,都无法将其放在屏幕上的正确位置。即使我从左侧对组件应具有的像素数量进行了硬编码,但调整屏幕大小也会破坏这一点。默认情况下,Dropdown组件在左侧生成。此外,只有left属性对其位置有任何影响。权利无可观察。另外,百分比似乎并不基于页面的宽度,因为要使Dropdown始终对齐在右侧,left属性必须为800%。
https://codepen.io/sjh5888/pen/rNaJgya
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;
Dropdown.js
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;
}
我是Web开发的新手,很开心。但是,这有点令人沮丧,因为我认为这应该是一个非常简单的解决方案。任何帮助,将不胜感激!
最佳答案
我想这就是你想要的
我更改了一些CSS和JS
的CSS
.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;
}
JS
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'))
我解决的是,在导航中的任何内容上打开
drop-down
时打开hover
,并将其位置设置在其上悬停的父对象下方