简体   繁体   English

响应式下拉菜单定位

[英]Responsive dropdown menu positioning

I'm creating a website for a client, and it contains a responsive dropdown menu which is loaded via a jquery script. 我正在为一个客户创建一个网站,它包含一个通过jquery脚本加载的响应下拉菜单。 The code is below. 代码如下。

I have a problem though. 我有一个问题。 When I give the container div #menu a position: static; 当我给容器div #menu一个位置时:static; everything seems to go well. 一切似乎进展顺利。 However, when I scroll and try to open the menu somewhere halfway the page (on mobile that is of course), It opens at the top of the page (above the content instead of over the content), so I have to scroll back to the top to see it. 但是,当我滚动并尝试在页面中间的某个位置(当然是在移动设备上)打开菜单时,它会在页面顶部(内容上方而不是内容上方)打开,因此我必须滚动回顶部看到它。

If i give the container div #menu a position: fixed; 如果我给容器div #menu一个位置:固定; the menu does load over the content, but then I can't scroll in the menu so I can't scroll to the last menu items. 菜单确实加载了内容,但随后我无法滚动菜单,因此无法滚动至最后一个菜单项。

Who can help me out here? 谁可以在这里帮助我?

EDIT: I can't post the client's site (it's on the client's development area and only cleared IP's have access) but I use almost the same code on this website . 编辑:我无法发布客户的网站(它位于客户的开发区域,并且只有清除的IP才可以访问),但是我在该网站上使用几乎相同的代码。 That website has the same problem, so you can test it there. 该网站存在相同的问题,因此您可以在那里进行测试。

I hope this will help to solve the problem, my client is getting impatient... thanks in advance! 我希望这将有助于解决问题,我的客户不耐烦...预先感谢!

HTML: HTML:

<div id="menu">
<div class="menu-top-menu-container">
    <ul id="menu-top-menu" class="menu">
        <li><a href="#">menu item 1</a>
        </li>
        <li><a href="#">menu item 2</a>
        </li>
        <li><a href="#">menu item 3</a>
        </li>
    </ul>
</div>

CSS 的CSS

/* Responsive menu */
#menu {
position: static;
float: left;
width: 98%;
height: auto!important;
margin: 110px 0 10px 0;
padding: 0px 1% 0px 1%;
line-height: 25px;
background-color: #efefef;
z-index: 99;
}
#menu li {
display: list-item;
list-style-type: none;
}
/* Menu items */
ul#menu-top-menu {
float: left;
top: 0px;
left: 0px;
width: 100%;
max-width: 1000px;
}
/* Lijst menu items */
#menu #menu-icon {
float: right;
position: fixed;
top: 53px;
right: 22px;
display: block;
background: url(http://www.bobdewebbouwer.nl/2014bybob/wp-content/themes/2014bybob/foto/menu.png);
background-repeat: no-repeat;
height: 30px;
width: 30px;
margin: 0 0px 5px 5px;
z-index: 999;
}
/* Menu icon tonen */
#menu #menu-top-menu {
display: none;
clear: both;
}
/* Menu verbergen tot er geklikt wordt op menu icon */
ul#menu-top-menu li {
padding: 10px 0 10px 0;
display: block;
clear: both;
text-align: left;
width: 95%;
}
/* Menu items wat hoger maken */
ul#menu-top-menu li a {
width: 100%;
}
ul#menu-top-menu li:hover ul {
margin-top: 10px;
}

jQuery jQuery的

jQuery(document).ready(function ($) {

/* prepend menu icon */
$('#menu').prepend('<div id="menu-icon"></div>');

/* toggle nav */
$("#menu-icon").on("click", function () {
    $("#menu-top-menu").slideToggle();
    $(this).toggleClass("active");
});

});

Try this: 尝试这个:

 var _responsiveDropDown = function (selectors) {

            $(document).on('click', selectors.dropDownSelector, function (e) {
                var _menuHeight = $(selectors.menuSelector).height(),
                    _openTopLimit = _menuHeight;

                if (selectors.isOpenTopSmallLimit) {
                    _openTopLimit = _openTopLimit / 2;
                }
                var _distance = $(selectors.lowerlimitSelector).offset().top - $(this).offset().top - _openTopLimit;

                if (_distance < _menuHeight) {
                    $(selectors.openedMenuSelector).css('top', -(_menuHeight + selectors.dropdownTopMargin) + 'px');
                } else {
                    $(selectors.menuSelector).css('top', '');
                }
            });
        }

usage: 用法:

var dropDownSelectors = {
                            menuSelector: 'ul#menu-top-menu',
                            openedMenuSelector: '.open > #menu-top-menu',
                            dropdownTopMargin: 19,
                            isOpenTopSmallLimit: false
                        }


responsiveDropDown(dropDownSelectors);

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

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