我不是最大的JS开发人员,所以它可能很简单。但是我在这里做了一个JSfiddle:https://jsfiddle.net/4xm3whzf/1/
我的问题是,由于我只是链接到同一页面上的标题,所以在选择菜单项后菜单不会关闭。如何确保它向下滚动并且菜单又消失了?
$(document).ready(function() {
$(".mobile_menu").simpleMobileMenu({
onMenuLoad: function(menu) {
console.log(menu)
console.log("menu loaded")
},
onMenuToggle: function(menu, opened) {
console.log(opened)
}
});
})
/**
* JQuery Simple MobileMenu
* https://github.com/Position2/jQuery-Simple-MobileMenu
*/
! function(s) {
var e = {
hamburgerId: "sm_menu_ham",
wrapperClass: "sm_menu_outer",
submenuClass: "submenu",
onMenuLoad: function() {
return !0
},
onMenuToggle: function() {
return !0
}
};
s.fn.simpleMobileMenu = function(n) {
if (0 === this.length) return this;
var t = {},
a = s(this),
i = function() {
t.hamburger = s("<div/>", {
id: t.settings.hamburgerId,
html: "<span></span><span></span><span></span><span></span>"
}), t.smmOuter = s("<div/>", {
class: t.settings.wrapperClass
}), a.appendTo(t.smmOuter), t.hamburger.add(t.smmOuter).appendTo(s("body"))
},
u = function() {
t.smmOuter.find("ul." + t.settings.submenuClass).each(function() {
var e = s(this),
n = e.closest("li"),
t = n.find("> a"),
a = s("<li/>", {
class: "back",
html: "<a href='#'>" + t.text() + "</a>"
});
n.addClass("hasChild"), a.prependTo(e)
})
};
t.settings = s.extend({}, e, n), i(), u(), "function" == typeof t.settings.onMenuLoad && t.settings
.onMenuLoad(a), t.hamburger.click(function(e) {
s("#" + t.settings.hamburgerId).toggleClass("open"), s("." + t.settings.wrapperClass).toggleClass(
"active").find("li.active").removeClass("active"), s("body").toggleClass(
"mmactive"), "function" == typeof t.settings.onMenuToggle && t.settings.onMenuToggle(
a, s("#" + t.settings.hamburgerId).hasClass("open"))
}), t.smmOuter.find("li.hasChild > a").click(function(e) {
e.preventDefault(), s("." + t.settings.wrapperClass).scrollTop(0), s(this).parent().addClass(
"active").siblings().removeClass("active")
}), t.smmOuter.find("li.back a").click(function(e) {
e.preventDefault(), s(this).closest("ul." + t.settings.submenuClass).parent().removeClass(
"active")
})
}
}(jQuery);
body {
margin: 0;
overflow-x: hidden;
font-family: 'Roboto', sans-serif;
background: green;
}
ul {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
.mobile_menu {
display: none;
position: relative;
}
.sm_menu_outer.active .mobile_menu {
display: block;
}
.sm_menu_outer {
height: 100vh;
overflow-y: scroll;
overflow-x: hidden;
position: fixed;
right: 0;
bottom: 0;
z-index: -9;
width: 100%;
opacity: 0;
background: linear-gradient(135deg, #0a5552 0%, #00a19a 100%);
-webkit-transition: opacity 0.2s;
-moz-transition: opacity 0.2s;
transition: opacity 0.2s;
}
.sm_menu_outer.active {
opacity: 1;
z-index: 999;
}
.mobile_menu {
padding-top: 7rem;
}
.sm_menu_outer .mobile_menu a {
color: white;
display: block;
letter-spacing: 1px;
padding: 1rem;
text-align: center;
font-size: 2rem;
text-transform: uppercase;
text-decoration: none;
}
/*Sub Menu anim */
.sm_menu_outer .mobile_menu .submenu {
background: linear-gradient(135deg, #0a5552 0%, #00a19a 100%);
height: 100%;
position: absolute;
right: -100%;
top: 0;
transition: all 0.5s ease 0s;
width: 100%;
z-index: 999;
}
.sm_menu_outer .mobile_menu li.active > .submenu {
right: 0;
}
.sm_menu_outer .mobile_menu li {
list-style-type: none;
border-bottom: 0.5px solid rgba(0, 0, 0, 0);
border-image: linear-gradient(to right, rgba(0, 0, 0, 0) 0%, #2a2a2a 20%, #2a2a2a 50%, #2a2a2a 80%, rgba(0, 0, 0, 0) 100%) 0 0 1 0;
color: #fff;
}
.sm_menu_outer .mobile_menu li.hasChild > a {
background: url("../images/next.png") no-repeat scroll 95% center / 5% auto;
}
/*Hamburger Animation */
#sm_menu_ham {
cursor: pointer;
float: right;
height: 25px;
position: absolute;
right: 5px;
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
transform: rotate(0deg);
-webkit-transition: all 0.5s ease-in-out 0s;
-moz-transition: all 0.5s ease-in-out 0s;
-o-transition: all 0.5s ease-in-out 0s;
transition: all 0.5s ease-in-out 0s;
width: 30px;
top: 2.6rem;
right: 2rem;
z-index: 9999;
}
#sm_menu_ham span {
background-color: #fff;
border-radius: 5px;
display: block;
height: 2px;
left: 0;
opacity: 1;
position: absolute;
-webkit-transform: rotate(0deg);
-moz-transform: rotate(0deg);
transform: rotate(0deg);
-webkit-transition: all 0.25s ease-in-out 0s;
-moz-transition: all 0.25s ease-in-out 0s;
-o-transition: all 0.25s ease-in-out 0s;
transition: all 0.25s ease-in-out 0s;
width: 30px;
}
#sm_menu_ham span:nth-child(1) {
top: 0;
}
#sm_menu_ham span:nth-child(2), #sm_menu_ham span:nth-child(3) {
top: 10px;
}
#sm_menu_ham span:nth-child(4) {
top: 20px;
}
#sm_menu_ham.open span:nth-child(1) {
left: 50%;
top: 18px;
width: 0;
}
#sm_menu_ham.open span:nth-child(2) {
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
transform: rotate(45deg);
}
#sm_menu_ham.open span:nth-child(3) {
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
transform: rotate(-45deg);
}
#sm_menu_ham.open span:nth-child(4) {
left: 50%;
top: 18px;
width: 0;
}
/*@media (min-width: 0px) and (max-width: 767px) {}
@media (min-width: 768px) and (max-width: 991px) {}
@media (min-width: 992px) and (max-width: 1024px) {}*/
@media (min-width: 1024px) {
#sm_menu_ham, .sm_menu_outer {
display: none;
}
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<nav id="desktop">
<ul id="menu">
<li>
<a href="#derfor">Derfor</a>
</li>
<li>
<a href="#features">Features</a>
</li>
<li>
<a href="#process">Proces</a>
</li>
<li>
<a href="/login.html">Login</a>
</li>
</ul>
</nav>
<nav id="mobile">
<ul class="mobile_menu">
<li>
<a href="#derfor">Derfor</a>
</li>
<li>
<a href="#features">Features</a>
</li>
<li>
<a href="#process">Proces</a>
</li>
<li>
<a href="/login.html">Login</a>
</li>
</ul>
</nav>
<div style="height:500px;padding-top:300px">
<h1 id="derfor">Derfor</h1>
</div>
<div style="height:500px;padding-top:300px">
<h1 id="features">features</h1>
</div>
<div style="height:500px;padding-top:300px">
<h1 id="process">Proces</h1>
</div>
<div style="height:500px;padding-top:300px">
<h1 id="derfor">Derfor</h1>
</div>
答案 0 :(得分:1)
您可以添加简单的事件侦听器,单击链接即可触发该事件。您可以在下面查看代码示例。
Array.from(document.querySelectorAll('ul.mobile_menu > li > a'), b => b.addEventListener('click', () => {
Array.from(
document.querySelectorAll("body.mmactive, div#sm_menu_ham"),
e => e.className = "")
document.getElementsByClassName('sm_menu_outer')[0].classList.remove("active");
}, false));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/Position2/jQuery-Simple-MobileMenu/master/dist/jquery-simple-mobilemenu.min.js"></script>
<nav id="desktop">
<ul id="menu">
<li>
<a href="#derfor">Derfor</a>
</li>
<li>
<a href="#features">Features</a>
</li>
<li>
<a href="#process">Proces</a>
</li>
<li>
<a href="/login.html">Login</a>
</li>
</ul>
</nav>
<nav id="mobile">
<ul class="mobile_menu">
<li>
<a href="#derfor">Derfor</a>
</li>
<li>
<a href="#features">Features</a>
</li>
<li>
<a href="#process">Proces</a>
</li>
<li>
<a href="/login.html">Login</a>
</li>
</ul>
</nav>
<div style="height:500px;padding-top:300px">
<h1 id="derfor">Derfor</h1>
</div>
<div style="height:500px;padding-top:300px">
<h1 id="features">features</h1>
</div>
<div style="height:500px;padding-top:300px">
<h1 id="process">Proces</h1>
</div>
<div style="height:500px;padding-top:300px">
<h1 id="derfor">Derfor</h1>
</div>