【问题标题】:Firefox fails to animate input text placeholderFirefox 无法为输入文本占位符设置动画
【发布时间】:2015-02-12 11:37:32
【问题描述】:

回答一些问题我试图展示如何在没有 JavaScript / jQuery 的情况下使用纯 CSS 对文本输入占位符进行动画处理。我将:not(:focus) 伪类与input-placeholder 一起使用,并将闪烁动画放入其中。在 Chrome、IE 和 Opera 上它运行良好,但在 Firefox 上却失败了。

如果我尝试设置其他属性,例如 color:red 它可以工作,所以我确信我的代码可以正确访问模糊时的占位符。我还在一个带有文本的简单 div 上尝试了相同的动画,它也可以工作。因此,Firefox 似乎特别无法为占位符设置动画。我错过了什么还是只是 Firefox 的错误?

这是我的代码:

#theInput:not(:focus)::-webkit-input-placeholder {
  -webkit-animation: simple-blink-text 1s step-start infinite;
}
#theInput:not(:focus)::-moz-placeholder {
  color: red;
  animation: simple-blink-text 1s step-start infinite;
}
#theInput:not(:focus):-ms-input-placeholder {
  animation: simple-blink-text 1s step-start infinite;
}
@-webkit-keyframes simple-blink-text {
  0% { color: black }
  25% { color: transparent }
  100% { color: black }
}
@keyframes simple-blink-text {
  0% { color: black }
  25% { color: transparent }
  100% { color: black }
}
<input type="text" id="theInput" placeholder="This field is required!">

CodePen中也一样

【问题讨论】:

  • 尝试在 Firefox 中使用 -moz-animation
  • 这是我尝试过的第一件事,但没有帮助
  • 试试answers here
  • @RahulDesai 这不是我的问题的答案。
  • @AlexanderDayan 我只是提出一些可能对你有帮助的想法。

标签: html firefox input css-animations


【解决方案1】:

似乎::-moz-placeholder 在 Firefox 中没有动画效果。尝试在 FireFox 中使用 @-moz-document url-prefix()

#theInput:not(:focus)::-webkit-input-placeholder {
  -webkit-animation: simple-blink-text 1s step-start infinite;
}
@-moz-document url-prefix() {
  #theInput:not(:focus) {
    animation: simple-blink-text 1s step-start infinite;
  }
}
#theInput:not(:focus):-ms-input-placeholder {
  animation: simple-blink-text 1s step-start infinite;
}
@-webkit-keyframes simple-blink-text {
  0% { color: black }
  25% { color: transparent }
  100% { color: black }
}
@keyframes simple-blink-text {
  0% { color: black }
  25% { color: transparent }
  100% { color: black }
}

【讨论】:

  • 有效:codepen.io/rijii49/pen/YPYmxK!感谢您的精彩回答!
  • 进一步调查这个问题我发现了一个额外的问题。因为您的解决方案为整个控件而不是占位符设置动画,所以即使插入了一些文本,它也会闪烁。但是,我发现了解决它的附加技巧:如果控件具有“必需”属性,则可以使用“:无效”伪类来检测空值(请参阅更新的 CodePen)。不管怎样,你的回答很好:)
猜你喜欢
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 2014-04-05
  • 2021-11-24
  • 2014-01-07
相关资源
最近更新 更多