gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 14:16:05 26 4
gpt4 key购买 nike

我想在已知宽度( 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com