简体   繁体   中英

Jquery menu not working in mobile browser

I have a website, I have used :

http://osvaldas.info/drop-down-navigation-responsive-and-touch-friendly

as my navigation. However everything works, but when i make the browser smaller I try opening the navigation and it doesn't open. after hours of investigation, there is a problem in this Jquery script.

        $(document).ready(function () {
        $('#mobileMenu').append('<div id="nav" role="navigation">' + $('#mainNav').html() + '</div>');
        $('#nav').append('<div style="position:absolute;top:0px; right:0px;"></div>');
        $('#mainNav').html('');
    }); 

any ideas on where I might be going wrong.

#nav is the class that opens the menu, but i need this code on my site but i think i may be missing something.

Javascript to handle click on menu icon:

$(window).on('resize', function() {
    if(jQuery(window).width() < 661) {
        $('#nav').find('.desktopNone').eq(0).unbind('click');
        $('#nav').find('.desktopNone').eq(0).on('click', function() {
            if($('#nav').find('ul').eq(0).css('display') == 'none') {
                $('#nav').find('ul').eq(0).show();
            } else {
                $('#nav').find('ul').eq(0).hide();
            }
        });
    } else {
        $('#nav').find('ul').eq(0).show();
    }
});

CSS to make it looking good:

media="all"
@media only screen and (max-width: 40em)
#nav > ul {
    height: auto;
    display: none;
    position: absolute;
    left: 0;
    right: 0;
    background: #F1F1F1;
    z-index: 100;
}

Instead of body load function use window re size function

$(document).ready(function () {
   $('#mobileMenu').append('<div id="nav" role="navigation">' + $('#mainNav').html() + '</div>');
       $('#nav').append('<div style="position:absolute;top:0px; right:0px;"></div>');
          $('#mainNav').html('');
});

Css

.hivMenuLi a
{
    color: #ed917b;
    text-decoration: none;
    padding: 5px 5px;
    width: 52px !important;
    margin-right: 8px;
}

.hcvMenuLi a
{
    color: #ffc72a;
    text-decoration: none;
    padding: 5px 5px;
    width: 115px !important;
    margin-right: 8px;
}

.oncologyMenuLi a
{
    color: #624c79;
    text-decoration: none;
    padding: 5px 5px;
    ;
    margin-right: -8px;
    margin-right: 6px;
}

.homeLi a
{
    color: #808184;
    text-decoration: none;
    padding: 5px 5px;
    width: 61px auto;
    margin-right: 8px;
}

.aboutLi a
{
    color: #808184;
    text-decoration: none;
    padding: 5px 4px;
    width: 74px !important;
}


#nav > a
{
    display: none;
}

#nav > p
{
    display: none;
}



.clearfix > li
{
    display: inline-block;
    position: relative;
    float: left;
}

    .clearfix > li > ul
    {
        display: inline-block;
        position: absolute;
        display: none;
        width: 130px;
        background-color: grey;
    }

    .clearfix > li:hover > ul
    {
        display: block;
    }

.clearfix > .hivMenuLi:hover > a
{
    color: white;
    background-color: #ed917b;
}

.clearfix > .hcvMenuLi:hover > a
{
    color: white;
    background-color: #ffc72a;
}

.clearfix > .oncologyMenuLi:hover > a
{
    color: white;
    background-color: #624c79;
}

.clearfix > .aboutLi:hover > a
{
    color: white;
    background-color: #808184;
}


.clearfix > li a
{
    width: 105px;
    display: block;
    font-family: Helvetica-Neue, Helvetica, Arial, sans-serif;
    font-size: 18px;
    font-weight: bold;
}

.clearfix > li
{
    display: inline-block;
    position: relative;
}

    .clearfix > li > ul
    {
        display: inline-block;
        position: absolute;
        display: none;
    }

    .clearfix > li:hover > ul
    {
        display: block;
    }

    .clearfix > li:hover a
    {
        color: #54565b;
    }

    .clearfix > li > ul > li:hover
    {
    }

    .clearfix > li > ul > li > a
    {
        font-family: Helvetica-Neue, Helvetica, Arial, sans-serif;
        font-size: 14px;
        font-weight: normal;
        padding: 2px 12px;
        line-height: 24px;
        width: 130px !important;
        display: block !important;
        z-index: 99999;
    }

        .clearfix > li > ul > li > a:hover
        {
            color: white;
            font-family: Helvetica-Neue, Helvetica, Arial, sans-serif;
            font-size: 14px;
            font-weight: normal;
            padding: 2px 12px;
            display: inline;
        }

    .clearfix > li > ul
    {
        background-color: #e6e6e6;
        -webkit-box-shadow: inset 0px 4px 9px -2px rgba(26,26,26,1);
        -moz-box-shadow: inset 0px 4px 9px -2px rgba(26,26,26,1);
        box-shadow: 4px 7px 9px -2px rgba(26,26,26,1);
        border-bottom: 3px solid #54565b;
        width: 159px !important;
        z-index: 1;
    }

.hivMenuLi > ul > li:hover
{
    background-color: #ed917b;
}

.hcvMenuLi > ul > li:hover
{
    background-color: #ffc72a;
}

.oncologyMenuLi > ul > li:hover
{
    background-color: #624c79;
}

.aboutLi > ul > li:hover
{
    background-color: #808184;
}

#oncologyMenu
{
    margin-right: -7px;
}
/*.clearfix > li .hivMenuLi
        {
            display: inline-block;
            position: relative;
        }

            .clearfix > li .hivMenuLi > ul
            {
                display: inline-block;
                position: absolute;
                display: none;
            }

            .clearfix > li .hivMenuLi:hover > ul
            {
                display: block;
            }

            .clearfix > li .hivMenuLi:hover a, .clearfix > li .hivMenuLi:hover span
            {
                color: #000;

            }
        .clearfix > li > ul > li > a
        {
            color: #000;
        }
        .clearfix > li > ul > li > a:hover
        {
            color:#000;
        }
        .clearfix > li > ul
        {
            background-color: white;
        }
        .hivMenuLi > ul > li:hover
        {
            background-color: pink;
        }*/

@media only screen and (min-width:769px)
{
    .desktopmenu
    {
        float: right;
        /*margin-left: 550px;*/
        float: right;
position: relative;
top: 97px;
    }
}


@media only screen and ( max-width: 62.5em ) /* 1000 */
{
    #nav
    {
        width: 100%;
        position: static;
        margin: 0;
    }
}

@media only screen and ( max-width: 40em ) /* 640 */
{
    html
    {
        font-size: 75%; /* 12 */
    }

    #nav
    {
        position: relative;
        top: auto;
        left: auto;
    }

        #nav > a
        {
            width: 3.125em; /* 50 */
            height: 3.125em; /* 50 */
            text-align: left;
            text-indent: -9999px;
            background-color: grey;
            position: absolute;
            top: 5px !important;
            right: 7px;
        }

        #nav > p
        {
            display: block;
            width: 100%;
            height: 50px;
            background-color: #e4e4e4;
            line-height: 50px;
            font-size: 21px;
            color: black;
            font-weight: bold;
        }


        #nav > a:before,
        #nav > a:after
        {
            position: absolute;
            border: 2px solid #fff;
            top: 35%;
            left: 25%;
            right: 25%;
            content: '';
        }

        #nav > a:after
        {
            top: 60%;
        }


        #nav:not( :target ) > a:first-of-type,
        #nav:target > a:last-of-type
        {
            display: block;
        }


        /* first level */

        #nav > ul
        {
           height: auto;
            display: none;
            position: absolute;
            left: 0;
            right: 0;

        }

        #nav:target > ul
        {
            display: block;
            position: relative;
        }

        #nav > ul > li
        {
            width: 100%;
            float: none;
        }

            #nav > ul > li > a
            {
                height: auto;
                text-align: left;
                /*padding: 0 0.833em; /* 20 (24) */ */;
            }

            #nav > ul > li:not( :last-child ) > a
            {
                border-right: none;
                padding: 6px;
            }


        /* second level */

        #nav li ul
        {
            position: static;
            padding-top: 0;
            width: 95% !important;
            line-height: 20px;
            margin-bottom: 10px;
        }

    .clearfix > li > ul
    {
        background-color: #e6e6e6;
        -webkit-box-shadow: inset 0px 4px 9px -2px rgba(26,26,26,1);
        -moz-box-shadow: inset 0px 4px 9px -2px rgba(26,26,26,1);
        box-shadow: 4px 7px 9px -2px rgba(167, 163, 163, 1);
        border-bottom: 3px solid #54565b;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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