现代侧边栏菜单

2025-05-24

现代侧边栏菜单

作为 CSS 爱好者,我总是尝试使用 CSS 复制我看到的设计。

今天想做一个侧边栏菜单。
我一如既往地用了 Codepen(愿上帝保佑 Codepen)。

首先,我在页面上添加了我需要的必要 HTML:


<aside>
  <p> Menu </p>
  <a href="javascript:void(0)">
    <i class="fa fa-user-o" aria-hidden="true"></i>
    My drive
  </a>
  <a href="javascript:void(0)">
    <i class="fa fa-laptop" aria-hidden="true"></i>
    Computers
  </a>
  <a href="javascript:void(0)">
    <i class="fa fa-clone" aria-hidden="true"></i>
    Shared with me
  </a>
  <a href="javascript:void(0)">
    <i class="fa fa-star-o" aria-hidden="true"></i>
    Starred
  </a>
  <a href="javascript:void(0)">
    <i class="fa fa-trash-o" aria-hidden="true"></i>
    Trash
  </a>
</aside>

Enter fullscreen mode Exit fullscreen mode

现在我希望我的侧边栏为 100vh 并且没有边距:

body {
  font-family: 'Roboto';
  width: 100%;
  height: 100vh;
  margin: 0;
}
Enter fullscreen mode Exit fullscreen mode

为了获得侧边栏的美观视觉效果:

aside {
  color: #fff;
  width: 250px;
  padding-left: 20px;
  height: 100vh;
  background-image: linear-gradient(30deg , #0048bd, #44a7fd);
  border-top-right-radius: 80px;
}

aside a {
  font-size: 12px;
  color: #fff;
  display: block;
  padding: 12px;
  padding-left: 30px;
  text-decoration: none;
}

aside a:hover {
  color: #3f5efb;
  background: #fff;
  position: relative;
  background-color: #fff;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
}

aside a i {
  margin-right: 5px;
}
Enter fullscreen mode Exit fullscreen mode

最难实现的效果是将鼠标悬停在菜单项上:

替代文本

对于左上角和左下角的悬停设计,我添加了 2 个伪元素,如下所示:


aside a:hover::after {
  content: "";
  position: absolute;
  background-color: transparent;
  bottom: 100%;
  right: 0;
  height: 35px;
  width: 35px;
  border-bottom-right-radius: 18px;
  box-shadow: 0 20px 0 0 #fff;
}

aside a:hover::before {
  content: "";
  position: absolute;
  background-color: transparent;
  top: 38px;
  right: 0;
  height: 35px;
  width: 35px;
  border-top-right-radius: 18px;
  box-shadow: 0 -20px 0 0 #fff;
}
Enter fullscreen mode Exit fullscreen mode

有一些硬编码值,但如果有效,它就有效!:))

感谢您阅读这篇文章,我很好奇您是否有其他方法来实现悬停效果。

文章来源:https://dev.to/florincornea/modern-sidebar-menu-53o1
PREV
1 小时完成 10 个 JavaScript 项目 - 编程挑战
NEXT
基于 React 的微前端