我爱编程

CSS 实现导航栏下划线效果

2018-04-07  本文已影响674人  苏敏

一般实现这个效果我们需要借助于js,但是我今天发现一个小技巧了可以单纯通过css就实现,一起来看看吧~

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div>
  <ul>
  <li>不可思议</li>
  <li>导航栏</li>
  <li>光标小下划线</li>
  <li>PURE CSS</li>
  <li>Nav Underline</li>
</ul>
  </div>
</body>
</html>
ul {
    display: flex;
    position: absolute;
    width: 800px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    list-style:none
}

li {
    position: relative;
    padding: 20px;
    font-size: 24px;
    color: #000;
    transition: 0.1s all linear;
    cursor: pointer;
}

li::before {
    content: "";
    position: absolute;
    top: 0;
    left: 100%;
    width: 0;
    height: 100%;
    border-bottom: 2px solid #000;
    transition: 0.2s all linear;
}

li:hover::before {
    width: 100%;
    top: 0;
    left: 0;
    transition-delay: 0.1s;
    border-bottom-color: #000;
}

li:hover ~ li::before {
    left: 0;
}

li:active {
    background: #000;
    color: #fff;
}

image.png
上一篇 下一篇

猜你喜欢

热点阅读