有趣的CSS - 输入框选中交互动效

页面效果

此效果主要使用 css 伪选择器配合 html5 required 属性来实现一个简单的输入框的交互效果。

此效果可适用于登录页入口、小表单提交等页面,增强用户实时交互体验。


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

核心代码

html代码

<label>
  <input type="text" required>
  <span>用户名</span>
</label>

页面主要使用 labelinput 标签,注意 input 中使用 required 属性,来判断 input 是否为空。

css代码

label{
   
  width: 240px;
  position: relative;
  display: flex;
  align-items: center;
}
input{
   
  width: 240px;
  height: 32px;
  line-height: 32px;
  padding: 0 10px;
  border: 2px solid transparent;
  border-bottom-color: #333;
  background-color: transparent;
  box-sizing: border-box;
  transition: all 0.3s;
  font-size: 14px;
  color: #333;
}
span{
   
  position: absolute;
  font-size: 14px;
  color: #999;
  left: 10px;
  cursor: text;
  z-index: 1;
  transition: all 0.3s;
}
label:hover input,input:focus{
   
  border-color: blue;
  border-radius: 8px;
}
input:focus+span,input:valid+span{
   
  transform: translateY(-32px);
}

使用选择器 :hover:foucus 来获取鼠标状态,根据不同鼠标的不同状态来设置样式以及过渡效果,使用 :valid 来验证 input 值。

完整代码

html页面

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <title>输入框选中交互动效</title>
  </head>
  <body>
    <div class="app">
      <label>
        <input type="text" required>
        <span>用户名</span>
      </label>
    </div>
  </body>
</html>

css样式

.app{
   
  width: 100%;
  height: 100vh;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
input{
   
  list-style: none;
  outline-style: none;
}
label{
   
  width: 240px;
  position: relative;
  display: flex;
  align-items: center;
}
input{
   
  width: 240px;
  height: 32px;
  line-height: 32px;
  padding: 0 10px;
  border: 2px solid transparent;
  border-bottom-color: #333;
  background-color: transparent;
  box-sizing: border-box;
  transition: all 0.3s;
  font-size: 14px;
  color: #333;
}
span{
   
  position: absolute;
  font-size: 14px;
  color: #999;
  left: 10px;
  cursor: text;
  z-index: 1;
  transition: all 0.3s;
}
label:hover input,input:focus{
   
  border-color: blue;
  border-radius: 8px;
}
input:focus+span,input:valid+span{
   
  transform: translateY(-32px);
}

页面效果

以上就是全部代码以及简单的写法思路,希望你喜欢这个交互输入框。


[1] 原文阅读

我是 Just,这里是「设计师工作日常」,求点赞求关注!!!

相关推荐

  1. HTML中input输入(详解输入用法)

    2024-02-03 10:38:03       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-03 10:38:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-03 10:38:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-03 10:38:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-03 10:38:03       18 阅读

热门阅读

  1. 《每天一分钟学习C语言·十三》

    2024-02-03 10:38:03       36 阅读
  2. linux 内核升级

    2024-02-03 10:38:03       28 阅读
  3. 基于机器学习的无损缺陷检测技术研究进展

    2024-02-03 10:38:03       35 阅读
  4. 2.2作业

    2.2作业

    2024-02-03 10:38:03      33 阅读
  5. Vue 图片加载失败处理

    2024-02-03 10:38:03       22 阅读
  6. Vue3快速上手

    2024-02-03 10:38:03       27 阅读
  7. react+ts

    react+ts

    2024-02-03 10:38:03      28 阅读
  8. 115.工业相机海康SDK开发指南(阅读)

    2024-02-03 10:38:03       20 阅读
  9. 探索ChatGPT:AI技术的新篇章与人类的共舞

    2024-02-03 10:38:03       28 阅读
  10. 【硬件产品经理】锂电池充电时间怎么计算?

    2024-02-03 10:38:03       29 阅读
  11. MySQL的备份与恢复

    2024-02-03 10:38:03       27 阅读