带自动背景切换 全屏菜单

  • 源码大小:7.36KB
  • 所需积分:1积分
  • 源码编号:19JP-3257
  • 浏览次数:585次
  • 最后更新:2023年05月23日
  • 所属栏目:其他
本站默认解压密码:19jp.com 或 19jp_com

简介

用jQuery和CSS/CS3编写的具有自动背景切换功能的现代全屏导航。

当用户将光标悬停在每个菜单项上时,背景图像将平滑过渡到新图像,为用户提供动态和引人入胜的体验

如何使用它:

1.将菜单列表和背景图像添加到页面中。

  1. <!-- Wrapper -->
  2. <div class="wrapp">
  3. <!-- BG Images -->
  4. <div class="menu-img">
  5. <img id="1-bg__img" src="1.jpg" alt="1">
  6. <img id="2-bg__img" src="2.jpg" alt="2">
  7. <img id="3-bg__img" src="3.jpg" alt="3">
  8. ...
  9. </div>
  10. <!-- Menu Items -->
  11. <ul class="menu">
  12. <li class="menu__item" data-id="1">
  13. <a href="#" class="menu__link">jQuery</a>
  14. </li>
  15. <li class="menu__item" data-id="2">
  16. <a href="#" class="menu__link">Script</a>
  17. </li>
  18. <li class="menu__item" data-id="3">
  19. <a href="#" class="menu__link">Net</a>
  20. </li>
  21. ...
  22. </ul>
  23. </div>

2.全屏导航所需的CSS样式。可以随意覆盖默认的CSS变量来创建自己的样式。

  1. :root {
  2. --pr-color: #fff;
  3. --second-color: #0a0a0a;
  4. --cubicbz: cubic-bezier(.9, 0, .1, 1);
  5. }
  6.  
  7. * {
  8. box-sizing: border-box;
  9. font-weight: 800;
  10. margin: 0;
  11. padding: 0;
  12. }
  13.  
  14. body {
  15. background: var(--second-color);
  16. }
  17.  
  18. .wrapp {
  19. position: relative;
  20. display: flex;
  21. align-items: center;
  22. justify-content: center;
  23. width: 100%;
  24. height: 100vh;
  25. overflow: hidden;
  26. z-index: 1;
  27. }
  28.  
  29. .menu-img {
  30. position: absolute;
  31. width: 100%;
  32. height: 100%;
  33. top: 0;
  34. left: 0;
  35. opacity: .4;
  36. filter: blur(6px);
  37. overflow: hidden;
  38. transform: scale(1.1);
  39. }
  40.  
  41. .menu-img img {
  42. position: absolute;
  43. width: 100%;
  44. height: 100%;
  45. object-fit: cover;
  46. top: 0;
  47. left: 0;
  48. transition: .8s var(--cubicbz);
  49. transform: scale(1.2);
  50. clip-path: polygon(0 0, 0 0, 0 100%, 0 100%);
  51. }
  52.  
  53. .menu-img img.active {
  54. transform: scale(1);
  55. clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
  56. }
  57.  
  58. .menu__item {
  59. position: relative;
  60. list-style: none;
  61. }
  62.  
  63. .menu__item+.menu__item {
  64. margin-top: 60px;
  65. }
  66.  
  67. .menu__item::before {
  68. position: absolute;
  69. content: '';
  70. width: 60px;
  71. height: 60px;
  72. top: 50%;
  73. left: 0;
  74. transform: translate(-80px, -50%) rotate(225deg);
  75. opacity: 0;
  76. background: url(../img/arrow.svg);
  77. background-size: cover;
  78. background-repeat: no-repeat;
  79. background-position: center center;
  80. will-change: transform;
  81. transition: .8s var(--cubicbz);
  82. }
  83.  
  84. .menu__link {
  85. position: relative;
  86. display: block;
  87. text-transform: uppercase;
  88. font-size: 75px;
  89. line-height: .8;
  90. text-decoration: none;
  91. color: transparent;
  92. -webkit-text-stroke: 1px var(--pr-color);
  93. z-index: 2;
  94. transition: .8s var(--cubicbz);
  95. }
  96.  
  97. .menu__item:hover .menu__link {
  98. transform: translateX(80px);
  99. color: var(--pr-color);
  100. -webkit-text-stroke: 1px transparent;
  101. }
  102.  
  103. .menu__item:hover::before {
  104. opacity: 1;
  105. transform: translate(0%, -50%) rotate(180deg);
  106. }

3.在文档中插入最新的jQuery库(slim版本)。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>

4.将鼠标悬停在菜单项上时在背景图像之间切换。

  1. $('.menu__item').on("mouseenter", function () {
  2. let id = $(this).data('id');
  3. $('#' + id + '-bg__img').addClass('active');
  4. });
  5. $('.menu__item').on("mouseleave", function () {
  6. $('.menu-img img').removeClass('active');
  7. });

预览截图