我想使用css 使汉堡菜单<HamburgerMenu className="Hamburger-Menu" id="HamMenu" style={styles}/>
在点击汉堡图标(复选框)时滑动 ,但不起作用<Hamburger id="ham-icon"/>
#ham-icon input[type="checkbox"]:checked + .Hamburger-Menu
Header.jsx:
import React, { useState, useEffect } from "react"
import Logo from "./icons/Logo.jsx"
import Hamburger from './icons/Hamburger.jsx'
import HamburgerMenu from './HamburgerMenu.jsx'
function Header(){
const [headerHeight, setHeaderHeight] = useState()
const [windowWidth, setWindowWidth] = useState(window.innerWidth)
useEffect(() => {
const HamMenu = document.getElementById("HamMenu")
const Header = document.getElementById("top-bar")
const HamMenuRect = HamMenu.getBoundingClientRect()
const HeaderRect = Header.getBoundingClientRect()
setHeaderHeight(HeaderRect.height)
console.log(HeaderRect.height)
}, []
)
const styles = {
'--Header-Height' : headerHeight + 'px',
'--Window-Width+5' : windowWidth+5 + 'px',
}
return(
<div className="Header" id="top-bar">
<Logo fill='black' width='70px' height='70px'/>
<div className="button-container">
<button> Products </button>
<button> Services </button>
<button> Company </button>
<button> Pricing </button>
</div>
<div>
<Hamburger id="ham-icon"/>
<HamburgerMenu className="Hamburger-Menu" id="HamMenu" style={styles}/>
</div>
</div>
)
}
export default Header
Hamburger.jsx(复选框):
import React from "react";
import styles from './Hamburger.module.css'
function Hamburger({...props}){
return (
<label className={styles.burger} {...props} htmlFor="burger">
<input type="checkbox" id="burger"/>
<span />
<span />
<span />
</label>
);
};
export default Hamburger;
汉堡菜单.jsx:
import { useEffect } from "react"
function HamburgerMenu({HamburgerMenu, ...props}){
return(
<div className={HamburgerMenu} {...props}>
<ul>
<li>Portfolio</li>
<li>Blog</li>
<li>About</li>
<li>Login</li>
<li>Language: EN</li>
</ul>
</div>
)
}
export default HamburgerMenu
索引.css:
*{
padding: 0;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
body{
overflow-x: hidden;
}
/* Header.jsx */
.Header{
display: flex;
justify-content: space-between;
align-items: center;
background-color: rgb(0, 186, 114);
width: 100%;
box-sizing: border-box;
padding: 8px 50px;
}
.button-container{
display: flex;
justify-content: center;
gap: 50px;
width: 100%;
}
.button-container button{
padding: 8px 12px;
font-size: 22px;
border: none;
border-radius: 5px;
background-color: rgba(255, 255, 255, 0);
color: white;
cursor: pointer;
}
.button-container button:hover{
background-color: hsla(0, 0%, 0%, 0.3);
}
.button-container button:active{
background-color: hsla(0, 0%, 0%, 0.2);
}
.Hamburger-Menu{
position: absolute;
background-color: hsl(0, 0%, 89%);
top: var(--Header-Height); /* from Header.jsx */
left: 100vw;
border: 5px solid hsl(0, 0%, 80%);
border-radius: 5px;
margin-top: 10px;
padding: 40px 20px;
transition: all 0.5s ease-in;
}
.Hamburger-Menu ul{
list-style-type: none;
white-space: nowrap;
}
#ham-icon input[type="checkbox"]:checked + .Hamburger-Menu{
right: 0vw;
}
您
checkbox
不是该.Hamburger-Menu
元素的兄弟(因为它包含在该label
元素内部),因此相邻或一般兄弟选择器在这种情况下将不起作用。请尝试
:has()
:或者如果你想继续使用相邻兄弟选择器