简体   繁体   English

为什么我的 onclick 汉堡包按钮在活动结束后停止工作?

[英]Why does my onclick hamburger button stop working after event?

I'm new to coding and have no idea what I'm doing.我是编码新手,不知道自己在做什么。 But I managed to make something cool, but I can't repeat the coolness.但是我设法做出了一些很酷的东西,但我不能重复这种酷。

I'm working on my mobile design, and I made it where you click the burger, sidenav opens, and when you click to the right of the sidebar, the sidebar closes.我正在做我的移动设计,我在你点击汉堡的地方做了它,sidenav 打开,当你点击侧边栏的右边时,侧边栏关闭。 Very cool.很酷。

The problem is that now it seems my onclick hamburger button is unclickable after it runs the event.问题是现在我的 onclick 汉堡包按钮在运行事件后似乎无法点击。 Please help me.请帮我。 What am I doing wrong?我究竟做错了什么? How can I make my hamburger have unlimited functional click events?我怎样才能让我的汉堡有无限的功能点击事件? Thanks.谢谢。

See my JS FIDDLE: http://jsfiddle.net/r214g0oc/1/查看我的 JS FIDDLE: http://jsfiddle.net/r214g0oc/1/

HTML: HTML:

<header id="navbar">

<div >
<img src="img/Logo.jpg" alt="logo" class="hmainlogo"/>
  <button class="openbtn" onclick="openNav()">☰</button>  

</div>

<div id="main"> 
    <div id="mySidebar" class="sidebar">
        <img src="img/Logo.jpg" alt="logo" class="hsblogo"/>
        <ul>

        <li><a href="index.php">Home</a></li>
        <li><a href="about.php">About</a></li>
        <li><a href="services.php">Services</a></li>
        <li><a href="rates.php">Rates</a></li>
        <li><a href="reviews.php">Reviews</a></li>
        </ul>
        <a href="freeconsult.php" id="hfreeconsult">Free Consult</a>
    </div>

</div>

</header>

JAVASCRIPT: JAVASCRIPT:

function openNav() {
    document.getElementById("mySidebar").style.width = "150px";
    document.getElementById("main").style.marginLeft = "150px";
}


function handleMousePos(event) {
    var mouseClickWidth = event.clientX;
    if (mouseClickWidth >= 150) {
        document.getElementById("mySidebar").style.left = '-150px';
    }
}

document.addEventListener("click", handleMousePos);

The code works every time, but it's just doing what you tell it (the bane of all computer programming).该代码每次都有效,但它只是按照您的指示执行(所有计算机编程的祸根)。

You're saying that if the user clicks in a place right of the 150px mark, it should move the sidebar to -150px (offscreen).你是说如果用户点击 150px 标记右边的地方,它应该将侧边栏移动到 -150px(屏幕外)。 Clicking again... moves the sidebar to -150px: in other words, it doesn't move it.再次单击...将侧边栏移动到 -150px:换句话说,它不会移动它。

If what you're after is a toggle, you need to check to see where the sidebar is now, and if it's already offscreen, move it back onscreen.如果你想要的是一个切换,你需要检查侧边栏现在在哪里,如果它已经在屏幕外,把它移回屏幕上。

   if (document.getElementById("mySidebar").style.left == '-150px')  document.getElementById("mySidebar").style.left =0;

This is pretty clunky coding style, though/ Better would be to use CSS, and define two styles: one for the sidebar in place and one without, and then check for which style is applied, and toggle the styles with addStyle and removeStyle.这是非常笨拙的编码风格,不过/更好的方法是使用 CSS,并定义两个 styles:一个用于侧边栏,一个没有,然后检查应用了哪种样式,并使用 addStyle 和 removeStyle 切换 styles。

Ciao, your problem is on handleMousePos .再见,你的问题出在handleMousePos上。 If mouseClickWidth >= 150 then you assign -150px to mySidebar style.如果mouseClickWidth >= 150 ,则将-150px分配给mySidebar样式。 Ok but you forgot the else statement to reassign 0px to your mySidebar .好的,但是您忘记了将0px重新分配给您的mySidebarelse语句。

So your code becomes:所以你的代码变成:

function handleMousePos(event) {
    var mouseClickWidth = event.clientX;
    if (mouseClickWidth >= 150) {
        document.getElementById("mySidebar").style.left = '-150px';
    }
    else {
        document.getElementById("mySidebar").style.left = '0px';
    }
}

Here your code modified.此处您的代码已修改。

Your handleMousePos(event) could be the problematic function here.您的handleMousePos(event)可能是这里有问题的 function。 You aren't removing the -150px after it gets added.添加后,您不会删除 -150px。 So my best suggestion would be to modify it this way:所以我最好的建议是这样修改它:

function openNav() {
document.getElementById("mySidebar").style.left = '0px';
document.getElementById("mySidebar").style.width = "150px";
document.getElementById("main").style.marginLeft = "150px";

} }

The better method would still be to use CSS for it and adding and removing a particular attribute like a class name which has its style already defined.更好的方法仍然是为它使用 CSS 并添加和删除一个特定的属性,如 class 名称,其样式已经定义。

Or you may opt to use a hidden checkbox and have your hamburger menu be a label for it.或者您可以选择使用隐藏的复选框并将您的汉堡菜单设为 label。 That will make it easier on the CSS side as you can just add:checked and define your expanded sidebar properties这将使 CSS 端更容易,因为您只需添加:选中并定义扩展的侧边栏属性

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

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