简体   繁体   English

用jQuery导航隐藏菜单

[英]navigation hide menu with jQuery

I build a nav menu with jquery, when I clicked on menu icon it will open but when I clicked again on menu icon it will not close 我用jquery构建一个导航菜单,当我点击菜单图标时它会打开,但是当我再次单击菜单图标时它不会关闭

this is html code: 这是HTML代码:

<aside class="aside_menu">
        <span class="arrow"></span>
        <div><a href="#">test</a></div>
        <div><a href="#">test</a></div>
        <div><a href="#">test</a></div>
        <div><a href="#">test</a></div>
        <div><a href="#">test</a></div>
        <div><a href="#">test</a></div>
        <div><a href="#">test</a></div>
        <div><a href="#">test</a></div>
        <div><a href="#">test</a></div>
        <div><a href="#">test</a></div>
</aside>

this is jquery code: 这是jquery代码:

$(document).ready(function () {
$(".aside_menu .arrow").click(function () {
    var aside_menu = $(this).parent();
    if (aside_menu.offset().right === 0)
        aside_menu.animate({right: "-200px"})
    else
        aside_menu.animate({right: "0px"})
})

}) })

the menu hide in the right side of page and only menu icon show up 菜单隐藏在页面右侧,只显示菜单图标

sorry for bad language 抱歉语言不好

.offset() returns an object containing the properties top and left , so your code will never work because aside_menu.offset().right is always undefined. .offset()返回一个包含topleft属性的对象,因此你的代码永远不会工作,因为aside_menu.offset().right总是未定义的。 See http://api.jquery.com/offset/ http://api.jquery.com/offset/

You have to use aside_menu.offset().left and then find the proper values to use in the if condition according to your page layout. 你必须使用aside_menu.offset().left ,然后根据你的页面布局找到在if条件中使用的正确值。

Simple things after the biggest resistance line, instead of jQuery, it's better to use JS for such simple things. 在最大阻力线之后的简单事情,而不是jQuery,最好使用JS来做这些简单的事情。 In addition, animations in JS are clumsy, especially on weaker com-puter and smartphones, which destroys user experience. 此外,JS中的动画是笨拙的,尤其是在较弱的计算机和智能手机上,这会破坏用户体验。 It's much easier to do it with simple JS and CSS. 使用简单的JS和CSS来实现它要容易得多。 Probably my code will not help you, but I hope to guide you on the right path. 可能我的代码不会帮助你,但我希望能引导你走上正确的道路。 Ps. PS。 Try not to use: "_" (underline) in classes and attributes and id's, because it is in terms of SEO, it is a shot in the knee, a better alternative is: "-" (dash). 尽量不要使用:“_”(下划线)在类和属性以及id中,因为它是在SEO方面,它是膝盖中的一个镜头,更好的选择是:“ - ”(破折号)。

 var menu = document.querySelector("#menu"); var button = document.querySelector("#button"); function toggleMenu(){ if(!menu.classList.contains("fade")){ menu.classList.add("fade"); }else{ menu.classList.remove("fade"); } } button.addEventListener("click", toggleMenu); 
 body{ margin: 0; display: flex; } #menu{ background: pink; width: 70px; height: 100%; text-align: center; transition: 0.34s; } #button{ height: 30px; transition: 0.34s; } .fade{ width: 0 !important; } .fade *{ display: none; } 
 <aside id="menu" class="fade"> <div><a href="#">test</a></div> <div><a href="#">test</a></div> <div><a href="#">test</a></div> <div><a href="#">test</a></div> <div><a href="#">test</a></div> <div><a href="#">test</a></div> <div><a href="#">test</a></div> <div><a href="#">test</a></div> <div><a href="#">test</a></div> <div><a href="#">test</a></div> </aside> <button id="button"> Button </button> 

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

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