gpt4 book ai didi

javascript - 当我打开滑出式菜单时覆盖

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:50 25 4
gpt4 key购买 nike

我有一个扩展导航栏,每当扩展导航打开时,我一直难以实现覆盖,就像滑出导航打开时 youtube 覆盖的显示方式一样。请帮忙。代码已得到很好的注释。

下面是展开导航的javascript代码,我用的是jquery

'use strict';

// Open offsite navigation.
$('#nav-expander').on('click', function(e) {
e.preventDefault();
$('nav').toggleClass('nav-expanded');
});

// Close offsite navigation.
$('.menu .close').on('click', function(e) {
e.preventDefault();
$('nav').toggleClass('nav-expanded');
});

// Close offsite navigation after user click on an link in navigation.
$('.menu a').on('click', function(e) {
//se.preventDefault();
$('nav').removeClass('nav-expanded');
});

$('.body').on('click', function(e) {
//e.preventDefault();
$('nav').removeClass('nav-expanded');
});

$('.body2').on('click', function(e) {
//e.preventDefault();
$('nav').removeClass('nav-expanded');
});

$('.btn').on('click', function(e) {
//e.preventDefault();
$('nav').removeClass('nav-expanded');
});

//ending of offsite navigation
/************************************
*************************************
*************************************
GENERAL STYLING
*************************************
*************************************
************************************/

body{
background-color: #F2F3F4;
}

/************************************
GENERAL STYLING ENDING
************************************/

/************************************
*************************************
*************************************
HEADER STYLING
*************************************
*************************************
************************************/

header{
height: 57px;
border-bottom: 1px #DDDDDD solid;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}

.main__header{
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-between;
align-items: center;
flex-grow: 1;
}

.header__content__left{
display: flex;
flex-direction: row;
flex-grow: 1;
align-items: center;
}

.header__content__right{
display: inline-flex;
flex-direction: row;
flex-grow: 1;
justify-content: flex-end;
align-items: center;
}

.header__content__right a{
font-weight: 600;
}

.header__margin__right{
margin-right: .5rem;
}

.header__margin__left{
margin-left: 1rem;
}

/************************************
*************************************
*************************************
SLIDE-OUT NAVIGATION STYLING
*************************************
*************************************
************************************/

.menu{
position: relative;
width: 280px;
display: block;
height: 100%;
top: 0;
left:-300px; /*was originally t right when the nav bar was on the right side*/
position: fixed;
z-index: 100;
text-align: center;
transition: left 0.1s; /** default on the right **/
overflow-y: auto; /* makes the expanding nav scrollable */
}

.menu.nav-expanded{
left: 0; /* was at right before, for nav bar to expand from left */
}

.menu .close{
font-size: 30px;
margin-right: 10px;
margin-top:10px;
}

.navbar__header{
height: 50px;
padding: 15px 30px;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}

.nav__items__extra{
padding: 7px 30px 7px;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}

.menu .nav__items{
padding-left: 0;
margin-top: 20px;
margin-bottom: 20px;
}

.menu ul{
list-style: none;
}

.nav__items li{
height: 44px;
}

.menu h4 a{
text-decoration: none;
}

.nav__items a{
text-decoration: none;
font-weight: 500;
}

/************************************
COLORING IN THE NAVBAR
************************************/

.navbar__default {
background: #f4f4f4;
}

.navbar__white {
background: #fff;
}

.navbar__black {
background: #000;
color: #fff;
}

.navbar__header__green{
color: #28B463;
}

.navbar__header__green:hover{
color: #28B463;
}


this is the html code for the expanding navbar.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>bootstrap homepage</title>
<link href="https://fonts.googleapis.com/css?family=Droid+Sans|Droid+Serif|Noto+Sans" rel="stylesheet">
<script src="https://use.fontawesome.com/ebcec35828.js"></script>
<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/style.css">
</head>

<body class="">

<!--- This is for the header content -
------------------------------------->
<header class="container-fluid main__header color__white">

<div class="header__content__left">
<a href="#" class="menu-toggle" id="nav-expander"><i class="fa fa-bars header__margin__right" style="font-size:20px;"></i></a>
<div class="color__logo__default">
<a href="#"></a>
<a href="#" class="desktop__responsive__logo"><h4>spaces</h4></a>
</div>
</div>

<div class="header__content__right">
<a href="#" class="fa fa-search"></a>
<a href="#" class="header__margin__left">Log In</a><i class="pl-1"></i>
<a href="#" class="btn btn-post-green text-white header__margin__left">Post</a>
</div>

</header>
<!--- Ending of the header content -
----------------------------------->

<!--- Slide-out navigation - - - - -
----------------------------------->
<nav class="menu navbar__white">
<a href="#" class="close"> <i class="fa fa-close pt-1 pl-2 pr-2 pb-2"></i></a>
<h4><a href="" class="navbar__header navbar__header__green">spaces</a></h4><hr style="margin-top:0px;">
<ul class="nav__items">
<li class="nav__li__style"><a href="#" class="nav__items__extra"> Explore</a></li>



<hr>
<li class="nav__li__style"><a href="#" class="nav__items__extra">About</a></li>
<li class="nav__li__style"><a href="#" class="nav__items__extra">Guidelines</a></li>
<li class="nav__li__style"><a href="#" class="nav__items__extra">Help and Support</a></li>
<li class="nav__li__style"><a href="#" class="nav__items__extra">Contact Us</a></li>
</ul>
</nav>
<!--- Ending of navigation - - - - -
----------------------------------->


<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</body>
</html>

最佳答案

是这样的吗?

你必须添加一个overlay div,固定位置覆盖整个屏幕,接下来你必须将它的z-index设置为低于你的导航面板但高于 css 中的所有其他元素,在您的情况下设置 z-index: 99 效果很好。

最后,由于您使用的是 jquery,您可以将它与导航栏一起show()hide()

'use strict';

// Open offsite navigation.
$('#nav-expander').on('click', function(e) {
e.preventDefault();
$('nav').toggleClass('nav-expanded');
$('.overlay').show();
});

// Close offsite navigation.
$('.menu .close').on('click', function(e) {
e.preventDefault();
$('nav').toggleClass('nav-expanded');
$('.overlay').hide();
});

// Close offsite navigation after user click on an link in navigation.
$('.menu a').on('click', function(e) {
//se.preventDefault();
$('nav').removeClass('nav-expanded');
$('.overlay').hide();
});

$('.body').on('click', function(e) {
//e.preventDefault();
$('nav').removeClass('nav-expanded');
$('.overlay').hide();
});

$('.body2').on('click', function(e) {
//e.preventDefault();
$('nav').removeClass('nav-expanded');
$('.overlay').hide();
});

$('.btn').on('click', function(e) {
//e.preventDefault();
$('nav').removeClass('nav-expanded');
$('.overlay').hide();
});

//ending of offsite navigation
/************************************
*************************************
*************************************
GENERAL STYLING
*************************************
*************************************
************************************/

body{
background-color: #F2F3F4;
}

.overlay{
position: fixed;
display: none;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
background-color: rgba(0,0,0,0.8);
z-index: 99;
}

/************************************
GENERAL STYLING ENDING
************************************/

/************************************
*************************************
*************************************
HEADER STYLING
*************************************
*************************************
************************************/

header{
height: 57px;
border-bottom: 1px #DDDDDD solid;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}

.main__header{
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-between;
align-items: center;
flex-grow: 1;
}

.header__content__left{
display: flex;
flex-direction: row;
flex-grow: 1;
align-items: center;
}

.header__content__right{
display: inline-flex;
flex-direction: row;
flex-grow: 1;
justify-content: flex-end;
align-items: center;
}

.header__content__right a{
font-weight: 600;
}

.header__margin__right{
margin-right: .5rem;
}

.header__margin__left{
margin-left: 1rem;
}

/************************************
*************************************
*************************************
SLIDE-OUT NAVIGATION STYLING
*************************************
*************************************
************************************/

.menu{
position: relative;
width: 280px;
display: block;
height: 100%;
top: 0;
left:-300px; /*was originally t right when the nav bar was on the right side*/
position: fixed;
z-index: 100;
text-align: center;
transition: left 0.1s; /** default on the right **/
overflow-y: auto; /* makes the expanding nav scrollable */
}

.menu.nav-expanded{
left: 0; /* was at right before, for nav bar to expand from left */
}

.menu .close{
font-size: 30px;
margin-right: 10px;
margin-top:10px;
}

.navbar__header{
height: 50px;
padding: 15px 30px;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}

.nav__items__extra{
padding: 7px 30px 7px;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}

.menu .nav__items{
padding-left: 0;
margin-top: 20px;
margin-bottom: 20px;
}

.menu ul{
list-style: none;
}

.nav__items li{
height: 44px;
}

.menu h4 a{
text-decoration: none;
}

.nav__items a{
text-decoration: none;
font-weight: 500;
}

/************************************
COLORING IN THE NAVBAR
************************************/

.navbar__default {
background: #f4f4f4;
}

.navbar__white {
background: #fff;
}

.navbar__black {
background: #000;
color: #fff;
}

.navbar__header__green{
color: #28B463;
}

.navbar__header__green:hover{
color: #28B463;
}


this is the html code for the expanding navbar.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>bootstrap homepage</title>
<link href="https://fonts.googleapis.com/css?family=Droid+Sans|Droid+Serif|Noto+Sans" rel="stylesheet">
<script src="https://use.fontawesome.com/ebcec35828.js"></script>
<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/style.css">
</head>

<body class="">
<div class="overlay">
</div>
<!--- This is for the header content -
------------------------------------->
<header class="container-fluid main__header color__white">

<div class="header__content__left">
<a href="#" class="menu-toggle" id="nav-expander"><i class="fa fa-bars header__margin__right" style="font-size:20px;"></i></a>
<div class="color__logo__default">
<a href="#"></a>
<a href="#" class="desktop__responsive__logo"><h4>spaces</h4></a>
</div>
</div>

<div class="header__content__right">
<a href="#" class="fa fa-search"></a>
<a href="#" class="header__margin__left">Log In</a><i class="pl-1"></i>
<a href="#" class="btn btn-post-green text-white header__margin__left">Post</a>
</div>

</header>
<!--- Ending of the header content -
----------------------------------->

<!--- Slide-out navigation - - - - -
----------------------------------->
<nav class="menu navbar__white">
<a href="#" class="close"> <i class="fa fa-close pt-1 pl-2 pr-2 pb-2"></i></a>
<h4><a href="" class="navbar__header navbar__header__green">spaces</a></h4><hr style="margin-top:0px;">
<ul class="nav__items">
<li class="nav__li__style"><a href="#" class="nav__items__extra"> Explore</a></li>



<hr>
<li class="nav__li__style"><a href="#" class="nav__items__extra">About</a></li>
<li class="nav__li__style"><a href="#" class="nav__items__extra">Guidelines</a></li>
<li class="nav__li__style"><a href="#" class="nav__items__extra">Help and Support</a></li>
<li class="nav__li__style"><a href="#" class="nav__items__extra">Contact Us</a></li>
</ul>
</nav>
<!--- Ending of navigation - - - - -
----------------------------------->


<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</body>
</html>

关于javascript - 当我打开滑出式菜单时覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46082016/

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