我的问题是当我尝试修复导航栏时,它只是从我的页面中消失了。我尝试的第一件事是创建一个名为 sticky 的类,它具有 position:fixed 但当我调用它时,我的导航栏停止显示(我认为它缩小到可以看到文本的程度)。任何想法如何在用户向下滚动时使导航栏保持静态?
HTML 代码
</head>
<body>
<!-- Navbar -->
<div class ="main">
<div class="navbar ">
<div class="container flex lead ">
<nav>
<ul>
<li><a href="#">Acasa</a></li>
<li><a href="features.html">Proiecte</a></li>
<li><a href="docs.html">Recenzii</a></li>
</ul>
</nav>
<div>
<h1>CarolHousing</h1>
</div>
<div class="social sm">
<a href="#"><i class="fab fa-facebook fa-2x"></i></a>
<a href="#"><i class="fab fa-instagram fa-2x"></i></a>
<a href="#">Contact</a>
</div>
</div>
</div>
</div>
<!-- Showcase -->
<section class="showcase">
<div class="container grid">
<div class="showcase-text">
<h1>Easier Deployment</h1>
<p>Deploy web apps of all kinds, from large scale enterprise APIs to static websites for individuals. Fill out the form to try a demo of our platform</p>
<a href="features.html" class="btn btn-outline">Read More</a>
</div>
<div class="showcase-form card">
<h2>Request a Demo</h2>
<form>
<div class="form-control">
<input type="text" name="name" placeholder="Name" required>
</div>
<div class="form-control">
<input type="text" name="company" placeholder="Company Name" required>
</div>
<div class="form-control">
<input type="email" name="email" placeholder="Email" required>
</div>
<input type="submit" value="Send" class="btn btn-primary">
</form>
</div>
</div>
</section>
<!-- Cli -->
<section class="cli">
<div class="container grid">
<img src="images/cli.png" alt="">
<div class="card">
<h3>Easy to use, cross platform CLI</h3>
</div>
<div class="card">
<h3>Deploy in seconds</h3>
</div>
</div>
</section>
<!-- Footer -->
<footer class="footer bg-dark py-5">
<div class="container grid grid-3">
<div>
<h1>Loruki
</h1>
<p>Copyright © 2020</p>
</div>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="features.html">Features</a></li>
<li><a href="docs.html">Docs</a></li>
</ul>
</nav>
<div class="social">
<a href="#"><i class="fab fa-facebook fa-2x"></i></a>
<a href="#"><i class="fab fa-instagram fa-2x"></i></a>
</div>
</div>
</footer>
</body>
</html>
CSS 代码:
.navbar {
background-color: var(--primary-color);
color: #fff;
min-height: 80px;
position: sticky;
top: 0;
}
.navbar ul {
display: flex;
flex-direction: row;
flex-grow:1;
}
.navbar a {
color: #fff;
padding: 10px;
margin: 0 2px;
}
.navbar a:hover {
border-bottom: 2px #fff solid;
}
.navbar .flex {
justify-content:space-between;
}
.navbar .social a{
margin: 0 2px;
flex-grow:1;
}
.navbar .social > *:nth-child(3){
font-size: 28px;
}.showcase {
height: 400px;
background-color: var(--primary-color);
color: #fff;
position: relative;
}
.showcase h1 {
font-size: 40px;
}
.showcase p {
margin: 20px 0;
}
.showcase .grid {
overflow: visible;
grid-template-columns: 55% auto;
gap: 30px;
}
.showcase-text {
animation: slideInFromLeft 1s ease-in;
}
.showcase-form {
position: relative;
top: 60px;
height: 350px;
width: 400px;
padding: 40px;
z-index: 100;
justify-self: flex-end;
animation: slideInFromRight 1s ease-in;
}
.showcase-form .form-control {
margin: 30px 0;
}
.showcase-form input[type='text'],
.showcase-form input[type='email'] {
border: 0;
border-bottom: 1px solid #b4becb;
width: 100%;
padding: 3px;
font-size: 16px;
}
.showcase-form input:focus {
outline: none;
}
.showcase::before,
.showcase::after {
content: '';
position: absolute;
height: 100px;
bottom: -70px;
right: 0;
left: 0;
background: #fff;
transform: skewY(-3deg);
-webkit-transform: skewY(-3deg);
-moz-transform: skewY(-3deg);
-ms-transform: skewY(-3deg);
}
@import url('https://fonts.googleapis.com/css2?family=Lato:wght@300&display=swap');
:root {
--primary-color: #047aed;
--secondary-color: #1c3fa8;
--dark-color: #002240;
--light-color: #f4f4f4;
--success-color: #5cb85c;
--error-color: #d9534f;
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
font-family: 'Lato', sans-serif;
color: #333;
line-height: 1.6;
}
ul {
list-style-type: none;
}
a {
text-decoration: none;
color: #333;
}
h1,
h2 {
font-weight: 300;
line-height: 1.2;
margin: 10px 0;
}
p {
margin: 10px 0;
}
img {
width: 100%;
}
code,
pre {
background: #333;
color: #fff;
padding: 10px;
}
.hidden {
visibility: hidden;
height: 0;
}
/* Features */
.features-head img,
.docs-head img {
width: 200px;
justify-self: flex-end;
}
.features-sub-head img {
width: 300px;
justify-self: flex-end;
}
.features-main .card > i {
margin-right: 20px;
}
.features-main .grid {
padding: 30px;
}
.features-main .grid > *:first-child {
grid-column: 1 / span 3;
}
.features-main .grid > *:nth-child(2) {
grid-column: 1 / span 2;
}
/* Docs */
.docs-main h3 {
margin: 20px 0;
}
.docs-main .grid {
grid-template-columns: 1fr 2fr;
align-items: flex-start;
}
.docs-main nav li {
font-size: 17px;
padding-bottom: 5px;
margin-bottom: 5px;
border-bottom: 1px #ccc solid;
}
.docs-main a:hover {
font-weight: bold;
}
/* Tablets and under */
@media (max-width: 768px) {
.grid,
.showcase .grid,
.stats .grid,
.cli .grid,
.cloud .grid,
.features-main .grid,
.docs-main .grid {
grid-template-columns: 1fr;
grid-template-rows: 1fr;
}
.showcase {
height: auto;
}
.showcase-text {
text-align: center;
margin-top: 40px;
animation: slideInFromTop 1s ease-in;
}
.showcase-form {
justify-self: center;
margin: auto;
animation: slideInFromBottom 1s ease-in;
}
.cli .grid > *:first-child {
grid-column: 1;
grid-row: 1;
}
.features-head,
.features-sub-head,
.docs-head {
text-align: center;
}
.features-head img,
.features-sub-head img,
.docs-head img {
justify-self: center;
}
.features-main .grid > *:first-child,
.features-main .grid > *:nth-child(2) {
grid-column: 1;
}
}
/* Mobile */
@media (max-width: 500px) {
.navbar {
height: 110px;
}
.navbar .flex {
flex-direction: column;
}
.navbar ul {
padding: 10px;
background-color: rgba(0, 0, 0, 0.1);
}
.showcase-form {
width: 300px;
}
}.container {
max-width: 1400px;
margin: 0 auto;
overflow: auto;
padding: 0 40px;
align-self: start;
}
.main {
min-height: 100vh; /*add*/
height: 3000px; /*this is a test rule*/
}
.card {
background-color: #fff;
color: #333;
border-radius: 10px;
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
padding: 20px;
margin: 10px;
}
.btn {
display: inline-block;
padding: 10px 30px;
cursor: pointer;
background: var(--primary-color);
color: #fff;
border: none;
border-radius: 5px;
}
.btn-outline {
background-color: transparent;
border: 1px #fff solid;
}
.btn:hover {
transform: scale(0.98);
}
/* Backgrounds & colored buttons */
.bg-primary,
.btn-primary {
background-color: var(--primary-color);
color: #fff;
}
.bg-secondary,
.btn-secondary {
background-color: var(--secondary-color);
color: #fff;
}
.bg-dark,
.btn-dark {
background-color: var(--dark-color);
color: #fff;
}
.bg-light,
.btn-light {
background-color: var(--light-color);
color: #333;
}
.bg-primary a,
.btn-primary a,
.bg-secondary a,
.btn-secondary a,
.bg-dark a,
.btn-dark a {
color: #fff;
}
/* Text colors */
.text-primary {
color: var(--primary-color);
}
.text-secondary {
color: var(--secondary-color);
}
.text-dark {
color: var(--dark-color);
}
.text-light {
color: var(--light-color);
}
/* Text sizes */
.lead {
font-size: 20px;
}
.sm {
font-size: 1rem;
}
.md {
font-size: 2rem;
}
.lg {
font-size: 3rem;
}
.xl {
font-size: 4rem;
}
.text-center {
text-align: center;
}
/* Alert */
.alert {
background-color: var(--light-color);
padding: 10px 20px;
font-weight: bold;
margin: 15px 0;
}
.alert i {
margin-right: 10px;
}
.alert-success {
background-color: var(--success-color);
color: #fff;
}
.alert-error {
background-color: var(--error-color);
color: #fff;
}
.flex {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
justify-content: center;
align-items: center;
height: 100%;
}
.grid-3 {
grid-template-columns: repeat(3, 1fr);
}
.sticky{
position: fixed;
top: 0;
width: 100%;
}
.cli .grid {
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 1fr);
}
.cli .grid > *:first-child {
grid-column: 1 / span 2;
grid-row: 1 / span 2;
}
答案 0 :(得分:1)
我的回答是建议。 制定一个规则,将所有内容包装在 main div 中,如下所示:
<div class="main">
...
</div>
在我的示例中,我使用了 position: sticky
。
在 css 中,我标记了编辑。
anime.timeline({loop: true})
.add({
targets: '.ml5 .line',
opacity: [0.5,1],
scaleX: [0, 1],
easing: "easeInOutExpo",
duration: 700
}).add({
targets: '.ml5 .line',
duration: 600,
easing: "easeOutExpo",
translateY: (el, i) => (-0.625 + 0.625*2*i) + "em"
}).add({
targets: '.ml5 .ampersand',
opacity: [0,1],
scaleY: [0.5, 1],
easing: "easeOutExpo",
duration: 600,
offset: '-=600'
}).add({
targets: '.ml5 .letters-left',
opacity: [0,1],
translateX: ["0.5em", 0],
easing: "easeOutExpo",
duration: 600,
offset: '-=300'
}).add({
targets: '.ml5 .letters-right',
opacity: [0,1],
translateX: ["-0.5em", 0],
easing: "easeOutExpo",
duration: 600,
offset: '-=600'
}).add({
targets: '.ml5',
opacity: 0,
duration: 1000,
easing: "easeOutExpo",
delay: 1000
});
body {
box-sizing: border-box; /*add*/
margin: 0; /*add*/
padding: 0; /*add*/
}
.navbar {
background-color: var(--primary-color);
color: #fff;
min-height: 80px;
position: sticky;
top: 0;
z-index: 9999;
}
.navbar ul {
display: flex;
flex-direction: row;
flex-grow:1;
}
.navbar a {
color: #fff;
padding: 10px;
margin: 0 2px;
}
.navbar a:hover {
border-bottom: 2px #fff solid;
}
.navbar .flex {
justify-content:space-between;
}
.navbar .social a{
margin: 0 2px;
flex-grow:1;
}
.navbar .social > *:nth-child(3){
font-size: 28px;
}.showcase {
height: 400px;
background-color: var(--primary-color);
color: #fff;
position: relative;
}
.showcase h1 {
font-size: 40px;
}
.showcase p {
margin: 20px 0;
}
.showcase .grid {
overflow: visible;
grid-template-columns: 55% auto;
gap: 30px;
}
.showcase-text {
animation: slideInFromLeft 1s ease-in;
}
.showcase-form {
position: relative;
top: 60px;
height: 350px;
width: 400px;
padding: 40px;
z-index: 100;
justify-self: flex-end;
animation: slideInFromRight 1s ease-in;
}
.showcase-form .form-control {
margin: 30px 0;
}
.showcase-form input[type='text'],
.showcase-form input[type='email'] {
border: 0;
border-bottom: 1px solid #b4becb;
width: 100%;
padding: 3px;
font-size: 16px;
}
.showcase-form input:focus {
outline: none;
}
.showcase::before,
.showcase::after {
content: '';
position: absolute;
height: 100px;
bottom: -70px;
right: 0;
left: 0;
background: #fff;
transform: skewY(-3deg);
-webkit-transform: skewY(-3deg);
-moz-transform: skewY(-3deg);
-ms-transform: skewY(-3deg);
}
@import url('https://fonts.googleapis.com/css2?family=Lato:wght@300&display=swap');
:root {
--primary-color: #047aed;
--secondary-color: #1c3fa8;
--dark-color: #002240;
--light-color: #f4f4f4;
--success-color: #5cb85c;
--error-color: #d9534f;
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
font-family: 'Lato', sans-serif;
color: #333;
line-height: 1.6;
}
ul {
list-style-type: none;
}
a {
text-decoration: none;
color: #333;
}
h1,
h2 {
font-weight: 300;
line-height: 1.2;
margin: 10px 0;
}
p {
margin: 10px 0;
}
img {
width: 100%;
}
code,
pre {
background: #333;
color: #fff;
padding: 10px;
}
.hidden {
visibility: hidden;
height: 0;
}
/* Features */
.features-head img,
.docs-head img {
width: 200px;
justify-self: flex-end;
}
.features-sub-head img {
width: 300px;
justify-self: flex-end;
}
.features-main .card > i {
margin-right: 20px;
}
.features-main .grid {
padding: 30px;
}
.features-main .grid > *:first-child {
grid-column: 1 / span 3;
}
.features-main .grid > *:nth-child(2) {
grid-column: 1 / span 2;
}
/* Docs */
.docs-main h3 {
margin: 20px 0;
}
.docs-main .grid {
grid-template-columns: 1fr 2fr;
align-items: flex-start;
}
.docs-main nav li {
font-size: 17px;
padding-bottom: 5px;
margin-bottom: 5px;
border-bottom: 1px #ccc solid;
}
.docs-main a:hover {
font-weight: bold;
}
/* Tablets and under */
@media (max-width: 768px) {
.grid,
.showcase .grid,
.stats .grid,
.cli .grid,
.cloud .grid,
.features-main .grid,
.docs-main .grid {
grid-template-columns: 1fr;
grid-template-rows: 1fr;
}
.showcase {
height: auto;
}
.showcase-text {
text-align: center;
margin-top: 40px;
animation: slideInFromTop 1s ease-in;
}
.showcase-form {
justify-self: center;
margin: auto;
animation: slideInFromBottom 1s ease-in;
}
.cli .grid > *:first-child {
grid-column: 1;
grid-row: 1;
}
.features-head,
.features-sub-head,
.docs-head {
text-align: center;
}
.features-head img,
.features-sub-head img,
.docs-head img {
justify-self: center;
}
.features-main .grid > *:first-child,
.features-main .grid > *:nth-child(2) {
grid-column: 1;
}
}
/* Mobile */
@media (max-width: 500px) {
.navbar {
height: 110px;
}
.navbar .flex {
flex-direction: column;
}
.navbar ul {
padding: 10px;
background-color: rgba(0, 0, 0, 0.1);
}
.showcase-form {
width: 300px;
}
}.container {
max-width: 1400px;
margin: 0 auto;
overflow: auto;
padding: 0 40px;
align-self: start;
}
.main {
min-height: 100vh; /*add*/
}
.card {
background-color: #fff;
color: #333;
border-radius: 10px;
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
padding: 20px;
margin: 10px;
}
.btn {
display: inline-block;
padding: 10px 30px;
cursor: pointer;
background: var(--primary-color);
color: #fff;
border: none;
border-radius: 5px;
}
.btn-outline {
background-color: transparent;
border: 1px #fff solid;
}
.btn:hover {
transform: scale(0.98);
}
/* Backgrounds & colored buttons */
.bg-primary,
.btn-primary {
background-color: var(--primary-color);
color: #fff;
}
.bg-secondary,
.btn-secondary {
background-color: var(--secondary-color);
color: #fff;
}
.bg-dark,
.btn-dark {
background-color: var(--dark-color);
color: #fff;
}
.bg-light,
.btn-light {
background-color: var(--light-color);
color: #333;
}
.bg-primary a,
.btn-primary a,
.bg-secondary a,
.btn-secondary a,
.bg-dark a,
.btn-dark a {
color: #fff;
}
/* Text colors */
.text-primary {
color: var(--primary-color);
}
.text-secondary {
color: var(--secondary-color);
}
.text-dark {
color: var(--dark-color);
}
.text-light {
color: var(--light-color);
}
/* Text sizes */
.lead {
font-size: 20px;
}
.sm {
font-size: 1rem;
}
.md {
font-size: 2rem;
}
.lg {
font-size: 3rem;
}
.xl {
font-size: 4rem;
}
.text-center {
text-align: center;
}
/* Alert */
.alert {
background-color: var(--light-color);
padding: 10px 20px;
font-weight: bold;
margin: 15px 0;
}
.alert i {
margin-right: 10px;
}
.alert-success {
background-color: var(--success-color);
color: #fff;
}
.alert-error {
background-color: var(--error-color);
color: #fff;
}
.flex {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
justify-content: center;
align-items: center;
height: 100%;
}
.grid-3 {
grid-template-columns: repeat(3, 1fr);
}
.sticky{
position: fixed;
top: 0;
width: 100%;
}
.cli .grid {
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 1fr);
}
.cli .grid > *:first-child {
grid-column: 1 / span 2;
grid-row: 1 / span 2;
}
/*Animation h1*/
/*.ml5 {
position: relative;
font-weight: 300;
font-size: 4.5em;
color: #402d2d;
}*/
.ml5 .text-wrapper {
position: relative;
display: inline-block;
padding-top: 0.1em;
padding-right: 0.05em;
padding-bottom: 0.15em;
line-height: 1em;
}
.ml5 .line {
position: absolute;
left: 0;
top: 0;
bottom: 0;
margin: auto;
height: 3px;
width: 100%;
background-color: #FFFFFF;
transform-origin: 0.5 0;
}
.ml5 .ampersand {
font-family: Baskerville, serif;
font-style: italic;
font-weight: 400;
width: 1em;
margin-right: -0.1em;
margin-left: -0.1em;
}
.ml5 .letters {
display: inline-block;
opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/2.0.2/anime.min.js"></script>
<div class="main">
<div class="navbar ">
<div class="container flex lead ">
<nav>
<ul>
<li><a href="#">Acasa</a></li>
<li><a href="features.html">Proiecte</a></li>
<li><a href="docs.html">Recenzii</a></li>
</ul>
</nav>
<div>
<h1 class="ml5">
<span class="text-wrapper">
<span class="line line1"></span>
<span class="letters letters-left">Carol</span>
<span class="letters ampersand">&</span>
<span class="letters letters-right">Housing</span>
<span class="line line2"></span>
</span>
</h1>
</div>
<div class="social sm">
<a href="#"><i class="fab fa-facebook fa-2x"></i></a>
<a href="#"><i class="fab fa-instagram fa-2x"></i></a>
<a href="#">Contact</a>
</div>
</div>
</div>
<section class="showcase">
<div class="container grid">
<div class="showcase-text">
<h1>Easier Deployment</h1>
<p>Deploy web apps of all kinds, from large scale enterprise APIs to static websites for individuals. Fill out the form to try a demo of our platform</p>
<a href="features.html" class="btn btn-outline">Read More</a>
</div>
<div class="showcase-form card">
<h2>Request a Demo</h2>
<form>
<div class="form-control">
<input type="text" name="name" placeholder="Name" required>
</div>
<div class="form-control">
<input type="text" name="company" placeholder="Company Name" required>
</div>
<div class="form-control">
<input type="email" name="email" placeholder="Email" required>
</div>
<input type="submit" value="Send" class="btn btn-primary">
</form>
</div>
</div>
</section>
<section class="cli">
<div class="container grid">
<img src="images/cli.png" alt="">
<div class="card">
<h3>Easy to use, cross platform CLI</h3>
</div>
<div class="card">
<h3>Deploy in seconds</h3>
</div>
</div>
</section>
<!-- Footer -->
<footer class="footer bg-dark py-5">
<div class="container grid grid-3">
<div>
<h1>Loruki
</h1>
<p>Copyright © 2020</p>
</div>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="features.html">Features</a></li>
<li><a href="docs.html">Docs</a></li>
</ul>
</nav>
<div class="social">
<a href="#"><i class="fab fa-facebook fa-2x"></i></a>
<a href="#"><i class="fab fa-instagram fa-2x"></i></a>
</div>
</div>
</footer>
</div>
答案 1 :(得分:0)
将容器的高度(必须是导航栏父级)设置为 100vh
并保持 overflow: auto
或设置为 overflow-y: scroll
。
然后将导航栏上的位置属性设置为“粘性”:
position: sticky
享受吧! :)
答案 2 :(得分:0)
尝试对导航容器执行此操作。
position: fixed;
这是一个代码示例。
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>A Great Demo on CodePen</title>
</head>
<body>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Works</li>
<li>Contact</li>
</ul>
</nav>
<div></div>
<div></div>
</body>
</html>
body {
margin: 0;
}
nav ul {
width: 100%;
margin: 0;
position: fixed;
display: flex;
background-color: green;
}
ul li {
list-style: none;
margin: 20px;
}
div {
height: 500px;
width: 500px;
background-color: red;
}