2016-07-05 104 views
2

幾個星期以來,我一直在研究一個Web應用程序項目,我目前正在研究一個下拉菜單。除了以下兩點之外,它的效果還是不錯的:下拉菜單出現問題

  • 當我通過點擊其中一個主要項目來推出菜單時,我想避免在其他子菜單的層次上展開綠色,菜單(例如通過點擊「菜單1」我不希望綠色塊出現在「菜單2」和「菜單3」下面)
  • 我也想讓我的項目更大。但是當我激活「填充:14px 16px;」在CSS代碼中的#menu li級別,子菜單項會以不好的方式移動。

你能幫我解決這些問題嗎? 謝謝!

斯特凡

下面是代碼:

$(function() { 
 
// Hide sub-menu: 
 
$(".subMenu").hide(); 
 
// Hide elements of the screen: 
 
$("#B3 th, #B3 td").hide(); 
 
$("[id^='B4_']").hide(); 
 
$("[id^='B5_']").hide(); 
 
// Hide/Display sub-menu once menu item is clicked: 
 
$(".mainlink").click(function() { 
 
\t $(".subMenu").hide(); 
 
    $("#B3 th, #B3 td").hide(); 
 
    $("[id^='B4_']").hide(); 
 
    $("[id^='B5_']").hide(); 
 
\t $(".level1").css("background-color","green"); 
 
\t $(".level2").css("background-color","orange"); 
 
\t $(this).parent().css("background-color","red"); 
 
    \t $(this).parent().find(".subMenu").toggle("slow", function() { 
 
\t \t // Animation complete. 
 
\t }); 
 

 
}); 
 

 
// Hide/Display elements of the body of the screen once sub-menu item is clicked: 
 
// Request/Create 
 
$("#item1_1").click(function() { 
 
$(".subMenu li").css("background-color","orange"); 
 
$(this).parent().css("background-color","red"); 
 
$("[id^='B5_']").hide(); 
 
$("[id='B5_1']").show(); 
 
}); 
 
// Request/Search 
 
$("#item1_2").click(function() { 
 
$(".subMenu li").css("background-color","orange"); 
 
$(this).parent().css("background-color","red"); 
 
$("[id^='B5_']").hide(); 
 
$("[id='B5_2']").show(); 
 
}); 
 
// Folder/Report/Create folder 
 
$("#item2_1").click(function() { 
 
$(".subMenu li").css("background-color","orange"); 
 
$(this).parent().css("background-color","red"); 
 
$("[id^='B5_']").hide(); 
 
$("[id='B5_3']").show(); 
 
}); 
 
// Folder/Report/Create report 
 
$("#item2_2").click(function() { 
 
$(".subMenu li").css("background-color","orange"); 
 
$(this).parent().css("background-color","red"); 
 
$("[id^='B5_']").hide(); 
 
$("[id='B5_4']").show(); 
 
}); 
 
// Folder/Report/Search 
 
$("#item2_3").click(function() { 
 
$(".subMenu li").css("background-color","orange"); 
 
$(this).parent().css("background-color","red"); 
 
$("[id^='B5_']").hide(); 
 
$("[id='B5_5']").show(); 
 
}); 
 
});
body { 
 
\t background-color: #3e8cbd; 
 
} 
 

 
header { 
 
\t border-style: solid; 
 
\t margin: 0; 
 
} 
 

 
footer { 
 
\t border-style: solid; 
 
\t margin: 0; 
 
} 
 

 
#menu ul { 
 
\t display: flex; 
 
    list-style-type: none; 
 
    padding: 0; 
 
} 
 

 
#menu li { 
 
\t width: 10em; 
 
\t color: white; 
 
\t text-align: center; 
 
\t border-right: 1px solid #bbb; 
 
\t border-top: 1px solid #bbb; 
 
\t background-color: green; 
 
\t /*padding: 14px 16px;*/ 
 
} 
 

 
#menu li:last-child { 
 
    border-right: none; 
 
} 
 

 
#menu ul ul { 
 
\t flex-direction: column; 
 
\t padding: 0; 
 
} 
 

 
#menu li li { 
 
\t background-color: orange; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body> 
 

 
<!-- Menu items --> 
 
<div id="menu"> 
 
\t <ul class="menu"> 
 
\t <li class="level1"><a id="item1" class="mainlink">Menu1</a> 
 
\t \t \t <!-- Request menu --> 
 
\t \t \t <ul class="subMenu" id="B2_1"> 
 
\t \t \t <li class="level2"><a id="item1_1" >Sub-Menu1-1</a></li> 
 
\t \t \t <li class="level2"><a id="item1_2" >Sub-Menu1-2</a></li> 
 
\t \t \t </ul> 
 
\t \t \t <!-- Request menu (end) --> 
 
\t </li> 
 
\t <li class="level1"><a id="item2" class="mainlink">Menu2</a> 
 
\t \t \t <!-- Dossier/Report screen --> 
 
\t \t \t <ul class="subMenu" id="B2_2"> 
 
\t \t \t <li class="level2"><a id="item2_1" >Sub-Menu2-1</a></li> 
 
\t \t \t <li class="level2"><a id="item2_2" >Sub-Menu2-2</a></li> 
 
\t \t \t <li class="level2"><a id="item2_3" >Sub-Menu2-3</a></li> 
 
\t \t \t </ul> 
 
\t \t \t <!-- Dossier/Report menu (end) --> 
 
\t </li> 
 
\t <li class="level1"><a id="item3" class="mainlink" onClick="alert('Development of the Menu3 functionalities postponed!')">Menu3</a></li> 
 
\t </ul> 
 
</div> \t 
 
<!-- Menu items (end) --> 
 

 
<!-- Screen body --> 
 
<div id="body"> 
 
\t <!-- B5 block --> 
 
\t <table id="B5"> 
 
\t \t <tr> 
 
\t \t \t <td> 
 
\t \t \t \t <input id="B5_1" type="button" value="Button1" onClick="alert('Action 1')"> 
 
\t \t \t </td> \t \t \t 
 
\t \t \t <td> 
 
\t \t \t \t <input id="B5_2" type="button" value="Button2" onClick="alert('Action 2')"> 
 
\t \t \t </td> 
 
\t \t \t <td> 
 
\t \t \t \t <input id="B5_3" type="button" value="Button3" onClick="alert('Action 3')"> 
 
\t \t \t </td> 
 
\t \t \t <td> 
 
\t \t \t \t <input id="B5_4" type="button" value="Button4" onClick="alert('Action 4')"> 
 
\t \t \t </td> 
 
\t \t \t <td> 
 
\t \t \t \t <input id="B5_5" type="button" value="Button5" onClick="alert('Action 5')"> 
 
\t \t \t </td> \t \t 
 
\t \t </tr> 
 
\t </table> 
 
\t <!-- B5 block (end) --> \t 
 
</div> \t 
 

 
</body>

+0

爲了避免菜單下面的 「綠色」,你應該使用'位置。 –

+0

'position:absolute'將使菜單與按鈕 – andyb

回答

0

使用的移position:absoluterelative這裏不會工作,因爲m ENU將與按鈕重疊。

我開始轉向大多數<li>風格的<a>,允許<li><a>的子菜單和高度(和綠色背景)增長不受影響。

我簡化了JavaScript和CSS,將JavaScript操縱的某種風格轉移回CSS。我還使用更新的代碼來使用更新的.on('click')語法,並使用HTML data attribute<a>鏈接到該按鈕,以消除始終查找元素的需求。在子菜單中absolute`:

// no need to use jQuery to find these all the time 
 
var menuAnchors = $('#menu > li > a'); // cache top level anchors 
 
var buttons = $("input[id^='B5_']"); // cache all buttons 
 

 
// declare one click handler for all #menu > li > a 
 
$('#menu').on('click', '> li > a', function() { 
 
    menuAnchors.css('background-color', ''); // reset red to green 
 
    this.style.backgroundColor = 'red'; // anchor elements get red background when clicked 
 

 
    $(".subMenu").hide().reset(); // hide all subMenu 
 
    $(this).next(".subMenu").toggle("slow", function() {}); // show nearest next subMenu 
 
}); 
 

 
// declare one click handler for all .subMenu anchors 
 
$('.subMenu').on('click', function(e) { 
 
    $(this).reset(); 
 
    e.target.style.backgroundColor = 'red'; // target anchor set to red 
 
    var buttonId = $(e.target).data('button'); // get buttonId from data attribute 
 
    $('#' + buttonId).show(); 
 
}); 
 

 
// custom function to both hide and reset background to orange 
 
$.fn.reset = function() { 
 
    buttons.hide(); // hide all buttons 
 
    $(this).find('a').css('background-color', ''); // reset red to orange 
 
    return $(this); 
 
}
body { 
 
    background-color: #3e8cbd; 
 
} 
 

 
ul { 
 
    display: flex; 
 
    list-style-type: none; 
 
    padding: 0; 
 
} 
 

 
#menu li { 
 
    width: 10em; 
 
    color: white; 
 
    text-align: center; 
 
} 
 

 
#menu li a { 
 
    display: block; 
 
    padding: 14px 16px; 
 
    cursor: pointer; 
 
    background-color: green; 
 
    border-right: 1px solid #bbb; 
 
    border-top: 1px solid #bbb; 
 
} 
 

 
#menu li:last-child a { 
 
    border-right: none; 
 
} 
 

 
.subMenu { 
 
    flex-direction: column; 
 
    padding: 0; 
 
    display: none; 
 
} 
 

 
#menu .subMenu a { 
 
    background-color: orange; 
 
} 
 

 
#B5 input { 
 
    display:none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<ul id="menu"> 
 
    <li> 
 
    <a class="menuLink">Menu1</a> 
 
    <ul class="subMenu"> 
 
     <li><a data-button="B5_1">Sub-Menu1-1</a></li> 
 
     <li><a data-button="B5_2">Sub-Menu1-2</a></li> 
 
    </ul> 
 
    </li> 
 
    <li> 
 
    <a class="menuLink">Menu2</a> 
 
    <ul class="subMenu"> 
 
     <li><a data-button="B5_3">Sub-Menu2-1</a></li> 
 
     <li><a data-button="B5_4">Sub-Menu2-2</a></li> 
 
     <li><a data-button="B5_5">Sub-Menu2-3</a></li> 
 
    </ul> 
 
    </li> 
 
    <li> 
 
    <a class="menuLink" onClick="alert('Development of the Menu3 functionalities postponed!')">Menu3</a> 
 
    </li> 
 
</ul> 
 

 
<table id="B5"> 
 
    <tr> 
 
    <td> 
 
     <input id="B5_1" type="button" value="Button1" onClick="alert('Action 1')"> 
 
    </td> 
 
    <td> 
 
     <input id="B5_2" type="button" value="Button2" onClick="alert('Action 2')"> 
 
    </td> 
 
    <td> 
 
     <input id="B5_3" type="button" value="Button3" onClick="alert('Action 3')"> 
 
    </td> 
 
    <td> 
 
     <input id="B5_4" type="button" value="Button4" onClick="alert('Action 4')"> 
 
    </td> 
 
    <td> 
 
     <input id="B5_5" type="button" value="Button5" onClick="alert('Action 5')"> 
 
    </td> 
 
    </tr> 
 
</table>

-1

你好,請更新以下CSS和在你的代碼,它會任何你想要的js ..

$(function() { 
 
// Hide sub-menu: 
 
$(".subMenu").hide(); 
 
// Hide elements of the screen: 
 
$("#B3 th, #B3 td").hide(); 
 
$("[id^='B4_']").hide(); 
 
$("[id^='B5_']").hide(); 
 

 

 
// Hide/Display sub-menu once menu item is clicked: 
 
$(".mainlink").click(function() { 
 
\t $(".level1").css("background-color","green"); 
 
\t $(".level2").css("background-color","orange"); 
 
\t $(this).parent().css("background-color","red"); 
 
    \t $(this).parent("li").find(".subMenu").slideToggle("slow", function() { 
 
\t \t // Animation complete. 
 
\t }); 
 

 
}); 
 

 
// Hide/Display elements of the body of the screen once sub-menu item is clicked: 
 
// Request/Create 
 
/* 
 
$("#item1_1").click(function() { 
 
$(".subMenu li").css("background-color","orange"); 
 
$(this).parent().css("background-color","red"); 
 
$("[id^='B5_']").hide(); 
 
$("[id='B5_1']").show(); 
 
}); 
 

 
// Request/Search 
 
$("#item1_2").click(function() { 
 
$(".subMenu li").css("background-color","orange"); 
 
$(this).parent().css("background-color","red"); 
 
$("[id^='B5_']").hide(); 
 
$("[id='B5_2']").show(); 
 
}); 
 
// Folder/Report/Create folder 
 
$("#item2_1").click(function() { 
 
$(".subMenu li").css("background-color","orange"); 
 
$(this).parent().css("background-color","red"); 
 
$("[id^='B5_']").hide(); 
 
$("[id='B5_3']").show(); 
 
}); 
 
// Folder/Report/Create report 
 
$("#item2_2").click(function() { 
 
$(".subMenu li").css("background-color","orange"); 
 
$(this).parent().css("background-color","red"); 
 
$("[id^='B5_']").hide(); 
 
$("[id='B5_4']").show(); 
 
}); 
 
// Folder/Report/Search 
 
$("#item2_3").click(function() { 
 
$(".subMenu li").css("background-color","orange"); 
 
$(this).parent().css("background-color","red"); 
 
$("[id^='B5_']").hide(); 
 
$("[id='B5_5']").show(); 
 
}); 
 
*/ 
 
});
header { 
 
\t border-style: solid; 
 
\t margin: 0; 
 
} 
 

 
footer { 
 
\t border-style: solid; 
 
\t margin: 0; 
 
} 
 

 
#menu ul { 
 
\t display: block; 
 
    list-style-type: none; 
 
    padding: 0; 
 
} 
 

 
#menu li { 
 
\t width: 10em; 
 
\t color: white; 
 
\t display: inline-block; 
 
\t float:left; 
 
\t width: 150px; 
 
\t height: 35px; 
 
\t line-height: 35px; \t 
 
\t text-align: center; 
 
\t border-right: 1px solid #bbb; 
 
\t border-top: 1px solid #bbb; 
 
\t background-color: green; 
 
\t /*padding: 14px 16px;*/ 
 
} 
 

 
#menu li ul li{ 
 
\t padding: 7px 32px; 
 
\t width: auto; 
 
\t height: auto; 
 

 
} 
 
#menu li:last-child { 
 
    border-right: none; 
 
} 
 

 
#menu ul ul { 
 
\t flex-direction: column; 
 
\t padding: 0; 
 
} 
 

 
#menu li li { 
 
\t background-color: orange; 
 
}

+1

重疊,代碼段不適用於我 – goto

0
  1. 絕對位置的子菜單中,相對的#menu李
  2. 沒有更多的子菜單項,因爲點1

demo

#main li {padding: 14px 16px; position:relative;} 
.subMenu {position:absolute;top:100%;left:0;}