时间:2021-07-01 10:21:17 帮助过:38人阅读

https://github.com/comehope/front-end-daily-challenges
定义 dom,容器是一个无序列表,列表项代表按钮:
<ul>
    <li>home</li>
</ul>居中显示:
body {
  margin: 0;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(deepskyblue, navy);
}去掉列表项前面的符号:
ul {
  padding: 0;
  list-style-type: none;
}设置按钮的文字样式:
ul li {
  color: #ddd;
  font-size: 25px;
  font-family: sans-serif;
  text-transform: uppercase;
}用伪元素在按钮的左侧增加一个方块:
ul li {
  position: relative;
}
ul li::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  background: tomato;
  left: -100%;
}用伪元素在按钮的右侧增加一条下划线:
ul li::after {
  content: '';
  position: absolute;
  width: 100%;
  height: 0.2em;
  background: tomato;
  bottom: 0;
  left: 100%;
}接下来设置鼠标悬停效果。
当鼠标悬停时,左侧的方块移到文字所在位置:
ul li::before {
  transition: 0.4s ease-out;
}
ul li:hover::before {
  left: 100%;
}右侧的下划线移到文字所在位置,它的动画时间延迟到方块的动画快结束时再开始:
ul li::after {
  transition: 0.3s 0.3s ease-out;
}
ul li:hover::after {
  left: 0%;
}同时,提高文字的亮度:
ul li {
  transition: 0.3s;
  cursor: pointer;
}
ul li:hover {
  color: #fff;
}隐藏掉按钮外的部分,使方块和下划线在默认状态下都不可见,只有鼠标悬停时它们才从两侧入场:
ul li {
  overflow: hidden;
}最后,在 dom 中再增加几个按钮:
<ul>
    <li>home</li>
    <li>products</li>
    <li>services</li>
    <li>contact</li>
</ul>布局多个按钮:
ul {
  display: flex;
  flex-direction: column;
  align-items: center;
}
ul li {
  margin: 0.5em;
}大功告成!
更多炫酷CSS3、javascript特效代码,尽在:javascript特效大全
以上就是如何使用纯CSS实现从按钮两侧滑入装饰元素的悬停特效(附源码)的详细内容,更多请关注Gxlcms其它相关文章!