当前位置: 首页 > news >正文

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

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

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

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

核心代码

html 代码

1
2
3
4
5
6
<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 部分代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
.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 ,@www.xuepai.net并通过 content 属性插入文本,然后通过定位让其每个伪元素放到其上或下的位置。

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

完整代码如下

html 页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css" rel="external nofollow" >
    <title>文字上下滑动按钮</title>
  </head>
  <body>
    <div class="app">
      <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>
  </body>
</html>

css 样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/** 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;
}

 

http://www.jsqmd.com/news/188234/

相关文章:

  • 企业微信审批流增强:上传图片自动提取字段信息
  • Linux 之 vmstat
  • 银行卡号检测防范:防止HunyuanOCR被滥用于信息窃取
  • 阿里云OSS触发函数:上传即识别,HunyuanOCR自动处理
  • C#调用HunyuanOCR API?跨语言集成方案可行性分析
  • 损失函数组合设计:各子任务权重分配的优化策略
  • Open Neural Network Exchange在HunyuanOCR中的应用潜力
  • Vision Encoder-Decoder架构剖析:HunyuanOCR的技术根基
  • 身份证号识别合规性:敏感字段处理的法律风险提示
  • Donut模型任务重叠分析:HunyuanOCR在文档理解中的定位
  • 杰理之tws关机流程默认有2s延时处理可以修改【篇】
  • 引言:技术趋势预测的背景与意义
  • 杰理之挂载emmc设置【篇】
  • AIC-OCR农业场景测试:田间作物标签识别准确度检验
  • Figma设计稿识别:HunyuanOCR提取界面文案用于本地化
  • 手机号码自动提取:隐私信息识别的安全边界讨论
  • TensorRT加速集成:英伟达官方优化工具链对接设想
  • 腾讯云COS事件通知:结合HunyuanOCR打造智能存储方案
  • Zotero插件构想:利用HunyuanOCR自动标注文献截图内容
  • 字号大小估计功能:HunyuanOCR是否能返回相对尺寸
  • 读取文件夹并展示图像的相关问题
  • 自监督预训练阶段:HunyuanOCR如何利用无标签数据
  • RISC-V架构展望:未来在平头哥处理器上运行的潜力
  • 2025年宁夏银川优秀的岗亭生产厂家排行榜单,岗亭集成房屋/值班岗亭/成品移动岗亭/移动岗亭,岗亭生产厂家哪家好 - 品牌推荐师
  • 2025年吴忠头部岗亭采购推荐排行榜,岗亭/民宿移动房屋/岗亭环保厕所/成品移动岗亭/户外站岗岗亭,岗亭采购联系电话 - 品牌推荐师
  • 手机截图翻译需求旺:HunyuanOCR拍照翻译功能测评
  • CCPA数据权利响应:用户请求删除OCR处理记录的机制
  • 国产密码算法支持:SM2/SM3/SM4能否用于HunyuanOCR通信
  • 不只是识别文字:HunyuanOCR还能做文档问答?
  • 8.11 sys 模块