```html
/* 基本样式重置 */
* {
margin: 0;
padding: 0;
boxsizing: borderbox;
}
body {
fontfamily: Arial, sansserif;
backgroundcolor: f2f2f2; /* 设置背景色 */
}
.container {
width: 100%;
maxwidth: 1200px; /* 设置最大宽度 */
margin: 0 auto;
padding: 0 20px;
}
.navbar {
backgroundcolor: rgba(255, 255, 255, 0.8); /* 设置导航栏背景色,使用rgba来设置透明度 */
padding: 20px 0; /* 设置上下内边距 */
position: fixed;
top: 0;
left: 0;
width: 100%;
zindex: 1000; /* 设置层级,确保导航栏在最顶层 */
transition: backgroundcolor 0.3s ease; /* 添加过渡效果 */
}
.navbar ul {
liststyle: none;
textalign: center;
}
.navbar ul li {
display: inlineblock;
margin: 0 15px; /* 设置左右外边距 */
}
.navbar ul li a {
textdecoration: none;
color: 333; /* 设置链接文字颜色 */
fontsize: 18px;
transition: color 0.3s ease; /* 添加过渡效果 */
}
.navbar ul li a:hover {
color: 666; /* 鼠标悬停时改变链接文字颜色 */
}
/* 添加阴影效果,提升导航栏的可读性 */
.navbar.shadow {
boxshadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
/* 当滚动页面时,导航栏变为不透明 */
.navbar.scrolled {
backgroundcolor: ffffff; /* 更改为不透明的背景色 */
}
在这里,您可以找到我们提供的各种优质服务。
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla nec enim eu justo tincidunt feugiat. Morbi nec facilisis ipsum. Pellentesque consectetur diam vitae nibh lobortis, sit amet blandit magna ultricies. In id elit rhoncus, finibus quam eget, tincidunt quam. Suspendisse at justo et velit luctus pulvinar.
Sed aliquam, eros sit amet viverra tristique, nisi ipsum tincidunt nisl, ut vulputate nunc dui id felis. Curabitur elementum tellus nec nulla ultricies, eget molestie metus ultrices. Nam aliquet, sapien vel finibus faucibus, quam purus sodales elit, nec rhoncus neque lorem et neque. Nam id aliquet ligula.
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Proin euismod neque et urna aliquet aliquet. Nam quis tristique enim. Suspendisse potenti. Sed nec leo nisl.
Donec mattis sit amet libero non fermentum. Nulla facilisi. Duis sed lorem ac magna aliquet gravida. Nam pharetra faucibus felis, non tristique enim rutrum sed. Phasellus efficitur pharetra mi, eget vehicula dui rhoncus nec. Integer volutpat laoreet fermentum. Phasellus consequat dolor et mi aliquet posuere.
Quisque euismod tellus vel feugiat fringilla. Nam in interdum tortor. Aenean semper, mi vel egestas dignissim, odio leo viverra ex, id volutpat enim turpis vel enim. Curabitur at consequat risus. Duis ultrices eros ut mi convallis, in placerat ex ullamcorper. Pellentesque quis augue ut mi congue varius et eget felis.
// 获取导航栏
var navbar = document.getElementById("navbar");
// 获取导航栏的初始位置
var navbarOffset = navbar.offsetTop;
// 监听滚动事件
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
// 当滚动位置大于或等于导航栏的初始位置时,给导航栏添加一个类名
if (window.pageYOffset >= navbarOffset) {
navbar.classList.add("scrolled");
} else {
navbar.classList.remove("scrolled");
}
}
// 监听滚动事件以添加阴影效果
window.addEventListener("scroll", function() {
if (window.scrollY > 0) {
navbar.classList.add("shadow");
} else {
navbar.classList.remove("shadow");
}
});