div+js实现粘性菜单导航效果代码

代码语言:html

所属分类:其他

代码描述:div+js实现粘性菜单导航效果代码,可点击导航条进行页面滚动。

代码标签: div js 粘性 菜单 导航

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

  <meta name="viewport" content="width=device-width, initial-scale=1">
<link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/reset.min.css">
<link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/all.6.4.0.css">
  
<style>
/*
 I was initially planning of a simpler CSS style but this got more complicated as I progress.
 I'm leaving these classnames as it is, as I don't feel like naming these class names
 properly anymore 😅
*/

@import url('https://fonts.googleapis.com/css2?family=Croissant+One&family=Luckiest+Guy&family=Montserrat&family=Paytone+One&display=swap');

body {
	font-family: sans-serif;
	overflow: hidden;
}

main {
	max-height: 100vh;
	overflow: auto;
	position: relative;
	scroll-behavior: smooth;
}

header {
	color: #ffffff;
	font-family: 'Croissant One', cursive;
	font-size: 3rem;
	text-shadow: -0.3rem 0.25rem #000000;
	background-image: url('//repo.bfw.wiki/bfwrepo/image/6420d4e9a0ce0.png');
	background-position: 10% 45%;
	background-repeat: no-repeat;
	background-size: cover;
	height: 10rem;
	position: relative;
}

header > div {
	background-color: rgba(0, 0, 0, 0.5);
	width: 100%;
	height: 100%;
	padding-left: 2rem;
	display: flex;
	align-items: center;
	position: absolute;
	top: 0;
	left: 0;
	box-sizing: border-box;
	backdrop-filter: blur(4px);
}

header > div > span {
	margin-left: 1rem;
}

nav {
	background-color: #0f0f0f;
	width: 100%;
	padding: 0 2rem;
	display: flex;
	justify-content: flex-end;
	position: sticky;
	top: 0;
	left: 0;
	box-sizing: border-box;
	z-index: 1;
}

nav > a {
	color: #ffffff;
	font-family: 'Luckiest Guy', cursive;
	font-size: 1.2rem;
	text-decoration: none;
	height: var(--link-height);
	padding: 1.25rem;
	display: inline-flex;
	justify-content: center;
	align-items: center;
	position: relative;
	box-sizing: border-box;
	transition: filter 64ms ease-out;
}

nav > a:hover {
	filter: drop-shadow(0 0 0.5rem #ffffff);
}

nav > a > span {
	margin-left: 0.5rem;
}

nav > a::after {
	content: '';
	background-color: #ffffff;
	width: 60%;
	height: 0.2rem;
	display: block;
	position: absolute;
	left: 50%;
	top: 80%;
	transform-origin: center;
	transform: scaleX(0) translateX(-50%);
	transition: transform 64ms ease-out;
}

/* This might be cleaner if I used SCSS instead I guess */
main:has(#portfolio:target) > nav > a:first-child::after,
main:not(:has(#portfolio:target)):not(:has(#press:target)):not(:has(#shop:target)):not(:has(#about:target)) > nav > a:first-child::after {
	transform: scaleX(1) translateX(-50%);
}

main:has(#portfolio:target) > nav > a:first-child::after {
	transform: scaleX(1) translateX(-50%);
}

main:has(#press:target) > nav > a:nth-child(2)::after {
	transform: scaleX(1) translateX(-50%);
}

main:has(#shop:target) > nav > a:nth-child(3)::after {
	transform: scaleX(1) translateX(-50%);
}

main:has(#about:target) > nav > a:last-child::after {
	transform: scaleX(1) translateX(-50%);
}

section {
	background-attachment: fixed;
	background-position: center;
	background-repeat: no-repeat;
	background-size: cover;
	height: 100vh;
}

section > div {
	background-color: rgba(0, 0, 0, 0.6);
	width: 100%;
	height: 100%;
	margin: 0;
	padding: 4rem;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: flex-start;
	box-sizing: border-box;
	backdrop-filter: blur(8px);
}

section:nth-child(2n) > div {
	align-items: flex-end;
}

section > div > h2 {
	color: #ffffff;
	font-family: 'Paytone One', sans-serif;
	font-size: 2.5rem;
	margin: 0;
	margin-bottom: 1.5rem;
}

section > div > h2 > span {
	margin-left: 0.5rem;
}

section > div > span {
	color: #ffffff;
	font-family: 'Montserrat', sans-serif;
	margin-bottom: 1.5rem;
}

section > div > button {
	color: #ffffff;
	font-family: 'Montserrat', sans-serif;
	font-size: 1rem;
	background-color: #218ae5;
	padding: 1rem 1.25rem;
	border: 0;
	border-radius: 10px;
	cursor: pointer;
	transition: background-color 64ms ease-out;
}

section > div > button:hover {
	background-color: #2d93ec;
}

section > div > button:active,
section > div > button:active {
	background-color: #1a83de;
}

section > div > button > span {
	margin-left: 0.25rem;
}

section:nth-child(2n) > div > span {
	text-align: right;
}

#portfolio {
	background-image: url('//repo.bfw.wiki/bfwrepo/image/64300e0d8afd4.png');
}

#press {
	background-image: url('//repo.bfw.wiki/bfwrepo/image/642e2dd7f27a0.p.........完整代码请登录后点击上方下载按钮下载查看

网友评论0