gpt4 book ai didi

javascript - 在同一页面中导航 anchor 标记时关闭左滑菜单?

转载 作者:行者123 更新时间:2023-12-02 15:10:32 26 4
gpt4 key购买 nike

当我切换打开左滑菜单并选择 anchor 标记链接时,它会导航到同一页面上的锚定部分,但菜单保持打开状态。

我想要一个简单的 JavaScript 函数来在每次单击 anchor 链接并导航到所需部分时关闭切换菜单

https://jsfiddle.net/trsjL9th/

我已经尝试了在堆栈溢出上找到的所有其他解决方案,包括:

$('#menu li').on('click', function(){ $("#menu").hide(); $("#menu-icon").removeClass("active"); });

但似乎没有任何效果,我对 Javascript 仍然很陌生,所以任何帮助或插入正确的方向将不胜感激。

Javascript:

(function(window) {

'use strict';

/**
* Extend Object helper function.
*/
function extend(a, b) {
for(var key in b) {
if(b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}

/**
* Each helper function.
*/
function each(collection, callback) {
for (var i = 0; i < collection.length; i++) {
var item = collection[i];
callback(item);
}
}

/**
* Menu Constructor.
*/
function Menu(options) {
this.options = extend({}, this.options);
extend(this.options, options);
this._init();
}

/**
* Menu Options.
*/
Menu.prototype.options = {
wrapper: '#o-wrapper', // The content wrapper
type: 'slide-left', // The menu type
menuOpenerClass: '.c-button', // The menu opener class names (i.e. the buttons)
maskId: '#c-mask' // The ID of the mask
};

/**
* Initialise Menu.
*/
Menu.prototype._init = function() {
this.body = document.body;
this.wrapper = document.querySelector(this.options.wrapper);
this.mask = document.querySelector(this.options.maskId);
this.menu = document.querySelector('#c-menu--' + this.options.type);
this.closeBtn = this.menu.querySelector('.c-menu__close');
this.menuOpeners = document.querySelectorAll(this.options.menuOpenerClass);
this._initEvents();
};

/**
* Initialise Menu Events.
*/
Menu.prototype._initEvents = function() {
// Event for clicks on the close button inside the menu.
this.closeBtn.addEventListener('click', function(e) {
e.preventDefault();
this.close();
}.bind(this));

// Event for clicks on the mask.
this.mask.addEventListener('click', function(e) {
e.preventDefault();
this.close();
}.bind(this));
};

/**
* Open Menu.
*/
Menu.prototype.open = function() {
this.body.classList.add('has-active-menu');
this.wrapper.classList.add('has-' + this.options.type);
this.menu.classList.add('is-active');
this.mask.classList.add('is-active');
this.disableMenuOpeners();
};

/**
* Close Menu.
*/
Menu.prototype.close = function() {
this.body.classList.remove('has-active-menu');
this.wrapper.classList.remove('has-' + this.options.type);
this.menu.classList.remove('is-active');
this.mask.classList.remove('is-active');
this.enableMenuOpeners();
};

/**
* Disable Menu Openers.
*/
Menu.prototype.disableMenuOpeners = function() {
each(this.menuOpeners, function(item) {
item.disabled = true;
});
};

/**
* Enable Menu Openers.
*/
Menu.prototype.enableMenuOpeners = function() {
each(this.menuOpeners, function(item) {
item.disabled = false;
});
};

/**
* Add to global namespace.
*/
window.Menu = Menu;

})(window);


var slideLeft = new Menu({
wrapper: '#o-wrapper',
type: 'slide-left',
menuOpenerClass: '.c-button',
maskId: '#c-mask'
});

var slideLeftBtn = document.querySelector('#c-button--slide-left');

slideLeftBtn.addEventListener('click', function(e) {
e.preventDefault;
slideLeft.open();
});

HTML:

<!-- Start of Slidesmenu -->
<div class="slidesmenu">
<div id="o-wrapper" class="o-wrapper">
<div class="c-buttons">
<button id="c-button--slide-left" class="c-button"></button>
</div>
<!-- other content in here -->
</div>
<!-- /o-wrapper -->
<!-- menus here -->
<div id="c-mask" class="c-mask"></div>
<!-- /c-mask -->
<nav id="c-menu--slide-left" class="c-menu c-menu--slide-left">
<button class="c-menu__close"></button>
<ul class="c-menu__items">
<li class="c-menu__item"><a href="#home" class="c-menu__link">Home</a></li>
<li class="c-menu__item"><a href="#about" class="c-menu__link">about</a></li>
<li class="c-menu__item"><a href="#contact" class="c-menu__link">contact</a></li>
<li class="c-menu__item"><a href="#howitworks" class="c-menu__link">howitworks</a></li>
<li class="c-menu__item"><a href="#tools" class="c-menu__link">Tools</a></li>
<li class="c-menu__item"><a href="#help" class="c-menu__link">Help</a></li>
<li class="c-menu__item"><a href="#customers" class="c-menu__link">Customers</a></li>
<li class="c-menu__item"><a href="#business" class="c-menu__link">business</a></li>
<li class="c-menu__item"><a href="#location" class="c-menu__link">location</a></li>
</ul>
</nav>
<!-- /c-menu slide-left -->
</div>
<!-- End of Slidesmenu -->
<div id="about"><p>About</p></div>
<div id="contact"><p>Contact</p></div>
<div id="howitworks"><p>How it Works</p></div>
<div id="tools"><p>Tools</p></div>
<div id="help"><p>Help</p></div>
<div id="customers"><p>Customers</p></div>
<div id="business"><p>Business</p></div>
<div id="location"><p>Location</p></div>

CSS:

.c-menu {
position: fixed;
z-index: 200;
background-color: #333;
transition: transform 0.3s;
}

.c-menu__items {
list-style: none;
margin: 0;
padding: 0; margin-top:90px;
}



.c-menu__items li {
font-size:20px; margin-top:20px;
}

/**
* Close button resets.
*/
.c-menu__close {
color: #fff;
background-color:#333;
font-size: 14px;
border: none;
box-shadow: none;
border-radius: none;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
cursor: pointer;
}

/**
* Close button resets.
*/
.c-menu__close:focus {
outline: none;
}

/**
* Body states.
*
* When a menu is active, we want to hide the overflows on the body to prevent
* awkward document scrolling.
*/
body.has-active-menu {
overflow: hidden;
}

.c-button {

background-color: blue;
background-repeat: no-repeat;
background-position: 0px 0px;
border: none; margin-top:13px; margin-left:5px;
cursor: pointer; z-index:1;
height: 25px;
padding-left: 25px;
vertical-align: middle;
}


.c-menu__close {
background-color: white;
background-repeat: no-repeat;
background-position: 0px 0px;
border: none; margin-top:10px; margin-right:2px;
cursor: pointer;
height: 25px;
padding-left: 25px;
vertical-align: middle;
}



.c-mask {
position: fixed;
z-index: 100;
top: 0;
left: 0;
overflow: hidden;
width: 0;
height: 0;
background-color: #000;
opacity: 0;
transition: opacity 0.3s, width 0s 0.3s, height 0s 0.3s;
}

.c-mask.is-active {
width: 100%;
height: 100%;
opacity: 0.7;
transition: opacity 0.3s;
}

.c-menu--slide-left,
.c-menu--slide-right,
.c-menu--push-left,
.c-menu--push-right {
width: 100%;
height: 100%;
overflow-y: auto;
}

@media all and (min-width: 320px) {
.c-menu--slide-left,
.c-menu--slide-right,
.c-menu--push-left,
.c-menu--push-right {
width: 300px;
}
}

.c-menu--slide-left .c-menu__item,
.c-menu--slide-right .c-menu__item,
.c-menu--push-left .c-menu__item,
.c-menu--push-right .c-menu__item {
display: block;
text-align: left;

}

.c-menu--slide-left .c-menu__item:first-child,
.c-menu--slide-right .c-menu__item:first-child,
.c-menu--push-left .c-menu__item:first-child,
.c-menu--push-right .c-menu__item:first-child {
border-top: none;
}

.c-menu--slide-left .c-menu__item:last-child,
.c-menu--slide-right .c-menu__item:last-child,
.c-menu--push-left .c-menu__item:last-child,
.c-menu--push-right .c-menu__item:last-child {
border-bottom: none;
}

.c-menu--slide-left .c-menu__link,
.c-menu--slide-right .c-menu__link,
.c-menu--push-left .c-menu__link,
.c-menu--push-right .c-menu__link {
display: block; text-decoration:none; margin-top:15px;
padding: 4px 24px;
color: #fff;
}

.c-menu__link:hover {color:#c8d100;}

.c-menu--slide-left .c-menu__close,
.c-menu--slide-right .c-menu__close,
.c-menu--push-left .c-menu__close,
.c-menu--push-right .c-menu__close {
display: block;
padding: 10px 10px; clear:both;
width: 30px;float:right;
}

.c-menu--slide-left,
.c-menu--push-left {
top: 0;
left: 0;
transform: translateX(-100%);
}

@media all and (min-width: 320px) {
.c-menu--slide-left,
.c-menu--push-left {
transform: translateX(-300px);
}
}

.c-menu--slide-left.is-active,
.c-menu--push-left.is-active {
transform: translateX(0);
}

.c-menu--slide-right,
.c-menu--push-right {
top: 0;
right: 0;
transform: translateX(100%);
}

@media all and (min-width: 320px) {
.c-menu--slide-right,
.c-menu--push-right {
transform: translateX(300px);
}
}

.c-menu--slide-right.is-active,
.c-menu--push-right.is-active {
transform: translateX(0);
}

.o-wrapper.has-push-left {
transform: translateX(100%);
}

@media all and (min-width: 320px) {
.o-wrapper.has-push-left {
transform: translateX(300px);
}
}

.o-wrapper.has-push-right {
transform: translateX(-100%);
}

@media all and (min-width: 320px) {
.o-wrapper.has-push-right {
transform: translateX(-300px);
}
}




#about { background:blue; height:100vh; width:100%; color:white; font-size:100px;}

#contact { background:yellow; height:100vh; width:100%; color:white; font-size:100px;}

#howitworks { background:green; height:100vh; width:100%; color:white; font-size:100px;}

#tools { background:red; height:100vh; width:100%; color:white; font-size:100px;}

#about { background:black; height:100vh; width:100%; color:white; font-size:100px;}

#help { background:green; height:100vh; width:100%; color:white; font-size:100px;}

#customers { background:purple; height:100vh; width:100%; color:white; font-size:100px;}

#business { background:orange; height:100vh; width:100%; color:white; font-size:100px;}

#location { background:brown; height:100vh; width:100%; color:white; font-size:100px;}

最佳答案

我将此代码添加到 JavaScript 的底部:

// Find all menu links
var navLinks = document.querySelectorAll('.c-menu__link');
// For each menu link
var index = 0, length = navLinks.length;
for ( ; index < length; index++) {
// Attach click event, on click call close function
navLinks[index].addEventListener('click',
function () {
slideLeft.close();
}
);
}

fiddle :https://jsfiddle.net/trsjL9th/3/

基本上按其类查找菜单中的所有链接,并附加一个调用菜单关闭函数的单击事件。

关于javascript - 在同一页面中导航 anchor 标记时关闭左滑菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34818156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com