简体   繁体   English

我的 jQuery 代码无法有效地打开和关闭移动菜单栏

[英]My jQuery code is not working efficiently to open and close mobile menu bar

Pls I need help with the nav bar(mobile view) I want to use jQuery to open and close the menu.请问我需要导航栏(移动视图)的帮助我想使用 jQuery 打开和关闭菜单。 This is my first every web dev project这是我的第一个每 web 开发项目

https://github.com/alwayswantedtocode/my-first-web-dev-project https://github.com/alwayswantedtocode/my-first-web-dev-project

.toggle() isn't enough! .toggle() 还不够!

Use remove/add class instead.请改用删除/添加 class。

$('.navigation').removeClass('').addClass('')

https://jsfiddle.net/drmtpocn/1/ https://jsfiddle.net/drmtpocn/1/

I can see you're defining your $(Document).ready function on each function, there is no need for that considering you will have most of your JQuery functions run inside it, so you can only define it at the start of your project.我可以看到你在每个 function 上定义你的 $(Document).ready function,没有必要考虑你将在其中运行大部分 JQuery 函数,所以你只能在项目开始时定义它. You will then place the other functions inside of only that one document.ready function.然后,您将把其他功能仅放在该文档中。准备好 function。

If you want a simple way of resolving this you can use the following code:如果你想要一个简单的方法来解决这个问题,你可以使用下面的代码:

   $(document).ready(() => {

    $('.menu-Icon span').click(() => {
        $('.navigation').addClass('open');
        $(".navigation").removeClass('close');

    });
    
    $(".X-close").click(() => {
        $(".navigation").toggleClass('close');
    });
});

this will remove the "close" class every time you open it and it will add the open class, when you close the menu after, it will then re-add the close class.这将在您每次打开它时删除“关闭”class,它会添加打开的 class,当您之后关闭菜单时,它会重新添加关闭的 class。

There is a another option with a built-in JQuery function that might make it look a bit better than just appearing.还有一个内置 JQuery function 的选项,这可能会使它看起来比仅仅出现好一点。

You would put the following into your JQuery您可以将以下内容放入您的 JQuery

  $(document).ready(() => {

    $('.menu-Icon').click(() => {
        $('.navigation').slideDown();

     

    });
    
    $(".X-close").click(() => {
        $('.navigation').slideUp();
    });
})

; ;

and your CSS on line 304 would change, here is the edited snippet.并且您在第 304 行的 CSS 会更改,这是编辑后的代码段。

@media only screen and (max-width:991px) /* Line 290 */
{
    .header .navigation{ 
        position: fixed;
        right:-250 ;/*initially zero*/
        width:250px;
        height: 100%;
        overflow-y: auto; 
        background-color: #3e5e75;
        /* opacity: 0.75; */
        top: 0;
        z-index: 1000;
        padding: 15px 0;
        transition: all 0.5s ease ;
       display: none; /*Changed element line 304*/
       /* Changed from visibility to display */
        
    }

For more details on the jQuery slideUP/slideDown please follow the link below:)有关 jQuery slideUP/slideDown 的更多详细信息,请点击以下链接:)

SlideDown滑下

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

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