css - 是否可以从头到尾过渡占位符文本?

标签 css placeholder

我想在已知宽度( overflow hidden )的文本输入中从头到尾转换一行动态占位符文本

现在我知道对于常规容器 div 我可以使用转换来转换正确的长度...

所以对于长度为 100px 的容器,我可以使用以下方法过渡到文本末尾:transform: translateX(calc(100px - 100%)

div {
  width: 100px;
  border: 2px solid green;
  margin: 10px;
  padding: 5px;
}
div {
  white-space: nowrap;
  overflow: hidden;
}
span {
  display: inline-block;
  animation: 4s scrollText forwards;
}

@keyframes scrollText {
  to {
      transform: translateX(calc(100px - 100%));
  }
}
<div><span>some really really really long text here</span></div>

我想知道我是否可以使用 placeholder pseudo element 的占位符文本实现上述效果.

所以我尝试了以下方法:

input {
  width: 100px;
  border: 2px solid green;
  margin: 10px;
  padding: 5px;
}

input::placeholder {
  color: red;
  animation: 4s scrollText;
  transform: translateX(0);
}

@keyframes scrollText {
  from {
      transform: translateX(0);
  }
  to {
      transform: translateX(calc(100px - 100%));
  }
}
<input type="text" placeholder="some really really really long text here">

...但是由于占位符伪元素上可用属性的限制,上面的代码片段不起作用

All properties that apply to the ::first-line pseudo-element also apply to the ::placeholder pseudo-element.

不幸的是,transform1 没有出现在 that list 中.

所以我想知道是否有另一种方法可以实现此目的 - 可能使用占位符伪元素支持的其他属性。


  1. 奇怪的是,在没有 calc 函数和动画的情况下,transform 属性似乎(有点)在 Chrome 上工作。

input {
  width: 100px;
  border: 2px solid green;
  margin: 10px;
  padding: 5px;
}

input::placeholder {
  color: red;
  transform: translateX(-50%);
}
<input type="text" placeholder="some really really really long text here">

最佳答案

您可以使用额外的包装器来模拟这一点,而无需在输入元素上真正使用占位符:

input {
  width: 100px;
  border: 2px solid green;
  padding: 5px;
  vertical-align:top;
  background:transparent; /* a transparent background to show the pseudo element */
}


.input {
  display:inline-block;
  margin: 10px;
  position:relative;
  z-index:0;
  overflow:hidden;
}

.input:before {
  content:attr(placeholder);
  position:absolute;
  left:5px;
  top:5px;
  white-space:nowrap; /* no line break */
  color: red;
  pointer-events:none; /* avoid any interaction */
  animation: 4s scrollText forwards;
  z-index:-1;
}

@keyframes scrollText {
  to {
      transform: translateX(calc(100px - 100%));
  }
}

/* hide the before on focus */
.input:focus-within:before{
   visibility:hidden;
}

/* add background to hide the before when there is text and no focus*/
input:not(:placeholder-shown) {
  background:#fff;
}
<div class="input" placeholder="some really really really long text here">
<input type="text" placeholder=" "> <!-- needs an empty placeholder for :placeholder-shown -->
</div>

关于css - 是否可以从头到尾过渡占位符文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55901797/

相关文章:

jquery - 如何让图像在 chrome、IE 和 safari 中调整大小

html - 如何在不使用 JS/jQuery 的情况下使占位符消失?

python - 以程序方式将占位符添加到 tkinter Entry 小部件

css - 具有焦点的输入的焦点占位符

html - 如何限制 flexbox 布局中最高后代的动态高度?

html - 导航位于底部,需要内部列表显示在导航上方而不是导航下方吗?

html - 在 css 的一个输入占位符中可以有 2 种不同的字体大小吗?

java - 如何从 Maven 插件访问 Maven 占位符?

html - 如何使用 html/css 显示内联和居中的多个图像?

html - 导航栏宽度高于其父 div