gpt4 book ai didi

css - 单击同一页面上的 anchor 链接时关闭汉堡菜单

转载 作者:行者123 更新时间:2023-11-28 14:56:57 25 4
gpt4 key购买 nike

我正在使用这个纯 CSS 汉堡包菜单代码:https://codepen.io/erikterwan/pen/EVzeRP

/*
* Made by Erik Terwan
* 24th of November 2015
* MIT License
*
*
* If you are thinking of using this in
* production code, beware of the browser
* prefixes.
*/

body
{
margin: 0;
padding: 0;

/* make it look decent enough */
background: #232323;
color: #cdcdcd;
font-family: "Avenir Next", "Avenir", sans-serif;
}

a
{
text-decoration: none;
color: #232323;

transition: color 0.3s ease;
}

a:hover
{
color: tomato;
}

#menuToggle
{
display: block;
position: relative;
top: 50px;
left: 50px;

z-index: 1;

-webkit-user-select: none;
user-select: none;
}

#menuToggle input
{
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;

cursor: pointer;

opacity: 0; /* hide this */
z-index: 2; /* and place it over the hamburger */

-webkit-touch-callout: none;
}

/*
* Just a quick hamburger
*/
#menuToggle span
{
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;

background: #cdcdcd;
border-radius: 3px;

z-index: 1;

transform-origin: 4px 0px;

transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
opacity 0.55s ease;
}

#menuToggle span:first-child
{
transform-origin: 0% 0%;
}

#menuToggle span:nth-last-child(2)
{
transform-origin: 0% 100%;
}

/*
* Transform all the slices of hamburger
* into a crossmark.
*/
#menuToggle input:checked ~ span
{
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}

/*
* But let's hide the middle one.
*/
#menuToggle input:checked ~ span:nth-last-child(3)
{
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}

/*
* Ohyeah and the last one should go the other direction
*/
#menuToggle input:checked ~ span:nth-last-child(2)
{
transform: rotate(-45deg) translate(0, -1px);
}

/*
* Make this absolute positioned
* at the top left of the screen
*/
#menu
{
position: absolute;
width: 300px;
margin: -100px 0 0 -50px;
padding: 50px;
padding-top: 125px;

background: #ededed;
list-style-type: none;
-webkit-font-smoothing: antialiased;
/* to stop flickering of text in safari */

transform-origin: 0% 0%;
transform: translate(-100%, 0);

transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}

#menu li
{
padding: 10px 0;
font-size: 22px;
}

/*
* And let's slide it in from the left
*/
#menuToggle input:checked ~ ul
{
transform: none;
}
<!--    Made by Erik Terwan    -->
<!-- 24th of November 2015 -->
<!-- MIT License -->
<nav role="navigation">
<div id="menuToggle">
<!--
A fake / hidden checkbox is used as click reciever,
so you can use the :checked selector on it.
-->
<input type="checkbox" />

<!--
Some spans to act as a hamburger.

They are acting like a real hamburger,
not that McDonalds stuff.
-->
<span></span>
<span></span>
<span></span>

<!--
Too bad the menu has to be inside of the button
but hey, it's pure CSS magic.
-->
<ul id="menu">
<a href="#"><li>Home</li></a>
<a href="#"><li>About</li></a>
<a href="#"><li>Info</li></a>
<a href="#"><li>Contact</li></a>
<a href="https://erikterwan.com/" target="_blank"><li>Show me more</li></a>
</ul>
</div>
</nav>

我遇到的问题是,当我单击菜单项时它仍然打开,因为它是一个带有 anchor 链接的单页站点。单击任何菜单项链接关闭菜单的最佳方法是什么?使用 javascript 删除菜单 onclick 不是一个好的选择,因为 X 还需要恢复为汉堡包,如果您使用此方法则不会。任何帮助表示赞赏,提前致谢!

最佳答案

可以在不破坏关闭图标行为的情况下添加一点 JavaScript。

查看下面修改后的代码片段,在单击菜单链接时添加 eventListener 时,只需取消选中与切换菜单汉堡图标对应的输入字段:

var menu = document.getElementById('menu');
var closeIcon = document.getElementById("closeIcon");

menu.addEventListener('click', handleMenuClick);

function handleMenuClick(event) {
if (event.target instanceof HTMLAnchorElement) {
closeIcon.checked = false;
}
}
/*
* Made by Erik Terwan
* 24th of November 2015
* MIT License
*
*
* If you are thinking of using this in
* production code, beware of the browser
* prefixes.
*/

body
{
margin: 0;
padding: 0;

/* make it look decent enough */
background: #232323;
color: #cdcdcd;
font-family: "Avenir Next", "Avenir", sans-serif;
}

a
{
text-decoration: none;
color: #232323;

transition: color 0.3s ease;
}

a:hover
{
color: tomato;
}

#menuToggle
{
display: block;
position: relative;
top: 50px;
left: 50px;

z-index: 1;

-webkit-user-select: none;
user-select: none;
}

#menuToggle input
{
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;

cursor: pointer;

opacity: 0; /* hide this */
z-index: 2; /* and place it over the hamburger */

-webkit-touch-callout: none;
}

/*
* Just a quick hamburger
*/
#menuToggle span
{
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;

background: #cdcdcd;
border-radius: 3px;

z-index: 1;

transform-origin: 4px 0px;

transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
opacity 0.55s ease;
}

#menuToggle span:first-child
{
transform-origin: 0% 0%;
}

#menuToggle span:nth-last-child(2)
{
transform-origin: 0% 100%;
}

/*
* Transform all the slices of hamburger
* into a crossmark.
*/
#menuToggle input:checked ~ span
{
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}

/*
* But let's hide the middle one.
*/
#menuToggle input:checked ~ span:nth-last-child(3)
{
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}

/*
* Ohyeah and the last one should go the other direction
*/
#menuToggle input:checked ~ span:nth-last-child(2)
{
transform: rotate(-45deg) translate(0, -1px);
}

/*
* Make this absolute positioned
* at the top left of the screen
*/
#menu
{
position: absolute;
width: 300px;
margin: -100px 0 0 -50px;
padding: 50px;
padding-top: 125px;

background: #ededed;
list-style-type: none;
-webkit-font-smoothing: antialiased;
/* to stop flickering of text in safari */

transform-origin: 0% 0%;
transform: translate(-100%, 0);

transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}

#menu li
{
padding: 10px 0;
font-size: 22px;
}

#menu a {
display: block;
}

/*
* And let's slide it in from the left
*/
#menuToggle input:checked ~ ul
{
transform: none;
}
<!--    Made by Erik Terwan    -->
<!-- 24th of November 2015 -->
<!-- MIT License -->
<nav role="navigation">
<div id="menuToggle">
<!--
A fake / hidden checkbox is used as click reciever,
so you can use the :checked selector on it.
-->
<input id="closeIcon" type="checkbox" />

<!--
Some spans to act as a hamburger.

They are acting like a real hamburger,
not that McDonalds stuff.
-->
<span></span>
<span></span>
<span></span>

<!--
Too bad the menu has to be inside of the button
but hey, it's pure CSS magic.
-->
<ul id="menu">
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Info</a>
</li>
<li>
<a href="#">Contact</a>
</li>
<li>
<a href="https://erikterwan.com/" target="_blank">Show me more</a>
</li>
</ul>
</div>
</nav>

请注意,我还修复了菜单列表元素的语义,将元素放入 li 元素中,而不是像以前那样反之亦然。加上 display: block; 到菜单元素,使它们全宽并且可以正确点击。

但是,这不应影响菜单的外观。

注意如果您想知道纯 CSS 解决方案是否可行。理论上,您可以尝试通过单击菜单链接来取消选中汉堡图标输入字段。您可以尝试使用同名的 radio 输入,这样它们就会相互切换。

不幸的是,有两件事反对这一点:

  1. 它会使菜单链接上的代码超出需要并添加更多非语义 html。
  2. 使用菜单链接上的 radio 输入方法,您将很难通过按下汉堡元素本身来切换菜单,因为显示汉堡的 radio 输入不会自行切换。

关于css - 单击同一页面上的 anchor 链接时关闭汉堡菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50789489/

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