先上效果
完整代码
以下是完整代码:
<div class="container"> <nav> <a href="#">home</a> <a href="#">about</a> <a href="#">blog</a> <a href="#">protfolio</a> <a href="#">conanct</a> <div class="animation"></div> </nav> </div>
* { padding: 0; margin: 0; } body { display: flex; width: 100%; height: 100vh; justify-content: center; align-items: center; background-color: rgb(17, 55, 87); } nav { position: relative; width: 590px; height: 50px; background-color: rgb(20, 66, 104); border-radius: 8px; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1); } nav a { z-index: 10; position: relative; display: block; float: left; font-size: 15px; line-height: 50px; width: 100px; height: 50px; text-decoration: none; text-transform: uppercase; color: #ffff; text-align: center; } 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; top: 0; left: 0; width: 100px; height: 100%; background-color: #f97f51; z-index: 1; transition: all 0.3s; border-radius: 8px; } nav a:nth-child(1):hover~.animation { width: 100px; left: 0px; } 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; }