gpt4 book ai didi

javascript - 单击菜单外的任意位置可关闭菜单

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

即使在这里阅读了大约 20 个相关问题/线程后,我还是无法弄清楚这一点,已经尝试了几个小时,但就是无法得到它,所以真的希望有人能让我摆脱痛苦!

我有一个滑出菜单,当前需要用户单击“X”才能关闭它,我需要让用户可以单击菜单之外的任何位置或单击“X”来关闭它。

我创建了一个codepen以便更轻松地查看代码,因为代码相当多。

jQuery(document).ready(function($){
$('.btn-open-menu').click(function () {
$('header').addClass('open');
$('html').addClass('open');
$('html, body').addClass('noscroll');

});

$('.link-menu').click(function () {
$('header').removeClass('open');
$('html').removeClass('open');
$('html, body').removeClass('noscroll');

});

$('.btn-close-menu').click(function () {
$('header').removeClass('open');
$('html').removeClass('open');
$('html, body').removeClass('noscroll');

});

$('.btn-open-menu2').click(function () {
$('header').addClass('open');
$('html').addClass('open');
$('html, body').addClass('noscroll');
});

$('.link-menu').click(function () {
$('header').removeClass('open');
$('html').removeClass('open');
$('html, body').removeClass('noscroll');

});

$('.btn-close-menu2').click(function () {
$('header').removeClass('open');
$('html').removeClass('open');
$('html, body').removeClass('noscroll');

});



});
 header {
width: 100%;

background-color: transparent;
position: fixed;
top: 0;
left: 0;
z-index: 10;
transition: 0.5s ease-in-out;
}


header .btn-open-menu {
width: 25px;
height: 25px;
position: absolute;
top: 50px;
right: 50px;
cursor: pointer;
}

@media only screen and (max-width: 767px) {
header .btn-open-menu {
top: 25px;
right: 20px;
}
}

@media only screen and (max-width: 767px) {
header .header-content {
width: 100% !important;
}
}
header .header-content {
width: 30%;
height: 100vh;
height: calc(var(--vh, 1vh) * 100);
background-color: #FB4D98;
box-sizing: border-box;
position: absolute;
top: 0;
left:initial; right: -100%;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
flex-direction: column;
align-content: center;
transition: 0.8s ease-in-out;
}


@media only screen and (max-width: 767px) {
header .header-content .btn-close-menu {
top: 20px !important;
right: 20px !important;
}
}

header .header-content .btn-close-menu {
width: 25px;
height: 25px;
position: absolute;
top: 50px;
right: 50px;
cursor: pointer;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
flex-direction: row;
align-content: center;
}
header .header-content .btn-close-menu:before, header .header-content .btn-close-menu:after {
background: #F4FB82;
content: '';
display: block;
width: 100%;
height: 4px;
position: absolute;
transition: 0.5s ease-in-out;
}
header .header-content .btn-close-menu:before {
transform: rotate(45deg);
}

header .header-content .btn-close-menu:hover{
transform: rotate(180deg);
transition:0.5s ease-in-out;

}

header .header-content .btn-close-menu:after {
transform: rotate(-45deg);
}

header .header-content nav {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
flex-direction: column;
align-content: center;
}
header .header-content nav a {
color: #000;
letter-spacing: 0.5px;
line-height: 35px;
text-decoration: none;
transition: 0.5s;
}
header .header-content nav a:hover {
color: rgba(0, 0, 0, 0.5);
transition: 1s;
}
header .header-content .social {
display: flex;
justify-content: center;
align-items: center;
margin: 40px 0 0 0;
font-size: 20px;
}
header .header-content .social a {
color: #000;
text-decoration: none;
margin: 0 10px;
transition: 0.5s;
}
header .header-content .social a:hover {
color: #F4FB82;
transform: scale(1.5);
transition: 0.5s;
}


header.open {
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
transition: 0.5s ease-in-out;
}
header.open .btn-open-menu, header.open .logo {
opacity: 0;
transition: 1s ease-in-out;
}
header.open .header-content {
left:initial; right: 0;
transition: 0.8s ease-in-out;
}

.Menu-list {
font-family: "alliance";
font-size: 4vw;
line-height: 1.2;
text-transform: uppercase;
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
}

@media only screen and (max-width: 767px) {
.Menu-list {
font-family: "alliance";
font-size: 12vw;
}
}
.Menu-list-item {
position: relative;
color: transparent;
cursor: pointer;
}

ul.Menu-list{
padding-inline-start: 0px !important;

}
.Menu-list-item::before {
content: '';
display: block;
position: absolute;
top: 49%;
left: -10%;
right: -10%;
height: 4px;
border-radius: 4px;
margin-top: -2px;
background: #F4FB82;
transform: scale(0);

z-index: 1;
}
.Mask {
display: block;
position: absolute;
overflow: hidden;
padding-right: 5px;
padding-left: 5px;
color: #F4FB82;
top: 0;
height: 49%;
transition: all 1s cubic-bezier(0.16, 1.08, 0.38, 0.98);
}

@media only screen and (max-width: 767px) {
.Mask {
transition: all 0.2s cubic-bezier(0.16, 1.08, 0.38, 0.98);
}
}
.Mask span {
display: block;
}
.Mask + .Mask {
top: 48.9%;
height: 51.1%;
}
.Mask + .Mask span {
transform: translateY(-49%);
}
.Menu-list-item:hover .Mask, .Menu-list-item:active .Mask {
color: #000;
transform: skewX(12deg) translateX(5px);
}
.Menu-list-item:hover .Mask + .Mask, .Menu-list-item:active .Mask + .Mask {
transform: skewX(12deg) translateX(-5px);
}
.Menu-list-item:hover::before, .Menu-list-item:active::before {
transform: scale(1);
}


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<a class="btn-open-menu">
<span class="hamburguer"><img src="https://upload.wikimedia.org/wikipedia/commons/b/b2/Hamburger_icon.svg"></span>
</a>

<div class="header-content">
<a class="btn-close-menu"></a>

<nav>
<div class="Menu">
<ul class="Menu-list" data-offset="10">
<li class="Menu-list-item" data-offset="20" onclick="location.href='/home';">
Home
<span class="Mask"><span>Home</span></span>
<span class="Mask"><span>Home</span></span>
</li>
<li class="Menu-list-item" data-offset="16" onclick="location.href='/about';">
About
<span class="Mask"><span>About</span></span>
<span class="Mask"><span>About</span></span>
</li>
<li class="Menu-list-item" data-offset="12" onclick="location.href='/contact';">
Contact
<span class="Mask"><span>Contact</span></span>
<span class="Mask"><span>Contact</span></span>
</li>

</ul>
</div>
</nav>

</header>

最佳答案

更好的方法是在标题后面创建一个覆盖元素,并在单击时隐藏它和菜单。

jQuery(document).ready(function($){
$('.btn-open-menu').click(function () {
$('header').addClass('open');
$('html').addClass('open');
$('html, body').addClass('noscroll');

});

$('.link-menu').click(function () {
$('header').removeClass('open');
$('html').removeClass('open');
$('html, body').removeClass('noscroll');

});

$('.btn-close-menu').click(function () {
$('header').removeClass('open');
$('html').removeClass('open');
$('html, body').removeClass('noscroll');

});

$('.btn-open-menu2').click(function () {
$('header').addClass('open');
$('html').addClass('open');
$('html, body').addClass('noscroll');
});

$('.link-menu').click(function () {
$('header').removeClass('open');
$('html').removeClass('open');
$('html, body').removeClass('noscroll');

});

$('.btn-close-menu2').click(function () {
$('header').removeClass('open');
$('html').removeClass('open');
$('html, body').removeClass('noscroll');

});

$('.overlay').click(function () {
$('header').removeClass('open');
$('html').removeClass('open');
$('html, body').removeClass('noscroll');

});



});
 header {
width: 100%;

background-color: transparent;
position: fixed;
top: 0;
left: 0;
z-index: 10;
transition: 0.5s ease-in-out;
}


header .btn-open-menu {
width: 25px;
height: 25px;
position: absolute;
top: 50px;
right: 50px;
cursor: pointer;
}

@media only screen and (max-width: 767px) {
header .btn-open-menu {
top: 25px;
right: 20px;
}
}

@media only screen and (max-width: 767px) {
header .header-content {
width: 100% !important;
}
}
header .header-content {
width: 30%;
height: 100vh;
height: calc(var(--vh, 1vh) * 100);
background-color: #FB4D98;
box-sizing: border-box;
position: absolute;
top: 0;
left:initial; right: -100%;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
flex-direction: column;
align-content: center;
transition: 0.8s ease-in-out;
}


@media only screen and (max-width: 767px) {
header .header-content .btn-close-menu {
top: 20px !important;
right: 20px !important;
}
}

header .header-content .btn-close-menu {
width: 25px;
height: 25px;
position: absolute;
top: 50px;
right: 50px;
cursor: pointer;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
flex-direction: row;
align-content: center;
}
header .header-content .btn-close-menu:before, header .header-content .btn-close-menu:after {
background: #F4FB82;
content: '';
display: block;
width: 100%;
height: 4px;
position: absolute;
transition: 0.5s ease-in-out;
}
header .header-content .btn-close-menu:before {
transform: rotate(45deg);
}

header .header-content .btn-close-menu:hover{
transform: rotate(180deg);
transition:0.5s ease-in-out;

}

header .header-content .btn-close-menu:after {
transform: rotate(-45deg);
}

header .header-content nav {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
flex-direction: column;
align-content: center;
}
header .header-content nav a {
color: #000;
letter-spacing: 0.5px;
line-height: 35px;
text-decoration: none;
transition: 0.5s;
}
header .header-content nav a:hover {
color: rgba(0, 0, 0, 0.5);
transition: 1s;
}
header .header-content .social {
display: flex;
justify-content: center;
align-items: center;
margin: 40px 0 0 0;
font-size: 20px;
}
header .header-content .social a {
color: #000;
text-decoration: none;
margin: 0 10px;
transition: 0.5s;
}
header .header-content .social a:hover {
color: #F4FB82;
transform: scale(1.5);
transition: 0.5s;
}


header.open .overlay{
position:fixed;
width:100vW;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
transition: 0.5s ease-in-out;
}
header.open .btn-open-menu, header.open .logo {
opacity: 0;
transition: 1s ease-in-out;
}
header.open .header-content {
left:initial; right: 0;
transition: 0.8s ease-in-out;
}

.Menu-list {
font-family: "alliance";
font-size: 4vw;
line-height: 1.2;
text-transform: uppercase;
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
}

@media only screen and (max-width: 767px) {
.Menu-list {
font-family: "alliance";
font-size: 12vw;
}
}
.Menu-list-item {
position: relative;
color: transparent;
cursor: pointer;
}

ul.Menu-list{
padding-inline-start: 0px !important;

}
.Menu-list-item::before {
content: '';
display: block;
position: absolute;
top: 49%;
left: -10%;
right: -10%;
height: 4px;
border-radius: 4px;
margin-top: -2px;
background: #F4FB82;
transform: scale(0);

z-index: 1;
}
.Mask {
display: block;
position: absolute;
overflow: hidden;
padding-right: 5px;
padding-left: 5px;
color: #F4FB82;
top: 0;
height: 49%;
transition: all 1s cubic-bezier(0.16, 1.08, 0.38, 0.98);
}

@media only screen and (max-width: 767px) {
.Mask {
transition: all 0.2s cubic-bezier(0.16, 1.08, 0.38, 0.98);
}
}
.Mask span {
display: block;
}
.Mask + .Mask {
top: 48.9%;
height: 51.1%;
}
.Mask + .Mask span {
transform: translateY(-49%);
}
.Menu-list-item:hover .Mask, .Menu-list-item:active .Mask {
color: #000;
transform: skewX(12deg) translateX(5px);
}
.Menu-list-item:hover .Mask + .Mask, .Menu-list-item:active .Mask + .Mask {
transform: skewX(12deg) translateX(-5px);
}
.Menu-list-item:hover::before, .Menu-list-item:active::before {
transform: scale(1);
}

.social {
position: fixed;
bottom: 60px;
}

.copyright {
position: fixed;
bottom: 40px;
font-family: "alliance";
font-size: 14px;
color: #0E0E0E;
white-space: nowrap;
}

.privacy-policy {
position: fixed;
bottom: 20px;
font-family: "alliance";
font-size: 8px;
color: #0E0E0E !important;
white-space: nowrap;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<div class="overlay"></div>
<a class="btn-open-menu">
<span class="hamburguer"><img src="https://upload.wikimedia.org/wikipedia/commons/b/b2/Hamburger_icon.svg"></span>
</a>

<div class="header-content">
<a class="btn-close-menu"></a>

<nav>
<div class="Menu">
<ul class="Menu-list" data-offset="10">
<li class="Menu-list-item" data-offset="20" onclick="location.href='/home';">
Home
<span class="Mask"><span>Home</span></span>
<span class="Mask"><span>Home</span></span>
</li>
<li class="Menu-list-item" data-offset="16" onclick="location.href='/about';">
About
<span class="Mask"><span>About</span></span>
<span class="Mask"><span>About</span></span>
</li>
<li class="Menu-list-item" data-offset="12" onclick="location.href='/contact';">
Contact
<span class="Mask"><span>Contact</span></span>
<span class="Mask"><span>Contact</span></span>
</li>

</ul>
</div>
</nav>

</header>

关于javascript - 单击菜单外的任意位置可关闭菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68638066/

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