我有一个汉堡包按钮,以便使用JS在我的HTML文件中显示侧边栏。基本上,这是一个<div>
的代码段,一个小的JS代码以及一个包含实际汉堡包的<span>
类。我想做的是将汉堡包按钮(虽然它不是由按钮类定义的)与标题对齐。我设法做到了,但是由于某种原因该按钮现在不起作用。有人可以告诉我为什么吗?还有其他方法可以将按钮与标头类内联吗?
/*CSS for header:*/
.header {
position: absolute;
top: 0;
left: 50%;
width: 90%;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
font-family: "Operator SSm A", "Operator SSm B", monospace;
font-weight: 300;
font-size: 1rem;
-webkit-transition: all 0.6s cubic-bezier(0.77, 0, 0.175, 1);
transition: all 0.6s cubic-bezier(0.77, 0, 0.175, 1);
opacity: 0;
margin-top: 10px;
pointer-events: none;
}
/*CSS for hamburger menu:*/
.overlay-nav {
height: 100%;
width: 0;
position: fixed;
top: 0;
left: 0;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.9);
overflow-x: hidden;
transition: 0.5s;
z-index: 2000;
}
<header class="header" style="width:100%" id="header">
<div class="loading"></div>
<div id="index" class="overlay-nav">
<a href="javascript:void(0)" class="closebtn-nav" onclick="closeNav()">×</a>
<div class="overlay-nav-content">
<a href="#">Home</a>
<a href="#">About Us</a>
<a href="#">Sessions</a>
<a href="#">Competitions</a>
<a href="#">Meetings</a>
</div>
</div>
<span style="font-size:20px; cursor:pointer" onclick="openNav()">☰ </span>
<script>
function openNav() {
document.getElementById("index").style.width = "100%";
}
function closeNav() {
document.getElementById("index").style.width = "0%";
}
</script>
<div class="header__logo">
<img src="" alt="" />
<h1></h1>
</div>
P.S是否发生是因为指针事件未设置?
答案 0 :(得分:1)
是的,这是因为您使用的是pointer-events: none
,它将禁用任何由鼠标引起的事件。也许您的意思是使用pointer: none
摆脱任何指针。
此外,我不确定您在opacity: 0
类上拥有.header
的原因,但这隐藏了该按钮甚至不可见。
https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events
答案 1 :(得分:0)
您的指针事件:没有停止点击事件,并且您已将opacity:0应用于标题,因此它不显示,首先删除pointer-events: none;
和opacity:1
/*CSS for header:*/
.header {
position: absolute;
top: 0;
left: 50%;
width: 90%;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
font-family: "Operator SSm A", "Operator SSm B", monospace;
font-weight: 300;
font-size: 1rem;
-webkit-transition: all 0.6s cubic-bezier(0.77, 0, 0.175, 1);
transition: all 0.6s cubic-bezier(0.77, 0, 0.175, 1);
opacity: 1;
margin-top: 10px;
pointer-events:all;
}
.closebtn-nav{
color:#ffffff;
}
/*CSS for hamburger menu:*/
.overlay-nav {
height: 100%;
width: 0;
position: fixed;
top: 25px;
left: 0;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.9);
overflow-x: hidden;
transition: 0.5s;
z-index: 2000;
}
<script>
function openNav() {
document.getElementById("index").style.width = "100%";
}
function closeNav() {
document.getElementById("index").style.width = "0%";
}
</script>
<header class="header" style="width:100%" id="header">
<div class="loading"></div>
<div id="index" class="overlay-nav">
<a href="javascript:void(0)" class="closebtn-nav" onclick="closeNav()">×</a>
<div class="overlay-nav-content">
<a href="#">Home</a>
<a href="#">About Us</a>
<a href="#">Sessions</a>
<a href="#">Competitions</a>
<a href="#">Meetings</a>
</div>
</div>
<span style="font-size:20px; cursor:pointer" onclick="openNav()">☰ </span>
<div class="header__logo">
<img src="" alt="" />
<h1></h1>
</div>
</header>