html-使用CSS滚动时,使导航栏保持顶部
我正在尝试使导航栏与页面一起移动,但是如果用户向下滚动,则导航栏会停留在顶部。 谁能提供任何示例或如何提供? 非常感激。 (我用其他任何语言都没有希望)。 我尝试使用css sticky,但是没有用。
/* www..com
Blinx Service
Created by Pierre Chedraoui
(c) Copyright 2015
*/
/* BODY */
body {
margin: 0px;
background-color: #000000;
height: 2000px;
}
/* 1. HEADER */
.headercss {
width: auto;
height: 320px;
background-color: #000000;
position: relative;
}
.headerlogo {
width: auto;
height: 250px;
background-color: #272727;
position: relative;
}
.nav {
width: auto;
height: 70px;
background-color: #272727;
position: relative;
overflow: hidden;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
float:left;
width:100%;
overflow: hidden;
}
li {
float: left;
width:25%;
min-width: 243px;
overflow: hidden;
}
a:link, a:visited {
display: block;
height: 68px;
min-width: 243px;
font-size: 12px;
color: #FFFFFF;
border-right: 1px solid #000000;
border-top: 1px solid #000000;
background-color: #272727;
text-align: center;
text-decoration: none;
font-family: 'Raleway', Arial;
letter-spacing: 2pt;
line-height: 200%;
overflow: hidden;
}
a:hover, a:active {
background-color: #242424;
}
11个解决方案
47 votes
and more stuff to continue scrolling here
put what you want here
just adjust javascript size to match this window
and more stuff to continue scrolling here
put what you want here
just adjust javascript size to match this window
and more stuff to continue scrolling here
Yash Thakur answered 2020-07-24T19:22:02Z
15 votes
添加到您的.nav css块
position: fixed
它会工作
TheLovelySausage answered 2020-07-24T19:22:26Z
12 votes
我希望这可以帮助某人。 通过js确定导航偏移量,然后将粘性位置css应用于导航:
但是首先,我们将像这样在样式表中定义样式。
.sticky {
position: fixed;
width: 100%;
left: 0;
top: 0;
z-index: 100;
border-top: 0;
}
然后,我们将使用jQuery有条件地将该类应用于导航。
$(document).ready(function() {
var stickyNavTop = $('.nav').offset().top;
var stickyNav = function(){
var scrollTop = $(window).scrollTop();
if (scrollTop > stickyNavTop) {
$('.nav').addClass('sticky');
} else {
$('.nav').removeClass('sticky');
}
};
stickyNav();
$(window).scroll(function() {
stickyNav();
});
});
Amar Pratap answered 2020-07-24T19:22:55Z
5 votes
只需使用最高喜欢的答案中所述的z-index CSS属性,导航栏将停留在顶部。
例: