【问题标题】:Jquery menu not working in mobile browserJquery 菜单在移动浏览器中不起作用
【发布时间】:2014-09-15 09:11:22
【问题描述】:

我有一个网站,我用过:

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

作为我的导航。 但是一切正常,但是当我缩小浏览器时,我尝试打开导航但它没有打开。经过数小时的调查,这个 Jquery 脚本存在问题。

        $(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('');
    }); 

关于我可能出错的地方的任何想法。

#nav 是打开菜单的类,但我需要在我的网站上使用此代码,但我想我可能遗漏了一些东西。

【问题讨论】:

  • 好吧 .. 我测试了您的 DEMO (osvaldas.info/examples/…) ,并且在 Firefox 中运行良好,在所有分辨率下都运行良好.. 我认为您在桌子上遇到了麻烦.. 不!?更明确..
  • 那不是我的演示。这就是我要复制的例子

标签: javascript jquery drop-down-menu responsive-design


【解决方案1】:

处理点击菜单图标的Javascript:

$(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 让它看起来不错:

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;
}

【讨论】:

  • 我添加了代码,但#nav链接仍然无效,请查看网站上更新的代码
  • 我想打开一个#nav 栏来显示菜单...而不是让它自动显示。
  • 立即查看(添加窗口调整大小事件处理程序和 css 显示)
  • 我已经添加了你给我的所有东西,但它仍然无法正常工作,我认为这与我发布的 3 行代码有关......
【解决方案2】:

使用窗口重新调整大小功能代替身体负载功能

$(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;
    }
}

【讨论】:

  • 那不工作,这个使用工作,但由于这 3 行代码它停止了,我添加这些的原因是因为它必须在页面顶部才能使导航工作...添加你的代码让我回到第一格,因为它一直在跳来跳去
  • 你能告诉我你改变了什么,而不是向我展示整个 CSS。 jquery 看起来一样...
  • 您从另一个网站复制了代码。你错过了菜单样式表&lt;link rel="stylesheet" type="text/css" media="all" href="/css/menus.css" /&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 2015-05-02
  • 1970-01-01
相关资源
最近更新 更多