简体   繁体   English

首次点击时如何更改我的jquery脚本以动画移动菜单?

[英]how should i change my jquery script to animate mobile menu when first clicked?

i have the following mobile menu code i have been using for a while. 我有一段时间以来使用的以下手机菜单代码。 it works fine. 它工作正常。 i have an animation via css added so when the menu button is clicked it adds an animation for a smooth scroll - however i am noticing it does not work on the very fist click - all clicks after the first does work. 我通过css添加了一个动画,所以当单击菜单按钮时,它会添加动画以实现平滑滚动 - 但是我注意到它在第一次点击时不起作用 - 第一次点击后的所有点击都有效。 also if you see something gross in my jquery code do let me know, i am new to jquery and trying to learn by doing. 如果你在我的jquery代码中看到一些粗略的东西让我知道,我是jquery的新手,并试图通过实践来学习。

any help, or insight into something i am over looking, trying to resolve this would be greatly appreciated. 任何帮助,或洞察我正在寻找的东西,试图解决这一点将不胜感激。

jQuery(function() {
    //show the menu when button is clicked
    jQuery('#menu_btn').click(function() {
        if(jQuery('#menu').is(':visible')) {
            jQuery('#menu').animate({ left: '-100%' }, 'slow', function () {
                jQuery("#menu").css('display', 'none');
                jQuery('#menu_close').css('display', 'none');
            });
        } else {
            jQuery("#menu").css('display', 'block');
            jQuery('#menu').animate({ left: '0' }, 'slow', function(){
                jQuery('#menu_close').css('display', 'block');
            });
        }
    });

    //close menu when X button is clicked
    jQuery('#menu_close').click(function() {
        jQuery('#menu').animate({ left: '-100%' }, 'slow', function () {
            jQuery("#menu").css('display', 'none');
        });
    });

    callOnResize();
});

jQuery(window).resize( function(){
    callOnResize();
});

function callOnResize() {
    var winwidth = jQuery(window).width();
    if (winwidth < 760) {
        jQuery( '#menu' ).css({ display: 'none' });
        jQuery('#menu').animate({ left: '0' }, 'slow');
    } else if (winwidth >= 760) {
        jQuery( '#menu' ).css({ display: 'block' });
    }
}

the html of the menu is a very simple ul li view being output by wordpress 菜单的html是一个非常简单的ul li视图,由wordpress输出

<div id="menu">
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>
</div>

Elements are considered visible if they consume space in the document. 如果元素占用文档中的空间,则认为元素是可见的。 Visible elements have a width or height that is greater than zero. 可见元素的宽度或高度大于零。

Your menu div "#menu" should not be displayed by default "display:none". 默认情况下,不应显示菜单div“#menu”“display:none”。

The script should be something like this: 脚本应该是这样的:

jQuery('#menu_btn').click(function() {

        if(jQuery('#menu').is(':hidden')) {
            jQuery("#menu").css('display', 'block');
            jQuery('#menu').animate({ left: '0' }, 'slow', function(){
               jQuery('#menu_close').css('display', 'block');
            });
     });

        } else {

            jQuery('#menu').animate({ left: '-100%' }, 'slow', function () {
                jQuery("#menu").css('display', 'none');
                jQuery('#menu_close').css('display', 'none'); 
        }
    });

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

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