我有一个下拉菜单(响应),现在当用户将鼠标悬停显示下拉菜单时,在移动设备上显示所有级别的下拉菜单。
我想将其更改为与twitter bootstrap菜单相同。所以没有悬停显示下拉菜单,但显示用户点击项目时。我试图添加JS函数onclick但它永远不会被调用。
有没有办法在没有JavaScript但只是纯CSS的情况下制作它?
这是我的小提琴:http://jsfiddle.net/5vmayb6o/
更新
我有更新小提琴我成功添加JS功能来显示隐藏元素。但我还有一些问题。
HTML输出:
<nav class="animenu">
<input type="checkbox" id="button">
<label for="button" onclick="">Menu</label>
<ul>
<li class="first selected bc">
<a href="http://localhost/2/en-us/">Home</a>
</li>
<li>
<a href="http://localhost/2/en-us/About-Us">About Us</a>
<ul>
<li class="first">
<a href="http://localhost2/en-us/About-Us/StyleGuide">Style Guide</a>
</li>
<li class="last">
<a href="http://localhost2/en-us/About-Us/Test">Test</a>
<ul class="animenu-third-level">
<li class="first">
<a href="http://localhost2/en-us/About-Us/Test/3rd-level">3rd level</a>
</li>
<li>
<a href="http://localhost2/en-us/About-Us/Test/First-Page">First Page</a>
</li>
<li>
<a href="http://localhos2/en-us/About-Us/Test/Second-Page">Second Page</a>
</li>
<li>
<a href="http://localhos2/en-us/About-Us/Test/Third-Page">Third Page</a>
</li>
<li>
<a href="http://localhost2/en-us/About-Us/Test/Fourth-Page">Fourth Page</a>
</li>
<li class="last">
<a href="http://localhos2/en-us/About-Us/Test/Fifth-Page">Fifth Page</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
答案 0 :(得分:1)
试试这个
$(function() {
$('.animenu li').click(function(e) {
$(this).find('> ul').addClass('show');
$(this).addClass('selected');
e.stopImmediatePropogation();
});
$(document).click(function(e) {
if ($(e.target).closest('.animenu').length == 0) {
$('.animenu .show').parents('li').removeClass('selected');
$('.animenu ul').removeClass('show');
}
})
});
&#13;
.animenu > ul:after {
content: "";
display: table;
clear: both;
}
body {
-webkit-animation: bugfix infinite 1s;
}
@-webkit-keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
.animenu {
font: 13px Arial, Helvetica;
}
.animenu * {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.animenu ul {
margin: 0;
padding: 0;
list-style: none;
}
.animenu li {
position: relative;
}
.animenu li > ul.show {
opacity: 1;
visibility: visible;
margin: 0;
}
li {
outline: none;
}
.animenu li.selected > a {
color: white;
background: #ff0000;
}
.animenu input[type=checkbox] {
position: absolute;
top: -9999px;
left: -9999px;
}
.animenu label {
display: none;
cursor: pointer;
user-select: none;
}
.animenu > ul {
background: #f1f1f1;
}
.animenu > ul > li {
float: left;
}
.animenu > ul > li > a {
float: left;
padding: 1em 3em;
text-transform: uppercase;
}
.animenu > ul a {
color: #333;
text-decoration: none;
}
.animenu > ul ul {
position: absolute;
top: 100%;
left: 0;
z-index: 1;
opacity: 0;
visibility: hidden;
margin: 2em 0 0 0;
background-color: #f1f1f1;
background-image: none;
-webkit-transition-property: margin, opacity;
-moz-transition-property: margin, opacity;
-o-transition-property: margin, opacity;
transition-property: margin, opacity;
-webkit-transition-duration: .15s;
-moz-transition-duration: .15s;
-o-transition-duration: .15s;
transition-duration: .15s;
-webkit-transition-timing-function: ease-in-out;
-moz-transition-timing-function: ease-in-out;
-o-transition-timing-function: ease-in-out;
transition-timing-function: ease-in-out;
}
.animenu > ul ul li {
display: block;
}
.animenu > ul ul li:first-child > a {} .animenu > ul ul li:first-child > a:after {
content: '';
position: absolute;
left: 4em;
top: -12px;
border: 6px solid transparent;
border-bottom-color: inherit;
}
.animenu > ul ul li:last-child {
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
}
.animenu > ul ul li:last-child > a {} .animenu > ul ul a {
padding: 1em;
width: 175px;
display: block;
border-color: #373737;
}
.animenu > ul ul a {
/*background-color: #ff0000;
border-color: #fff;*/
color: #ff0000;
background-color: #f1f1f1;
}
@media screen and (max-width: 480px) {
.animenu > ul,
.animenu > ul ul {
visibility: visible;
opacity: 1;
display: none;
}
.animenu input[type=checkbox]:checked ~ label,
.animenu input[type=checkbox] ~ label:hover {
color: white;
}
.animenu label {
background-color: #f1f1f1;
color: #333;
text-transform: uppercase;
position: relative;
display: block;
padding: 1em 3em;
}
.animenu label:before {
position: absolute;
left: 0.5em;
top: 0.2em;
content: "\2261";
font-size: 2em;
}
.animenu > ul {
position: relative;
margin: 0.5em 0 !important;
padding: 0.25em;
background-color: #f1f1f1;
background-image: none;
}
.animenu > ul:after {
content: '';
position: absolute;
left: 2em;
top: -12px;
border: 6px solid transparent;
border-bottom-color: inherit;
}
.animenu > ul li {
display: block;
}
.animenu > ul > li {
float: none;
border: 0;
background-color: #f1f1f1;
background-image: none;
}
.animenu > ul > li > a {
float: none;
display: block;
padding: 1em;
}
.animenu > ul ul {
position: static;
background-color: #f1f1f1;
background-image: none;
margin: 0;
-webkit-transition-property: none;
-moz-transition-property: none;
-o-transition-property: none;
transition-property: none;
}
.animenu > ul ul li:first-child > a {} .animenu > ul ul li:first-child > a:after {
content: none;
}
.animenu > ul ul li:last-child > a {} .animenu > ul ul a {
padding-left: 2em;
display: block;
width: auto;
}
.animenu input[type=checkbox]:checked ~ ul {
display: block;
}
.animenu input[type=checkbox]:checked ~ ul ul {
display: block;
}
}
@media screen and (max-width: 600px) {
.animenu > ul > li > a {
padding: 1em 2em;
}
}
.animenu-third-level {
left: 100% !important;
top: 0 !important;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="animenu">
<input type="checkbox" id="button">
<label for="button" onclick="">Menu</label>
<ul>
<li class="first selected bc"><a href="http://localhost/2/en-us/">Home</a>
</li>
<li tabindex="0"><a href="javascript:void(0);">About Us</a>
<ul>
<li class="first"><a href="http://localhost2/en-us/About-Us/StyleGuide">Style Guide</a>
</li>
<li class="last" tabindex="0"><a href="javascript:void(0);">Test</a>
<ul class="animenu-third-level">
<li class="first"><a href="http://localhost2/en-us/About-Us/Test/3rd-level">3rd level</a>
</li>
<li><a href="http://localhost2/en-us/About-Us/Test/First-Page">First Page</a>
</li>
<li><a href="http://localhos2/en-us/About-Us/Test/Second-Page">Second Page</a>
</li>
<li><a href="http://localhos2/en-us/About-Us/Test/Third-Page">Third Page</a>
</li>
<li><a href="http://localhost2/en-us/About-Us/Test/Fourth-Page">Fourth Page</a>
</li>
<li class="last"><a href="http://localhos2/en-us/About-Us/Test/Fifth-Page">Fifth Page</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
&#13;
答案 1 :(得分:0)
我认为这就是你要找的东西:
下拉菜单只是HTML&amp; CSS 强>
http://koen.kivits.com/articles/pure-css-menu/