如何解决滚动移动菜单消失的问题?

时间:2019-04-17 14:41:31

标签: javascript css

我的WordPress网站上存在一个问题:https://nazim.skypage.in/

在移动设备上,当您切换菜单打开时,一旦开始向下滚动,它将立即消失。真正奇怪的是,它不会在Chrome检查器中发生,我可以在菜单上上下滚动。

任何指导都将非常有帮助。谢谢!

这是我的HTML源代码:

<nav class="clearfix" id='cssmenu'>
<div id="head-mobile"></div>
<div class="button">Menu</div>
<ul id="menu-main-menu" class="menu"><li id="menu-item-10" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-10"><a href="https://nazim.skypage.in/" aria-current="page">Home</a></li>
<li id="menu-item-237" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-237"><a href="https://example.com/blog/">Blog</a></li>
<li id="menu-item-313" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-313"><a href="#">Link 1</a>
<ul class="sub-menu">
    <li id="menu-item-317" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-317"><a href="#">Sub Menu</a></li>
</ul>
</li>
<li id="menu-item-314" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-314"><a href="#">Link 2</a>
<ul class="sub-menu">
    <li id="menu-item-318" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-318"><a href="#">Sub Menu</a></li>
</ul>
</li>
<li id="menu-item-315" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-315"><a href="#">Link 3</a></li>
<li id="menu-item-316" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-316"><a href="#">Link 4</a></li>
</ul></nav>

这是菜单CSS:

nav ul {
    padding: 0;
    margin: 0
}

nav .button {
    display: none
}

#cssmenu,
#cssmenu #head-mobile,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a {
    border: 0;
    list-style: none;
    line-height: 1;
    display: block;
    position: relative;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box
}

#cssmenu:after,
#cssmenu>ul:after {
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0
}

#cssmenu #head-mobile {
    display: none
}

#cssmenu {
    float: right;
    padding: .5% 0
}

#cssmenu>ul>li {
    float: left
}

#cssmenu>ul>li>a {
    padding: 17px;
    font-size: 15px;
    letter-spacing: 1px;
    text-decoration: none;
    color: #42474e;
    text-transform: uppercase
}

#cssmenu>ul>li>a:hover {
    color: #265683
}

ul#menu-menu {
    float: right
}

#cssmenu ul li.active a,
#cssmenu>ul>li:hover>a {
    color: #265683
}

#cssmenu ul li.active,
#cssmenu ul li.active:hover,
#cssmenu ul li.has-sub.active:hover,
#cssmenu>ul>li:hover {
    border-bottom: 3px solid #265683;
    -webkit-transition: background .3s ease;
    -ms-transition: background .3s ease;
    transition: background .3s ease
}

#cssmenu>ul>li.has-sub>a:after,
#cssmenu>ul>li.has-sub>a:before {
    display: block;
    background: #185387;
    content: '';
    position: absolute
}

#cssmenu ul ul li,
#cssmenu>ul>li.has-sub>a:before {
    -webkit-transition: all .25s ease;
    -ms-transition: all .25s ease
}

#cssmenu>ul>li.has-sub>a {
    padding-right: 30px
}

#cssmenu>ul>li.has-sub>a:after {
    top: 24px;
    right: 11px;
    width: 8px;
    height: 2px
}

#cssmenu>ul>li.has-sub>a:before {
    top: 21px;
    right: 14px;
    width: 2px;
    height: 8px;
    transition: all .25s ease
}

#cssmenu>ul>li.has-sub:hover>a:before {
    top: 23px;
    height: 0
}

#cssmenu ul ul {
    position: absolute;
    left: -9999px
}

#cssmenu ul ul li {
    height: 0;
    background: #333;
    transition: all .25s ease
}

#cssmenu ul ul li.has-sub>a:after,
#cssmenu ul ul li.has-sub>a:before {
    position: absolute;
    display: block;
    background: #ddd;
    content: ''
}

#cssmenu li:hover>ul {
    left: auto
}

#cssmenu li:hover>ul>li {
    height: 35px
}

#cssmenu ul ul ul {
    margin-left: 100%;
    top: 0
}

#cssmenu ul ul li a {
    border-bottom: 1px solid rgba(150, 150, 150, .15);
    padding: 11px 15px;
    width: 190px;
    font-size: 15px;
    text-decoration: none;
    color: #ddd;
    font-weight: 400
}

#cssmenu ul ul li.last-item>a,
#cssmenu ul ul li:last-child>a {
    border-bottom: 0
}

#cssmenu ul ul li a:hover,
#cssmenu ul ul li:hover>a {
    color: #fff
}

#cssmenu ul ul li.has-sub>a:after {
    top: 16px;
    right: 11px;
    width: 8px;
    height: 2px
}

#cssmenu ul ul li.has-sub>a:before {
    top: 13px;
    right: 14px;
    width: 2px;
    height: 8px;
    -webkit-transition: all .25s ease;
    -ms-transition: all .25s ease;
    transition: all .25s ease
}

#cssmenu ul ul>li.has-sub:hover>a:before {
    top: 17px;
    height: 0
}

#cssmenu ul li.has-sub ul li.has-sub ul li:hover,
#cssmenu ul ul li.has-sub:hover {
    background: #363636
}

#cssmenu ul ul ul li.active a {
    border-left: 1px solid #333
}

#cssmenu>ul ul>li.has-sub>ul>li.active>a,
#cssmenu>ul>li.has-sub>ul>li.active>a {
    border-top: 1px solid #333
}


@media screen and (max-width:1000px) {
    #cssmenu ul,
    #cssmenu ul ul>li.has-sub>a:after,
    #cssmenu ul ul>li.has-sub>a:before,
    #cssmenu>ul>li.has-sub>a:after,
    #cssmenu>ul>li.has-sub>a:before {
        display: none
    }
    #cssmenu,
    #cssmenu ul,
    nav {
        width: 100%
    }
    #cssmenu ul li {
        width: 100%;
        border-top: 1px solid #eee
    }
    #cssmenu li:hover>ul>li,
    #cssmenu ul ul li {
        height: auto
    }
    #cssmenu ul li a,
    #cssmenu ul ul li a {
        width: 100%;
        border-bottom: 0
    }
    #cssmenu>ul>li {
        float: none
    }
    #cssmenu ul ul li a {
        padding-left: 25px;
        color: #ddd;
        background: 0 0
    }
    #cssmenu ul ul li {
        background: #333!important
    }
    #cssmenu ul ul li:hover {
        background: #363636!important
    }
    #cssmenu ul ul ul li a {
        padding-left: 35px
    }
    #cssmenu ul ul li.active>a,
    #cssmenu ul ul li:hover>a {
        color: #fff
    }
    #cssmenu ul ul,
    #cssmenu ul ul ul {
        position: relative;
        left: 0;
        width: 100%;
        margin: 0;
        text-align: left
    }
    #cssmenu #head-mobile {
        display: block;
        color: #ddd;
        font-size: 12px;
        font-weight: 700
    }
    nav .button {
        width: 94%;
        padding: 3%;
        right: 0;
        top: 0;
        cursor: pointer;
        z-index: 12399994;
        display: block;
        box-shadow: none;
        border-radius: 0;
        background: #f2b74c;
        color: #fff;
        text-transform: uppercase;
        font-size: 100%
    }
    nav .button:after,
    nav .button:before {
        right: 20px;
        width: 20px;
        position: absolute;
        display: block;
        content: ''
    }
    nav .button:after {
        top: 22px;
        height: 4px;
        border-top: 2px solid #fff;
        border-bottom: 2px solid #fff
    }
    nav .button:before {
        -webkit-transition: all .3s ease;
        -ms-transition: all .3s ease;
        transition: all .3s ease;
        top: 16px;
        height: 2px;
        background: #fff
    }
    nav .button.menu-opened:after {
        -webkit-transition: all .3s ease;
        -ms-transition: all .3s ease;
        transition: all .3s ease;
        top: 23px;
        border: 0;
        height: 2px;
        width: 19px;
        background: #fff;
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        -o-transform: rotate(45deg);
        transform: rotate(45deg)
    }
    nav .button.menu-opened:before {
        top: 23px;
        background: #fff;
        width: 19px;
        -webkit-transform: rotate(-45deg);
        -moz-transform: rotate(-45deg);
        -ms-transform: rotate(-45deg);
        -o-transform: rotate(-45deg);
        transform: rotate(-45deg)
    }
    #cssmenu .submenu-button {
        position: absolute;
        z-index: 99;
        right: 0;
        top: 0;
        display: block;
        border-left: 1px solid #eee;
        height: 50px;
        width: 50px;
        cursor: pointer
    }
    #cssmenu .submenu-button.submenu-opened {
        background: #262626
    }
    #cssmenu ul ul .submenu-button {
        height: 38px;
        width: 38px
    }
    #cssmenu .submenu-button:after {
        position: absolute;
        top: 23px;
        right: 21px;
        width: 8px;
        height: 2px;
        display: block;
        background: #ddd;
        content: ''
    }
    #cssmenu ul ul .submenu-button:after {
        top: 17px;
        right: 15px
    }
    #cssmenu .submenu-button.submenu-opened:after {
        background: #fff
    }
    #cssmenu .submenu-button:before {
        position: absolute;
        top: 20px;
        right: 24px;
        display: block;
        width: 2px;
        height: 8px;
        background: #ddd;
        content: ''
    }
    #cssmenu ul ul .submenu-button:before {
        top: 14px;
        right: 18px
    }
    #cssmenu .submenu-button.submenu-opened:before {
        display: none
    }
    #cssmenu ul ul ul li.active a {
        border-left: none
    }
    #cssmenu>ul ul>li.has-sub>ul>li.active>a,
    #cssmenu>ul>li.has-sub>ul>li.active>a {
        border-top: none
    }

这是我的菜单js代码:

<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<script>(function($) {
$.fn.menumaker = function(options) {  
 var cssmenu = $(this), settings = $.extend({
   format: "dropdown",
   sticky: false
 }, options);
 return this.each(function() {
   $(this).find(".button").on('click', function(){
     $(this).toggleClass('menu-opened');
     var mainmenu = $(this).next('ul');
     if (mainmenu.hasClass('open')) { 
       mainmenu.slideToggle().removeClass('open');
     }
     else {
       mainmenu.slideToggle().addClass('open');
       if (settings.format === "dropdown") {
         mainmenu.find('ul').show();
       }
     }
   });
   cssmenu.find('li ul').parent().addClass('has-sub');
multiTg = function() {
     cssmenu.find(".has-sub").prepend('<span class="submenu-button"></span>');
     cssmenu.find('.submenu-button').on('click', function() {
       $(this).toggleClass('submenu-opened');
       if ($(this).siblings('ul').hasClass('open')) {
         $(this).siblings('ul').removeClass('open').slideToggle();
       }
       else {
         $(this).siblings('ul').addClass('open').slideToggle();
       }
     });
   };
   if (settings.format === 'multitoggle') multiTg();
   else cssmenu.addClass('dropdown');
   if (settings.sticky === true) cssmenu.css('position', 'fixed');
resizeFix = function() {
  var mediasize = 1000;
     if ($( window ).width() > mediasize) {
       cssmenu.find('ul').show();
     }
     if ($(window).width() <= mediasize) {
       cssmenu.find('ul').hide().removeClass('open');
     }
   };
   resizeFix();
   return $(window).on('resize', resizeFix);
 });
  };
})(jQuery);

(function($){
$(document).ready(function(){
$("#cssmenu").menumaker({
   format: "multitoggle"
});
});
})(jQuery);
</script>

2 个答案:

答案 0 :(得分:0)

我在您的代码中找不到任何内容,因此这只是一个疯狂的猜测。但这听起来像是JavaScript问题。我认为当用户调整窗口大小时,菜单(及其样式)正在重置。大多数移动浏览器在滚动时都会隐藏网址栏,从而触发javascript中的窗口调整大小事件。

答案 1 :(得分:0)

对我来说,SKeurentjes 的疯狂猜测是正确的。 什么对我有用:

  • 去掉js中的所有resize事件
  • 重复菜单的 html,一个在类下,例如。 .menu.mobile,其他在 .menu.desktop 下。媒体查询不/显示每一个。
  • 然后将不同的 js 行为绑定到 .menu 和 .desktop 类。