html - 输入事件状态下标签转换的意外行为

标签 html css transition

为什么在 left:0 绝对定位的时候标签上有空隙?

尝试重新创建类似于 Material Design 的 UI。我尝试过使用 translateY(-6px) 或其他方法,但这对于具有更多字符的标签来说不是动态的。

.formField {
  position: relative;
  height: 40px;
}

.form {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 40px;
}

.label {
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(0, 24px) scale(1);
  transition: all .3s ease-in;
}

input {
  position: absolute;
  bottom: 0;
  left: 0;
}

input:focus+.label {
  transform: translate(0, 1.5px) scale(.75);
}
<div class="formField">
  <form class="form">
    <input type="text" class="name" />
    <label for="name" class="label">Hello</label>
  </form>
</div>

最佳答案

By default, the origin of a transform is "50% 50%", which is exactly in the center of any given element.

尝试将 transform-origin: bottom left; 添加到您的 .label

.formField {
  position: relative;
  height: 40px;
}

.form {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 40px;
}

.label {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: bottom left;
  transform: translate(0, 24px) scale(1);
  transition: all .3s ease-in;
}

input {
  position: absolute;
  bottom: 0;
  left: 0;
}

input:focus+.label {
  transform: translate(0, 1.5px) scale(.75);
}
<div class="formField">
  <form class="form">
    <input type="text" class="name" />
    <label for="name" class="label">Hello</label>
  </form>
</div>

关于html - 输入事件状态下标签转换的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53163418/

相关文章:

javascript - 来自远程服务器的脚本加载缓慢

css - 蓝图 CSS - 使用它的高级规则

jquery - Coda slider 在第 12 个幻灯片项目后崩溃

Android:覆盖 Activity 主题对话框的挂起转换不起作用

html - 父 div 使用剪辑路径剪切子 div

css - 上层消失时如何在 div 移动上应用过渡?

c# - 为什么不在我的网站正文中显示背景图片?

python - django应用程序中的多字段搜索错误结果

javascript - html 表单提交中 "required"关键字的错误处理

javascript - 给页面转换每个类别用 jquery 改变