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

有趣的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,这里是「设计师工作日常」,求点赞求关注!!!

http://www.lryc.cn/news/292823.html

相关文章:

  • Unknown custom element:<xxx>-did you register the component correctly解决方案
  • 计算机网络自顶向下Wireshark labs-HTTP
  • 解决pandas写入excel时的ValueError: All strings must be XML compatible报错
  • 华为手表应用APP开发:watch系列 GT系列 1.配置调试设备
  • Vue(十九):ElementUI 扩展实现树形结构表格组件的勾父选子、半勾选、过滤出半勾选节点功能
  • SpringBoot RestTemplate 设置挡板
  • arcgis javascript api4.x加载非公开或者私有的arcgis地图服务
  • 2024年美赛数学建模A题思路分析 - 资源可用性和性别比例
  • UDP和TCP的区别和联系
  • delete、truncate和drop区别
  • 946. 验证栈序列
  • Linux系统管理和Shell脚本笔试题
  • docker 搭建 Seafile 集成 onlyoffice
  • 【Spring Boot 3】【JPA】嵌入式对象
  • STM32控制DS18B20温度传感器获取温度
  • 服务器常遇的响应状态码
  • 云原生业务全流程DevOps配置预研与实践
  • SouthLeetCode-打卡24年01月第5周
  • 【国产MCU】-CH32V307-通用DMA控制器及使用
  • mysql8.0-cnf文件
  • MySQL进阶45讲【11】怎么更好地给字符串字段加索引?
  • ​(三)hadoop之hive的搭建1
  • Spring事务传播机制
  • 智能分析网关V4+EasyCVR视频融合平台——高速公路交通情况的实时监控和分析一体化方案
  • Vue3.0(一):Vue的引入-options api-模板语法
  • API网关-Apisix多节点搭建(RPM包方式)
  • HAL库配置片内FLASH读写
  • 日志记录——单片机可执行文件合并
  • 2024数模美赛C题F题完整代码结果展示
  • H5调用安卓原生相机API案例