gpt4 book ai didi

javascript - 单击时将按钮切换到扩展导航菜单

转载 作者:行者123 更新时间:2023-12-04 07:38:25 25 4
gpt4 key购买 nike

当我单击菜单 anchor 元素时,它会将我带回页面顶部。当我滚动浏览网站时,我希望菜单停留在我单击菜单 anchor 时滚动到的部分。我希望菜单 anchor 只充当扩展导航的打开和关闭功能。 [

function openMenu() {
document.getElementById('menu').style.width = "608px"
document.getElementById('content').style.marginLeft = "608px"
}

function closeMenu() {
document.getElementById('menu').style.width = "0px"
document.getElementById('content').style.marginLeft = "0px"
}
:root {
--main-color: #FEFF66;
--second-color: #000000;
--third-color: #FFFFFF;
}
* {
margin: 0;
padding: 0;
}
.slide {
margin: 18px;
font-size: 25px;
position: fixed;
}
.slide a {
text-decoration: none;
color: var(--second-color);
writing-mode: vertical-rl;
transform: rotate(-180deg);
}
.help a {
text-decoration: none;
color: var(--second-color);
}

.help {
display: flex;
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: var(--third-color);
color: var(--second-color);
overflow: hidden;
}

.links {
font-size: 40px;
line-height: 60px;
padding-top: 14px;
}

.header-menu {
writing-mode: vertical-rl;
transform: rotate(-180deg);
margin: 18px;
font-size: 25px;
padding-left: 55px;
}
<section id="header">

<span class="slide">
<a href="#" onclick="openMenu()">MENU</a>
</span>

<div id="menu" class="help">
<div><a class="header-menu" href="#" onclick="closeMenu()">MENU</a></div>
<div class="links">
<a href="#story">OUR STORY</a><br>
<a href="#rooms">ROOMS + BOOK</a><br>
<a href="#neighbourhood">THE NEIGHBOURHOOD</a><br>
<a href="#art">THE ART</a><br>
<a href="#faq">FAQ</a>
</div>
</div>

</section>

] 1

最佳答案

在您的点击监听器传递中,收到的事件如下:

<div><a class="header-menu" href="#" onclick="closeMenu($event)">MENU</a></div>
$event在这种情况下,模板中具有特殊含义。
然后,在您的 closeMenu 上功能相应地调整并防止浏览器对 anchor 元素的默认行为。
// Here, the function parameter can be named whatever you like,
// I named it "event" but anything else would work.
function closeMenu(event) {
// The line below will prevent the default behavior of the browser
// for this element and event, which means, it scroll to anywhere
// in this case, nor redirect to another page in case you had a URL
// in the href attribute.
event.preventDefault();
// Your code ...
}

关于javascript - 单击时将按钮切换到扩展导航菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67624158/

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