div+css实现粘性头部导航条效果代码
代码语言:html
所属分类:菜单导航
代码描述:div+css实现粘性头部导航条效果代码,向下滚动试试。
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/reset.min.css"> <style> :root { --color-primary: purple; accent-color: var(--color-primary); } *, *::before, *::after { box-sizing: border-box; } body { font-family: Inter, Roboto, "Helvetica Neue", "Arial Nova", "Nimbus Sans", Arial, sans-serif; height: 100vh; padding-top: var(--_header-h, 4rem); } img { display: block; max-width: 100%; -o-object-fit: cover; object-fit: cover; } h1 { -webkit-margin-after: 1rem; margin-block-end: 1rem; font-size: 3rem; } h2 { font-size: 2rem; } a { color: var(--color-primary); text-decoration: none; } a:hover { text-decoration: underline; } .wrapper { display: grid; place-content: center; height: 100vh; } .header { padding: 2rem; position: fixed; inset: 0 0 auto 0; transition: color 0.3s; z-index: 10; color: #fff; } .header::before { content: ""; position: absolute; inset: 0; background-color: #fff; transform: scaleY(0); transition: transform 0.3s; transform-origin: top center; will-change: transform; } .header.is-sticky { color: #000; } .header.is-sticky::before { transform: scaleY(1); } .header__inner { position: relative; display: flex; align-items: center; justify-content: space-between; } .nav ul { display: flex; align-items: center; gap: 1rem; } .nav ul li a { color: inherit; } .content { padding-block: 2rem; width: min(100% - 2rem, 70ch); margin-inline: auto; line-height: 1.5; } .flow > * + * { -webkit-margin-before: 1em; margin-block-start: 1em; } .hero { position: relative; -webkit-margin-before: calc(var(--_header-h, 4rem) * -1); margin-block-start: calc(var(--_header-h, 4rem) * -1); } .hero::before { content: ""; position: absolute; inset: 0; background-color: rgba(0, 0, 0, 0.3); } </style> </head> <body> <header class="header"> <div class="header__inner"> <div class="logo"> LOGO </div> <nav class="nav"> <ul> <li><a href="#">Item 1</a></li> <li><a href="#">Item 2</a></li> <li><a href="#">Item 3</a></li> <li><a href="#">Item 4</a></li> </ul> </nav> </div> </header> <div class="hero"> <div class="hero__thumb"> <img src="//repo.bfw.wiki/bfwrepo/image/631aaff63da14.png" alt=""> </div> </div> <div class="content flow"> <h1>Heading</h1> <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quaerat assumenda facilis ut rerum quae totam consequun.........完整代码请登录后点击上方下载按钮下载查看
网友评论0