简体   繁体   English

修复导航栏子菜单从屏幕外消失的问题

[英]Fixing navbar sub-menus from going off-screen

I am using the following script as navbar, however I can't figure out how to make the menus/submenus respect the screen sides, and not continue to open outside of the screen border.. I don't want to change the default behavior (direction) of all the menus, just to change it it it would be cut by any of the sides.. 我使用以下脚本作为导航栏,但是我无法弄清楚如何使菜单/子菜单尊重屏幕的两侧,并且不能继续在屏幕边框之外打开。.我不想更改默认行为(方向)的所有菜单,只要对其进行更改,它将被任意一侧剪切。

I found a similar post (and another one too), but I don't know how to implement the same answer to this current code. 我找到了类似的帖子 (也有另一篇 ),但是我不知道如何对当前代码实现相同的答案。

Example of the menu being cut by the side of the monitor: 显示器侧面被剪切的菜单示例: 在此处输入图片说明

Here is the code snippet, along with jsfiddle link too. 这是代码段,以及jsfiddle链接。

 $(document).ready(function() { /* MAIN MENU */ $('#main-menu > li:has(ul.sub-menu)').addClass('parent'); $('ul.sub-menu > li:has(ul.sub-menu) > a').addClass('parent'); $('#menu-toggle').click(function() { $('#main-menu').slideToggle(300); return false; }); $(window).resize(function() { if ($(window).width() > 700) { $('#main-menu').removeAttr('style'); } }); }); 
 body { background-color: #eee; background-image: url(../images/patterns/light_toast.png); color: #666; font-family: 'Open Sans', sans-serif; font-size: 12px; margin: 0px; padding: 0px; } a { color: #23dbdb; text-decoration: none; } a:hover { color: #000; } ol, ul { list-style: none; padding: 0px; margin: 0px; } #wrap { margin: 0 auto; } .inner { margin: 0 auto; max-width: 940px; padding: 0 40px; } .relative { position: relative; } .right { float: right; } .left { float: left; } /* HEADER */ #wrap>header { background-color: #333; padding-bottom: 20px; } .logo { display: inline-block; font-size: 0; padding-top: 15px; } #navigation { position: absolute; right: 40px; bottom: 0px; } #menu-toggle { display: none; float: right; } /* HEADER > MENU */ #main-menu { float: right; font-size: 0; margin: 10px 0; } #main-menu>li { display: inline-block; margin-left: 30px; padding: 2px 0; } #main-menu>li.parent { background-image: url(../images/plus-gray.png); background-size: 7px 7px; background-repeat: no-repeat; background-position: left center; } #main-menu>li.parent>a { padding-left: 14px; } #main-menu>li>a { color: #eee; font-size: 14px; line-height: 14px; padding: 30px 0; text-decoration: none; } #main-menu>li:hover>a, #main-menu>li.current-menu-item>a { color: #23dbdb; } /* HEADER > MENU > DROPDOWN */ #main-menu li { position: relative; } ul.sub-menu { /* level 2 */ display: none; left: 0px; top: 38px; padding-top: 10px; position: absolute; width: 150px; z-index: 9999; } ul.sub-menu ul.sub-menu { /* level 3+ */ margin-top: -1px; padding-top: 0; left: 149px; top: 0px; } ul.sub-menu>li>a { background-color: #333; border: 1px solid #444; border-top: none; color: #bbb; display: block; font-size: 12px; line-height: 15px; padding: 10px 12px; } ul.sub-menu>li>a:hover { background-color: #2a2a2a; color: #fff; } ul.sub-menu>li:first-child { border-top: 3px solid #23dbdb; } ul.sub-menu ul.sub-menu>li:first-child { border-top: 1px solid #444; } ul.sub-menu>li:last-child>a { border-radius: 0 0 2px 2px; } ul.sub-menu>li>a.parent { background-image: url(../images/arrow.png); background-size: 5px 9px; background-repeat: no-repeat; background-position: 95% center; } #main-menu li:hover>ul.sub-menu { display: block; /* show the submenu */ } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script> <div id="wrap"> <header> <div class="inner relative"> <a class="logo" href="#"><img src="https://www.gravatar.com/avatar/851659509f07dd2fe27882da61f0da0a?s=64" alt="fresh design web"></a> <a id="menu-toggle" class="button dark" href="#"><i class="icon-reorder"></i></a> <nav id="navigation"> <ul id="main-menu"> <li class="current-menu-item"><a href="http://www.freshdesignweb.com">Home</a></li> <li class="parent"> <a href="#">Features</a> <ul class="sub-menu"> <li><a href="#"><i class="icon-wrench"></i> Elements</a></li> <li><a href="#"><i class="icon-credit-card"></i> Pricing Tables</a></li> <li><a href="#"><i class="icon-gift"></i> Icons</a></li> <li> <a class="parent" href="#"><i class="icon-file-alt"></i> Pages</a> <ul class="sub-menu"> <li><a href="#">Full Width</a></li> <li><a href="#">Left Sidebar</a></li> <li><a href="#">Right Sidebar</a></li> <li><a href="#">Double Sidebar</a></li> </ul> </li> </ul> </li> <li><a href="#">Portfolio</a></li> <li class="parent"> <a href="#">Blog</a> <ul class="sub-menu"> <li><a href="#">Large Image</a></li> <li><a href="#">Medium Image</a></li> <li><a href="#">Masonry</a></li> <li> <a class="parent" href="#"><i class="icon-file-alt"></i> Pages ></a> <ul class="sub-menu"> <li><a href="#">Full Width</a></li> <li><a href="#">Left Sidebar</a></li> <li><a href="#">Right Sidebar</a></li> <li><a href="#">Double Sidebar</a></li> </ul> </li> <li><a href="#">Double Sidebar</a></li> <li><a href="#">Single Post</a></li> </ul> </li> <li><a href="#">Contact</a></li> </ul> </nav> <div class="clear"></div> </div> </header> 

in your sub-sub menus you're aligning based on left margin at 149px, just use the same attribute but instead for the right. 在子子菜单中,您是根据149px的左边距对齐的,只使用相同的属性,而是使用右边的属性。 right:149px; instead of left:149px; 而不是left:149px;

ul.sub-menu ul.sub-menu {
  /* level 3+ */
  right: 149px;
}

Also you can align the sub menus to the left of the nav items. 您也可以将子菜单对准导航项目的左侧。 To do this use : 为此使用:

ul.sub-menu { 
  /* level 2 */
  right: 0;
  left: initial;
}

I included both examples in this snippet: 我在此片段中包括了两个示例:

 $(document).ready(function() { /* MAIN MENU */ $('#main-menu > li:has(ul.sub-menu)').addClass('parent'); $('ul.sub-menu > li:has(ul.sub-menu) > a').addClass('parent'); $('#menu-toggle').click(function() { $('#main-menu').slideToggle(300); return false; }); $(window).resize(function() { if ($(window).width() > 700) { $('#main-menu').removeAttr('style'); } }); }); 
 body { background-color: #eee; background-image: url(../images/patterns/light_toast.png); color: #666; font-family: 'Open Sans', sans-serif; font-size: 12px; margin: 0px; padding: 0px; } a { color: #23dbdb; text-decoration: none; } a:hover { color: #000; } ol, ul { list-style: none; padding: 0px; margin: 0px; } #wrap { margin: 0 auto; } .inner { margin: 0 auto; max-width: 940px; padding: 0 40px; } .relative { position: relative; } .right { float: right; } .left { float: left; } /* HEADER */ #wrap>header { background-color: #333; padding-bottom: 20px; } .logo { display: inline-block; font-size: 0; padding-top: 15px; } #navigation { position: absolute; right: 40px; bottom: 0px; } #menu-toggle { display: none; float: right; } /* HEADER > MENU */ #main-menu { float: right; font-size: 0; margin: 10px 0; } #main-menu>li { display: inline-block; margin-left: 30px; padding: 2px 0; } #main-menu>li.parent { background-image: url(../images/plus-gray.png); background-size: 7px 7px; background-repeat: no-repeat; background-position: left center; } #main-menu>li.parent>a { padding-left: 14px; } #main-menu>li>a { color: #eee; font-size: 14px; line-height: 14px; padding: 30px 0; text-decoration: none; } #main-menu>li:hover>a, #main-menu>li.current-menu-item>a { color: #23dbdb; } /* HEADER > MENU > DROPDOWN */ #main-menu li { position: relative; } ul.sub-menu { /* level 2 */ display: none; top: 38px; padding-top: 10px; position: absolute; width: 150px; z-index: 9999; right:0; left:initial; } ul.sub-menu ul.sub-menu { /* level 3+ */ margin-top: -1px; padding-top: 0; right: 149px; top: 0px; } ul.sub-menu>li>a { background-color: #333; border: 1px solid #444; border-top: none; color: #bbb; display: block; font-size: 12px; line-height: 15px; padding: 10px 12px; } ul.sub-menu>li>a:hover { background-color: #2a2a2a; color: #fff; } ul.sub-menu>li:first-child { border-top: 3px solid #23dbdb; } ul.sub-menu ul.sub-menu>li:first-child { border-top: 1px solid #444; } ul.sub-menu>li:last-child>a { border-radius: 0 0 2px 2px; } ul.sub-menu>li>a.parent { background-image: url(../images/arrow.png); background-size: 5px 9px; background-repeat: no-repeat; background-position: 95% center; } #main-menu li:hover>ul.sub-menu { display: block; /* show the submenu */ } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script> <div id="wrap"> <header> <div class="inner relative"> <a class="logo" href="#"><img src="https://www.gravatar.com/avatar/851659509f07dd2fe27882da61f0da0a?s=64" alt="fresh design web"></a> <a id="menu-toggle" class="button dark" href="#"><i class="icon-reorder"></i></a> <nav id="navigation"> <ul id="main-menu"> <li class="current-menu-item"><a href="http://www.freshdesignweb.com">Home</a></li> <li class="parent"> <a href="#">Features</a> <ul class="sub-menu"> <li><a href="#"><i class="icon-wrench"></i> Elements</a></li> <li><a href="#"><i class="icon-credit-card"></i> Pricing Tables</a></li> <li><a href="#"><i class="icon-gift"></i> Icons</a></li> <li> <a class="parent" href="#"><i class="icon-file-alt"></i> Pages</a> <ul class="sub-menu"> <li><a href="#">Full Width</a></li> <li><a href="#">Left Sidebar</a></li> <li><a href="#">Right Sidebar</a></li> <li><a href="#">Double Sidebar</a></li> </ul> </li> </ul> </li> <li><a href="#">Portfolio</a></li> <li class="parent"> <a href="#">Blog</a> <ul class="sub-menu"> <li><a href="#">Large Image</a></li> <li><a href="#">Medium Image</a></li> <li><a href="#">Masonry</a></li> <li> <a class="parent" href="#"><i class="icon-file-alt"></i> Pages ></a> <ul class="sub-menu"> <li><a href="#">Full Width</a></li> <li><a href="#">Left Sidebar</a></li> <li><a href="#">Right Sidebar</a></li> <li><a href="#">Double Sidebar</a></li> </ul> </li> <li><a href="#">Double Sidebar</a></li> <li><a href="#">Single Post</a></li> </ul> </li> <li><a href="#">Contact</a></li> </ul> </nav> <div class="clear"></div> </div> </header> 

I guess the real question here is, where do you want it to go? 我想这里的真正问题是,您希望它去哪里? If there's no page real estate to the right of your menu, you can't place the submenu to the right, you have to place it to the left. 如果菜单右侧没有页面空间,则不能将子菜单置于右侧,而必须将其置于左侧。

ul.sub-menu ul.sub-menu { /* level 3+ */
    margin-top: -1px;
    padding-top: 0;
    left: -149px;
    top: 0px;
}

Note, the left value is changed from 149px to -149px. 请注意,左侧的值从149px更改为-149px。 That places the submenu to the left (where you have screen real estate), and not to the right, where you don't. 这会将子菜单放在左侧(您拥有屏幕空间),而不是在右侧(您没有屏幕空间)。

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

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