1. 程式人生 > 其它 >HTML&CSS實現導航欄滑動背景效果

HTML&CSS實現導航欄滑動背景效果

技術標籤:前端css動畫大合集csshtmlcss3html5animation

大家可以先看一下背景滾動的效果
在這裡插入圖片描述

在這裡插入圖片描述
這個效果需要注意的就是滑鼠懸浮的時機hover,滑鼠在懸浮到不同的選項上時,背景的那個色塊距離左側的邊距會發生改變。在滑鼠離開導航欄時,色塊會返回到你定義的那個位置(這個位置在實際的開發中,肯定是當前頁面的位置,這個位置你可以自行更改)。

下面是程式碼部分:
html部分:

<div class="container">
		<nav>
			<a href="#">home</a>
			<
a
href="#">
about</a> <a href="#">blog</a> <a href="#">protfolio</a> <a href="#">contact</a> <div class="animation start-home"></div> <!-- 這個div是背景色塊 --> </nav> </div>

下面是css程式碼部分

  * {
		margin: 0;
		padding: 0;
	}

	body {
		background-color: rgb(45, 60, 80);
		display: flex;
		justify-content: center;
		align-items: center;
		height: 800px;
	}

	nav {
		width: 590px;
		height: 50px;
		background-color: rgb(51, 73, 94);
		position: relative;
		border-radius: 8px;
		box-shadow: 0 2px 3px rgba
(0, 0, 0, 0.1); } nav a { position: relative; display: block; float: left; font-size: 15px; line-height: 50px; width: 100px; height: 50px; color: #fff; text-transform: uppercase; text-decoration: none; text-align: center; z-index: 10; } nav a:nth-child(1) { width: 100px; } nav a:nth-child(2) { width: 110px; } nav a:nth-child(3) { width: 100px; } nav a:nth-child(4) { width: 160px; } nav a:nth-child(5) { width: 120px; } nav .animation { position: absolute; height: 100%; background-color: #f97f51; z-index: 9; border-radius: 8px; transition: all 0.3s; } /* 這個地方控制背景色塊的起始位置 */ .start-home, nav a:nth-child(1):hover~.animation { width: 100px; left: 0; } nav a:nth-child(2):hover~.animation { width: 110px; left: 100px; } nav a:nth-child(3):hover~.animation { width: 100px; left: 210px; } nav a:nth-child(4):hover~.animation { width: 160px; left: 310px; } nav a:nth-child(5):hover~.animation { width: 120px; left: 470px; }

下面就是完整的程式碼部分,需要的小夥伴直接複製就可以了。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}

			body {
				background-color: rgb(45, 60, 80);
				display: flex;
				justify-content: center;
				align-items: center;
				height: 800px;
			}

			nav {
				width: 590px;
				height: 50px;
				background-color: rgb(51, 73, 94);
				position: relative;
				border-radius: 8px;
				box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
			}

			nav a {
				position: relative;
				display: block;
				float: left;
				font-size: 15px;
				line-height: 50px;
				width: 100px;
				height: 50px;
				color: #fff;
				text-transform: uppercase;
				text-decoration: none;
				text-align: center;
				z-index: 10;
			}

			nav a:nth-child(1) {
				width: 100px;
			}

			nav a:nth-child(2) {
				width: 110px;
			}

			nav a:nth-child(3) {
				width: 100px;
			}

			nav a:nth-child(4) {
				width: 160px;
			}

			nav a:nth-child(5) {
				width: 120px;
			}

			nav .animation {
				position: absolute;
				height: 100%;
				background-color: #f97f51;
				z-index: 9;
				border-radius: 8px;
				transition: all 0.3s;
			}

			.start-home,
			nav a:nth-child(1):hover~.animation {
				width: 100px;
				left: 0;
			}

			nav a:nth-child(2):hover~.animation {
				width: 110px;
				left: 100px;
			}

			nav a:nth-child(3):hover~.animation {
				width: 100px;
				left: 210px;
			}

			nav a:nth-child(4):hover~.animation {
				width: 160px;
				left: 310px;
			}

			nav a:nth-child(5):hover~.animation {
				width: 120px;
				left: 470px;
			}
		</style>
	</head>
	<body>
		<div class="container">
			<nav>
				<a href="#">home</a>
				<a href="#">about</a>
				<a href="#">blog</a>
				<a href="#">protfolio</a>
				<a href="#">contact</a>
				<div class="animation start-home"></div>
			</nav>
		</div>
	</body>
</html>

最後,整理不易,走過路過的小夥伴們,留個贊再走吧。
在這裡插入圖片描述