html - 输入 :focus to change an other div

标签 html css input-field

我想选择我的输入字段,然后我放在输入字段中的段落向上移动。因此,当我有 input:focus 时,我希望 .tekstenveld p 向上移动。

我正在尝试重新创建我在 codrops 上看到的关于创意输入字段的东西,但我无法弄清楚他们如何选择一个东西并移动其他东西。

Here is the link到创意输入字段,这样您就可以理解我的意思。

这是我到目前为止在 JS fiddle 中尝试过的内容

input {
  height: 65px;
  width: 300px;
  outline: none;
  border: none;
  padding: 0 10px 0 10px;
  font-size: 2em;
  font-family: 'Lato';
  border-bottom: 2px solid #000;
  transition: 0.2s;
  -webkit-transition: 0.2s;
  z-index: 100;
}

input:focus {
  border-bottom: 6px solid #D11E1F;
  transition: 0.2s;
  -webkit-transition: 0.2s;
}

.tekstenveld {
  position: relative;
  margin: 10px 0 10px 0;
}

.tekstenveld p {
  position: absolute;
  display: block;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  font-size: 1.5em;
  bottom: 7px;
  left: 12px;
  z-index: 50;
  pointer-events: none;
}

.tekstenveld input:focus p {
  bottom: 40px;
}
<div class="tekstenveld">
  <p>Example text</p>
  <input type="text" class="bedrijfsnaam" name="bedrijfsnaam">
</div>

最佳答案

要在 :focus 上激活纯 css 效果,您需要先切换段落和输入之间的顺序。那么css规则应该是

.tekstenveld input:focus + p {
  ...
}

也许在 bottom 属性上应用一个简短的 css transition 以使其更好:

p {
   ...
   transition: bottom .5s;
}

(当段落移动到 input 元素之上时,确保有足够的空间容纳段落)

这是最终结果:https://jsfiddle.net/0y9k50e5/1/


如果您还需要在填充时将文本保留在输入上方,您可以将 required 属性添加到 input 并像这样更改样式

.tekstenveld input:focus + p,
.tekstenveld input:valid + p {
  bottom: 40px;
}

参见 https://jsfiddle.net/0y9k50e5/2/ (注意:此方法适用于IE10+)

关于html - 输入 :focus to change an other div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34676038/

相关文章:

jquery-ui 可拖动 : change clone helper's css margin correctly

twitter-bootstrap - 移动设备上 Bootstrap 的 Datepicker 更改格式

html - -webkit-动画浏览器兼容性

jquery - 如何设置body的动态宽度

javascript - 使用 ASP.NET 的 OnFocus OnBlur

html - 为什么 100vh 不能填满整个页面?

jquery - 交错图像和输入字段的问题

javascript - 从单独的 JavaScript 文件禁用另一个 HTML 文件上的按钮

html - 使用 HTML/CSS 创建悬停下拉菜单时遇到问题

jquery - 将两行添加到一个文本框,是否可以