我尝试复制的菜单来自网站:https://matchbook.com/
问题:当您展开菜单而不是折叠它时出现问题,而如果您单击第一个链接(马/网球)它出现在错误的位置。我使用margin-left和margin-right根据条件移动div。我已经在网上查看了这样的菜单,但还没找到。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sidebar Push Menu</title>
<link href='https://fonts.googleapis.com/css?family=Roboto:400,500,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="wrapper">
<!--Sidebar-->
<div id="sidebar-wrapper">
<ul id="NavSub1" style="padding-top: 60px;" class="sidebar-nav sub-menu">
<li>Another Link</li>
<li>Another Link</li>
<li>Another Link</li>
</ul>
<ul class="sidebar-nav sub-parent">
<li style="background-color: #508a38;" class="item"><a href="#" id="menuToggle"><img id="flipSVG" style="width: 25px;" src="https://cdn1.iconfinder.com/data/icons/arrows-15/96/Arrow-Right2-512.png" /></a></li>
<li id="NavLink1" class="item"><a href="#"><img id="NavLink1" style="width: 35px;" src="https://matchbook.com/assets/images/icons/icon-sport-svg/tennis.svg" /><b style="padding-right: 10px;" class="Hidden"> Tennis </b>▷</a></li>
<li class="item"><a href="#"><img style="width: 25px;" src="https://matchbook.com/assets/images/icons/icon-sport-svg/tennis.svg" /> <b class="Hidden"> Cricket </b> </a></li>
<li class="item"><a href="#"><img style="width: 25px;" src="http://www.codingforums.com/images/rugby-union.svg" /> <b class="Hidden"> Soccer </b></a></li>
<li class="item"><a href="#"><img style="width: 25px;" src="http://www.codingforums.com/images/tennis.svg" /> <b class="Hidden"> Rugby league </b></a></li>
<li class="item"><a href="#"><img style="width: 25px;" src="http://www.codingforums.com/images/soccer.svg" /> <b class="Hidden"> Australian Rules </b></a></li>
<li class="item"><a href="#"><img style="width: 25px;" src="http://www.codingforums.com/images/az-sport.svg" /> <b class="Hidden"> All Sports </b></a></li>
</ul>
</div>
<!--Page Content-->
<div id="page-content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<h1>Where does it come from?</h1>
<p>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
</p>
</div>
</div>
</div>
</div>
</div>
<script src="js/NavScripts.js"></script>
<style>
</style>
<script>
$(document).ready(function () {
$("#menuToggle").click(function (e) {
e.preventDefault();
$("#wrapper").toggleClass("menuExpanded");
$(".Hidden").toggleClass("Show");
if ($("#wrapper").hasClass("menuExpanded")) {
$('#flipSVG').attr('src', 'https://cdn1.iconfinder.com/data/icons/nav-and-menu/64/arrow_back_left_navigation_user_interface_ui-512.png');
$('#flipSVG').css('margin-left', '110px');
$("#NavSub1").toggleClass("menuMoveRight");
} else {
$('#flipSVG').attr('src', 'https://cdn1.iconfinder.com/data/icons/arrows-15/96/Arrow-Right2-512.png');
$('#flipSVG').css('margin-left', '0');
}
});
/*******************/
//$("#menuToggle").click(function (e) {
// e.preventDefault();
//});
/******************/
$("#NavLink1").click(function (e) {
e.preventDefault();
$("#NavSub1").toggleClass("ToggleOpacity");
$("#NavSub1").toggleClass("menuMoveLeft");
});
});
</script>
<style>
/*Sidebar*/
#sidebar-wrapper {
z-index: 2;
position: absolute;
width: 70px;
height: 100%;
background-color: #43444A;
overflow: hidden;
}
/*menu content*/
#page-content-wrapper {
width: 100%;
position: absolute;
padding: 15px;
border: 5px solid green;
margin-left: 45px;
}
/*Change the width of the sidebar to display it*/
#wrapper.menuExpanded #sidebar-wrapper {
width: 180px;
transition: width .2s ease-in-out;
}
.item a {
white-space: nowrap;
}
.items a b {
margin-left: 5px;
}
.Hidden {
visibility: hidden;
}
.Show {
visibility: visible;
}
.date {
color: #fff;
}
#wrapper.menuExpanded #page-content-wrapper {
padding-left: 130px;
}
/*Sidebar menu styling*/
.sidebar-nav {
padding: 0;
list-style: none;
}
.sidebar-nav li {
text-indent: 20px;
line-height: 60px;
}
.sidebar-nav li a {
display: block;
text-decoration: none;
color: #ddd;
}
.sidebar-nav li a:hover {
background-color: #16A085;
}
.sub-parent li {
position: relative;
}
ul.sub-menu {
position: fixed;
background-color: #2B2F35;
list-style: none;
width: 180px;
height: 100%;
padding-left: 0;
color: #fff;
height: 100%;
opacity: 0;
transition: all ease .2s;
}
.menuMoveLeft {
margin-left: 70px !important;
}
.menuMoveRight {
margin-left: 180px !important;
}
.ToggleOpacity {
opacity: 1 !important;
}
</style>
</body>
</html>
答案 0 :(得分:0)
您的问题是,在您的条件语句if ($("#wrapper").hasClass("menuExpanded")) {...}
中,您设置了在MenuMoveRight
上切换班级#NavSub
的可能性,但您不会删除或切换该班级其他任何地方。
所以你最终有可能将其切换,但绝不会将其切换回来。这就是发生在你身上的事情。
答案是在初始链接点击时从MenuMoveRight
删除#NavSub1
,然后进入您可能重新添加的状态。
$(document).ready(function () {
$("#menuToggle").click(function (e) {
e.preventDefault();
$("#wrapper").toggleClass("menuExpanded");
$(".Hidden").toggleClass("Show");
$('#NavSub1').removeClass('menuMoveRight'); /* ADD THIS */
if ($("#wrapper").hasClass("menuExpanded")) {
$('#flipSVG').attr('src', 'https://cdn1.iconfinder.com/data/icons/nav-and-menu/64/arrow_back_left_navigation_user_interface_ui-512.png');
$('#flipSVG').css('margin-left', '110px');
$("#NavSub1").toggleClass("menuMoveRight");
} else {
$('#flipSVG').attr('src', 'https://cdn1.iconfinder.com/data/icons/arrows-15/96/Arrow-Right2-512.png');
$('#flipSVG').css('margin-left', '0');
}
});
$("#NavLink1").click(function (e) {
e.preventDefault();
$("#NavSub1").toggleClass("ToggleOpacity");
$("#NavSub1").toggleClass("menuMoveLeft");
});
});