HTML&CSS实现导航栏滑动背景效果

今天药忘吃喽~ 2023-01-01 02:48 361阅读 0赞

大家可以先看一下背景滚动的效果
在这里插入图片描述

在这里插入图片描述
这个效果需要注意的就是鼠标悬浮的时机hover,鼠标在悬浮到不同的选项上时,背景的那个色块距离左侧的边距会发生改变。在鼠标离开导航栏时,色块会返回到你定义的那个位置(这个位置在实际的开发中,肯定是当前页面的位置,这个位置你可以自行更改)。

下面是代码部分:
html部分:

  1. <div class="container">
  2. <nav>
  3. <a href="#">home</a>
  4. <a href="#">about</a>
  5. <a href="#">blog</a>
  6. <a href="#">protfolio</a>
  7. <a href="#">contact</a>
  8. <div class="animation start-home"></div> <!-- 这个div是背景色块 -->
  9. </nav>
  10. </div>

下面是css代码部分

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. background-color: rgb(45, 60, 80);
  7. display: flex;
  8. justify-content: center;
  9. align-items: center;
  10. height: 800px;
  11. }
  12. nav {
  13. width: 590px;
  14. height: 50px;
  15. background-color: rgb(51, 73, 94);
  16. position: relative;
  17. border-radius: 8px;
  18. box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
  19. }
  20. nav a {
  21. position: relative;
  22. display: block;
  23. float: left;
  24. font-size: 15px;
  25. line-height: 50px;
  26. width: 100px;
  27. height: 50px;
  28. color: #fff;
  29. text-transform: uppercase;
  30. text-decoration: none;
  31. text-align: center;
  32. z-index: 10;
  33. }
  34. nav a:nth-child(1) {
  35. width: 100px;
  36. }
  37. nav a:nth-child(2) {
  38. width: 110px;
  39. }
  40. nav a:nth-child(3) {
  41. width: 100px;
  42. }
  43. nav a:nth-child(4) {
  44. width: 160px;
  45. }
  46. nav a:nth-child(5) {
  47. width: 120px;
  48. }
  49. nav .animation {
  50. position: absolute;
  51. height: 100%;
  52. background-color: #f97f51;
  53. z-index: 9;
  54. border-radius: 8px;
  55. transition: all 0.3s;
  56. }
  57. /* 这个地方控制背景色块的起始位置 */
  58. .start-home, nav a:nth-child(1):hover~.animation {
  59. width: 100px;
  60. left: 0;
  61. }
  62. nav a:nth-child(2):hover~.animation {
  63. width: 110px;
  64. left: 100px;
  65. }
  66. nav a:nth-child(3):hover~.animation {
  67. width: 100px;
  68. left: 210px;
  69. }
  70. nav a:nth-child(4):hover~.animation {
  71. width: 160px;
  72. left: 310px;
  73. }
  74. nav a:nth-child(5):hover~.animation {
  75. width: 120px;
  76. left: 470px;
  77. }

下面就是完整的代码部分,需要的小伙伴直接复制就可以了。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. <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>
  7. </head>
  8. <body>
  9. <div class="container">
  10. <nav>
  11. <a href="#">home</a>
  12. <a href="#">about</a>
  13. <a href="#">blog</a>
  14. <a href="#">protfolio</a>
  15. <a href="#">contact</a>
  16. <div class="animation start-home"></div>
  17. </nav>
  18. </div>
  19. </body>
  20. </html>

最后,整理不易,走过路过的小伙伴们,留个赞再走吧。
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,361人围观)

还没有评论,来说两句吧...

相关阅读

    相关 导航切换效果

    在网页中经常会遇到导航栏切换效果,也就是鼠标放到某一导航栏上,底下对应的内容就会发生变化.下面说一下实现思路: 首先说下this的用法: this在事件中指向的是事件源对象