为什么我的 Ajax 响应显示在我的导航栏上方?
Why does my Ajax response display over my navigation bar?
每当我滚动鼠标时,所有列出的管理卡都会出现在导航栏上。这是一个非常恼人的缺陷,我似乎无法理解我哪里出错了。任何帮助,将不胜感激。谢谢!
#mainsec {
height: 100vh;
width: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
#navv {
background-color: #333; /* Black background color */
position: fixed; /* Make it stick/fixed */
top: 0; /* Stay on top */
width: 100%; /* Full width */
transition: top 0.3s; /* Transition effect when sliding down (and up) */
}
#navv a:hover {
background-color: rgba(221, 221, 221, 0.568);
color: black;
}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous" />
<link rel="stylesheet" type="text/css" href="../../css/admin_dash.css">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark" id="navv">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="../../assets/logo.png" alt="" width="30" height="24" class="d-inline-block align-text-top"> Phemesoft
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link active" aria-current="page" href="#sec1">Admins</a>
<a class="nav-link" href="#sec2">Manage Admins</a>
<a class="nav-link" href="#sec3">Course Content</a>
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</div>
</div>
</div>
</nav>
<div class="container">
<div class="column">
<div id="sec1">
<div class="row" id="mainsec">
<div class="col-sm-6">
<h2>Admins</h2>
</div>
<div class="col-sm-6">
<div class="row">
<div class="col-sm-5 cards-wrapper"></div>
</div>
</div>
</div>
</div>
<div id="sec2">
<div class="row" id="mainsec">
<div class="col-sm-6">
<h1>User to Admin</h1>
</div>
<div class="col-sm-6">
<form class="form" action="makeadmin" method="POST">
Enter email:
<input type="email" placeholder="Email" name="email">
<button type="submit" class="btn btn-light">Check</button>
</form>
</div>
</div>
</div>
<div id="sec3">
<div class="col-sm-6" id="mainsec">
<h1>Add course content</h1>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$.ajax({
url: "listadmins",
type: "GET",
success: function(response) {
var $results = $(".cards-wrapper");
for (var a = 0; a < response.length; a++) {
// console.log(response[a].Name);
$results.append(
'<div class="card"><div class="card-body"><h6 class="card-title">' +
response[a].Name +
'</h6></div></div>'
);
}
}
});
</script>
<script>
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navv").style.top = "0";
} else {
document.getElementById("navv").style.top = "-50px";
}
prevScrollpos = currentScrollPos;
};
</script>
</body>
</html>
这是问题的图片:
The card appears over then nav bar
Ps:我没有发布 /listadmins 的映射,因为它只是普通的数据库查询
从卡 class
中删除 'position: relative'
这与 ajax 直接无关,但与页面的其余部分无关。
您的html无效
- head 标签未关闭
- 正文标签未打开
- 重复的 id
mainsec
- 没有真正使用它们所以我把它们变成了 class 并删除了 id 的
- 其他一些无效标记
这里很难看出问题所以我稍微调整了这个网站的大小
现在我们已经解决了这个问题,您可以根据需要使用 mt-5 pt-4
调整顶部的页边距和填充,尝试一下这个,看看要移动到哪里。
.mainsec {
height: 5em;
width: 90%;
display: flex;
align-items: center;
justify-content: center;
}
#navv {
background-color: #333;
position: fixed;
top: 0;
width: 100%;
transition: top 0.3s;
}
#navv a:hover {
background-color: rgba(221, 221, 221, 0.568);
color: black;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous" />
<link rel="stylesheet" type="text/css" href="../../css/admin_dash.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark" id="navv">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="../../assets/logo.png" width="30" height="24" class="d-inline-block align-text-top" alt="Phemesoft logo">
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link active" aria-current="page" href="#sec1">Admins</a>
<a class="nav-link" href="#sec2">Manage Admins</a>
<a class="nav-link" href="#sec3">Course Content</a>
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</div>
</div>
</div>
</nav>
<div class="container mt-6">
<div class="">
<div id="sec1">
<div class="mainsec mt-5">
<div class="col-sm-3">
<h2>Admins</h2>
</div>
<div class="col-sm-7 mt-5 pt-4">
<div class="row">
<div class="col-sm-8 cards-wrapper">
<div class="cards-wrapper">
<div class="card">
<div class="card-body">
<h6 class="card-title">Pretend append
</h6>
</div>
</div>
<div class="card">
<div class="card-body">
<h6 class="card-title">Pretend append
</h6>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row" id="sec2">
<div class="row mainsec">
<div class="col-sm-6">
<h1>User to Admin</h1>
</div>
<div class="col-sm-6">
<form class="form" action="makeadmin" method="POST">
Enter email:
<input type="email" placeholder="Email" name="email">
<button type="submit" class="btn btn-light">Check</button>
</form>
</div>
</div>
</div>
<div class="row" id="sec3">
<div class="col-sm-6 mainsec">
<h1>Add course content</h1>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$.ajax({
url: "listadmins",
type: "GET",
success: function(response) {
var $results = $(".cards-wrapper");
for (var a = 0; a < response.length; a++) {
$results.append(
'<div class="card"><div class="card-body"><h6 class="card-title">' +
response[a].Name +
'</h6></div></div>'
);
}
}
});
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navv").style.top = "0";
} else {
document.getElementById("navv").style.top = "-50px";
}
prevScrollpos = currentScrollPos;
};
</script>
</body>
</html>
感谢大家的回复。我在评论中找到了答案。它是为导航栏使用更高的 z-index 值。
#navv {
background-color: #333; /* Black background color */
position: fixed; /* Make it stick/fixed */
top: 0; /* Stay on top */
width: 100%; /* Full width */
transition: top 0.3s; /* Transition effect when sliding down (and up) */
z-index: 2;
}
每当我滚动鼠标时,所有列出的管理卡都会出现在导航栏上。这是一个非常恼人的缺陷,我似乎无法理解我哪里出错了。任何帮助,将不胜感激。谢谢!
#mainsec {
height: 100vh;
width: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
#navv {
background-color: #333; /* Black background color */
position: fixed; /* Make it stick/fixed */
top: 0; /* Stay on top */
width: 100%; /* Full width */
transition: top 0.3s; /* Transition effect when sliding down (and up) */
}
#navv a:hover {
background-color: rgba(221, 221, 221, 0.568);
color: black;
}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous" />
<link rel="stylesheet" type="text/css" href="../../css/admin_dash.css">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark" id="navv">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="../../assets/logo.png" alt="" width="30" height="24" class="d-inline-block align-text-top"> Phemesoft
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link active" aria-current="page" href="#sec1">Admins</a>
<a class="nav-link" href="#sec2">Manage Admins</a>
<a class="nav-link" href="#sec3">Course Content</a>
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</div>
</div>
</div>
</nav>
<div class="container">
<div class="column">
<div id="sec1">
<div class="row" id="mainsec">
<div class="col-sm-6">
<h2>Admins</h2>
</div>
<div class="col-sm-6">
<div class="row">
<div class="col-sm-5 cards-wrapper"></div>
</div>
</div>
</div>
</div>
<div id="sec2">
<div class="row" id="mainsec">
<div class="col-sm-6">
<h1>User to Admin</h1>
</div>
<div class="col-sm-6">
<form class="form" action="makeadmin" method="POST">
Enter email:
<input type="email" placeholder="Email" name="email">
<button type="submit" class="btn btn-light">Check</button>
</form>
</div>
</div>
</div>
<div id="sec3">
<div class="col-sm-6" id="mainsec">
<h1>Add course content</h1>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$.ajax({
url: "listadmins",
type: "GET",
success: function(response) {
var $results = $(".cards-wrapper");
for (var a = 0; a < response.length; a++) {
// console.log(response[a].Name);
$results.append(
'<div class="card"><div class="card-body"><h6 class="card-title">' +
response[a].Name +
'</h6></div></div>'
);
}
}
});
</script>
<script>
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navv").style.top = "0";
} else {
document.getElementById("navv").style.top = "-50px";
}
prevScrollpos = currentScrollPos;
};
</script>
</body>
</html>
这是问题的图片: The card appears over then nav bar
Ps:我没有发布 /listadmins 的映射,因为它只是普通的数据库查询
从卡 class
中删除 'position: relative'这与 ajax 直接无关,但与页面的其余部分无关。
您的html无效
- head 标签未关闭
- 正文标签未打开
- 重复的 id
mainsec
- 没有真正使用它们所以我把它们变成了 class 并删除了 id 的 - 其他一些无效标记
这里很难看出问题所以我稍微调整了这个网站的大小
现在我们已经解决了这个问题,您可以根据需要使用 mt-5 pt-4
调整顶部的页边距和填充,尝试一下这个,看看要移动到哪里。
.mainsec {
height: 5em;
width: 90%;
display: flex;
align-items: center;
justify-content: center;
}
#navv {
background-color: #333;
position: fixed;
top: 0;
width: 100%;
transition: top 0.3s;
}
#navv a:hover {
background-color: rgba(221, 221, 221, 0.568);
color: black;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous" />
<link rel="stylesheet" type="text/css" href="../../css/admin_dash.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark" id="navv">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="../../assets/logo.png" width="30" height="24" class="d-inline-block align-text-top" alt="Phemesoft logo">
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link active" aria-current="page" href="#sec1">Admins</a>
<a class="nav-link" href="#sec2">Manage Admins</a>
<a class="nav-link" href="#sec3">Course Content</a>
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</div>
</div>
</div>
</nav>
<div class="container mt-6">
<div class="">
<div id="sec1">
<div class="mainsec mt-5">
<div class="col-sm-3">
<h2>Admins</h2>
</div>
<div class="col-sm-7 mt-5 pt-4">
<div class="row">
<div class="col-sm-8 cards-wrapper">
<div class="cards-wrapper">
<div class="card">
<div class="card-body">
<h6 class="card-title">Pretend append
</h6>
</div>
</div>
<div class="card">
<div class="card-body">
<h6 class="card-title">Pretend append
</h6>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row" id="sec2">
<div class="row mainsec">
<div class="col-sm-6">
<h1>User to Admin</h1>
</div>
<div class="col-sm-6">
<form class="form" action="makeadmin" method="POST">
Enter email:
<input type="email" placeholder="Email" name="email">
<button type="submit" class="btn btn-light">Check</button>
</form>
</div>
</div>
</div>
<div class="row" id="sec3">
<div class="col-sm-6 mainsec">
<h1>Add course content</h1>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$.ajax({
url: "listadmins",
type: "GET",
success: function(response) {
var $results = $(".cards-wrapper");
for (var a = 0; a < response.length; a++) {
$results.append(
'<div class="card"><div class="card-body"><h6 class="card-title">' +
response[a].Name +
'</h6></div></div>'
);
}
}
});
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navv").style.top = "0";
} else {
document.getElementById("navv").style.top = "-50px";
}
prevScrollpos = currentScrollPos;
};
</script>
</body>
</html>
感谢大家的回复。我在评论中找到了答案。它是为导航栏使用更高的 z-index 值。
#navv {
background-color: #333; /* Black background color */
position: fixed; /* Make it stick/fixed */
top: 0; /* Stay on top */
width: 100%; /* Full width */
transition: top 0.3s; /* Transition effect when sliding down (and up) */
z-index: 2;
}