欢迎来到科站长!

CSS/HTML

当前位置: 主页 > 网络编程 > CSS/HTML

css特效 - 按钮hover文字上下滑动

时间:2024-08-02 21:47:00|栏目:CSS/HTML|点击:

当鼠标悬浮按钮上方时,利用伪元素和定位来实现伪元素块上下滑入和滑出的交互效果。

此效果适用于较大的按钮入口,如主页 banner 处按钮,也可以放在当作首屏当作一个大 banner 作为视觉效果等场景。

核心代码部分,简要说明了写法思路;完整代码在最后,可直接复制到本地运行。

核心代码

html 代码

<div class="btn30">
  <span class="btn-text30">探</span>
  <span class="btn-text30">索</span>
  <span class="btn-text30">未</span>
  <span class="btn-text30">知</span>
</div>


一个 div 父元素,及其子元素 span ,形成一个大按钮。

css 部分代码

.btn30{
  height: 42px;
  position: relative;
  cursor: pointer;
  display: flex;
  overflow: hidden;
}
.btn-text30{
  width: 36px;
  height: 42px;
  line-height: 42px;
  text-align: center;
  display: block;
  background-color: #457356;
  color: #ffffff;
  font-size: 16px;
  font-weight: 700;
  position: relative;
}
.btn-text30:after{
  width: 36px;
  height: 42px;
  position: absolute;
  background-color: #3185fa;
  color: #ffffff;
  z-index: 99;
  transition: 0.3s ease-in-out;  /*添加过渡效果*/
}
.btn-text30:nth-of-type(1):after{
  content: '学';
  top: -42px;
  left: 0;
}
.btn-text30:nth-of-type(2):after{
  content: '无';
  top: 42px;
  left: 0px;
}
.btn-text30:nth-of-type(3):after{
  content: '止';
  top: -42px;
  left: 0;
}
.btn-text30:nth-of-type(4):after{
  content: '境';
  top: 42px;
  left: 0px;
}
.btn30:hover .btn-text30:after{
  top: 0;  /*改变伪元素定位*/
}


每个 span 添加其伪元素 :after ,并通过 content 属性插入文本,然后通过定位让其每个伪元素放到其上或下的位置。

通过 :hover 获取鼠标状态,当鼠标悬浮在按钮上方时,改变其伪元素的定位,利用 transition 过渡效果,来让其伪元素上下滑入滑出,实现按钮块上下滑动的的交互效果。

完整代码如下

html 页面

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css" rel="external nofollow" >
    <title>文字上下滑动按钮</title>
  </head>
  <body>
    <div>
      <div>
        <span>探</span>
        <span>索</span>
        <span>未</span>
        <span>知</span>
      </div>
    </div>
  </body>
</html>


css 样式

/** style.css **/
.app{
  width: 100%;
  height: 100vh;
  background-color: #ffffff;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.btn30{
  height: 42px;
  position: relative;
  cursor: pointer;
  display: flex;
  overflow: hidden;
}
.btn-text30{
  width: 36px;
  height: 42px;
  line-height: 42px;
  text-align: center;
  display: block;
  background-color: #457356;
  color: #ffffff;
  font-size: 16px;
  font-weight: 700;
  position: relative;
}
.btn-text30:after{
  width: 36px;
  height: 42px;
  position: absolute;
  background-color: #3185fa;
  color: #ffffff;
  z-index: 99;
  transition: 0.3s ease-in-out;
}
.btn-text30:nth-of-type(1):after{
  content: '学';
  top: -42px;
  left: 0;
}
.btn-text30:nth-of-type(2):after{
  content: '无';
  top: 42px;
  left: 0px;
}
.btn-text30:nth-of-type(3):after{
  content: '止';
  top: -42px;
  left: 0;
}
.btn-text30:nth-of-type(4):after{
  content: '境';
  top: 42px;
  left: 0px;
}
.btn30:hover .btn-text30:after{
  top: 0;
}

页面渲染效果

以上就是所有代码,以及简单的思路,希望对你有一些帮助或者启发。

CSS 是一种很酷很有趣的计算机语言,在这里跟大家分享一些 CSS 实例 Demo,为学习者获取灵感和思路提供一点帮助,希望你们喜欢。


上一篇:暂无

栏    目:CSS/HTML

下一篇:让你的网页动起来:Javascript+CSS拖曳盒子指南

本文标题:css特效 - 按钮hover文字上下滑动

本文地址:https://fushidao.cc/wangluobiancheng/589.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:1205677645 | 邮箱:1205677645@qq.com

Copyright © 2018-2024 科站长 版权所有冀ICP备14023439号