输入的字体超过输入框时,字体应用渐变透明效果

4
我遇到了一个问题:我正在创建一些具有不同状态的输入框;其中一种状态是当输入框中的文本比输入框本身还要长时:在这种情况下,输入框左侧隐藏的文本应该以渐变透明度淡出。

enter image description here

我现在的代码是这样的:

.Input {
    position: relative;
    width: 100%;
    border: none; 
    width: 200px;
}

.Input:before {
    content: '';
    position: absolute;
    bottom: 0;
    width: 100%;
    transition: border-bottom 0.5s ease;
    border-bottom: 1px solid gainsboro;
}

.Input input {
    margin-top: 20px;
    height: 40px;
    width: 100%;
    padding-left: 0;
    font-size: 16px;
    font-weight: 300;
    background-color: transparent;
    background-image: linear-gradient(to bottom, gainsboro 50%, tomato 50%);
    background-repeat: no-repeat;
    background-size: 0 11%;
    background-position: 50% 100%;
    transition: all 0.5s ease;
    box-shadow: none;
}

.Input h1 {
    font-size: 16px;
    color: gainsboro;
    font-weight: 400;
    position: absolute;
    pointer-events: none;
    left: 0;
    bottom: 0;
    transition: 0.5s ease all;
    width: 100%;
    line-height: unset;
}

.Input:hover:before {
    transition: border-bottom 0.5s ease;
    border-bottom: 1px solid dimgray;
}

input:focus {
    background-color: transparent;
    background-image: linear-gradient(to bottom, transparent 50%, tomato 50%);
    background-repeat: no-repeat;
    background-size: 100% 11%;
    background-position: 50% 100%;
    transition: all 0.5s ease;
}

input:focus,
input:not(:focus):valid {
    border: none;
    outline: none;
}

input:focus ~ h1, input:not(:focus):valid ~ h1 {
    color: tomato;
    transition: all 0.5s ease;
    transform: translateY(-25px);
}
<div class="Input">
<input type="text" class="Input" name="testInput" value="" data-id="" required>
<h1>
MyInput
</h1>
</div>
<br>

任何帮助都将受到欢迎...
提前感谢!

肯定需要一些JS,我猜你应该计算文本所占的高度,这将涉及字体大小、字体家族等。 - Temani Afif
https://dev59.com/XmUp5IYBdhLWcg3wmYgt - Temani Afif
我不知道为什么需要计算高度;我所需的是对文本应用渐变透明度,其宽度是固定的,因此不需要parseInt(window.getComputedStyle($('input')[0]).width, 10); - user8925307
但是您说当文本更长时,您希望使用这种渐变效果?因为这种渐变很容易实现。 - Temani Afif
是的,当文本长度超过宽度时,需要像图片中一样使用渐变效果。 - user8925307
1个回答

0

检查文本是否达到特定长度的想法,如果是,则显示隐藏元素。

在下面的示例中,我简化了测试,但您需要做的是计算文本应占用的宽度,然后与输入宽度进行比较。您可以查看此链接以获取一些想法计算文本宽度

$('.Input input').on('keypress change keyup', function() {
  if ($(this).val().length > 10) {
    $(this).parent().find('.grad').css('opacity', '1');
  } else {
    $(this).parent().find('.grad').css('opacity', '0');
  }
})
$('.Input .grad').click(function() {
  $(this).parent().find('input').focus();
}) 
.Input {
  position: relative;
  width: 100%;
  border: none;
  width: 200px;
}

.grad {
  position: absolute;
  cursor: text;
  top: 25px;
  bottom: 2px;
  left: 0;
  right: 90%;
  opacity: 0;
  transition: 0.5s;
  z-index: 1;
  background-image: linear-gradient(to right, rgba(255, 255, 255, 0.8), transparent);
}
.Input:before {
  content: '';
  position: absolute;
  bottom: 0;
  width: 100%;
  transition: border-bottom 0.5s ease;
  border-bottom: 1px solid gainsboro;
}

.Input input {
  margin-top: 20px;
  height: 40px;
  width: 100%;
  padding-left: 0;
  font-size: 16px;
  font-weight: 300;
  background-color: transparent;
  background-image: linear-gradient(to bottom, gainsboro 50%, tomato 50%);
  background-repeat: no-repeat;
  background-size: 0 11%;
  background-position: 50% 100%;
  transition: all 0.5s ease;
  box-shadow: none;
}

.Input h1 {
  font-size: 16px;
  color: gainsboro;
  font-weight: 400;
  position: absolute;
  pointer-events: none;
  left: 0;
  bottom: 0;
  transition: 0.5s ease all;
  width: 100%;
  line-height: unset;
}

.Input:hover:before {
  transition: border-bottom 0.5s ease;
  border-bottom: 1px solid dimgray;
}

input:focus {
  background-color: transparent;
  background-image: linear-gradient(to bottom, transparent 50%, tomato 50%);
  background-repeat: no-repeat;
  background-size: 100% 11%;
  background-position: 50% 100%;
  transition: all 0.5s ease;
}

input:focus,
input:not(:focus):valid {
  border: none;
  outline: none;
}

input:focus~h1,
input:not(:focus):valid~h1 {
  color: tomato;
  transition: all 0.5s ease;
  transform: translateY(-25px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Input">
  <span class="grad"></span>
  <input type="text" class="Input" name="testInput" value="" data-id="" required>
  <h1>
    MyInput
  </h1>
</div>
<br>


有趣的是...但你所做的是在输入上设置一个白色层;如果你的输入是一个组件,而且你不想设置这个层的颜色,而是文本的透明度呢? - user8925307
@NikitaSöze 图层的颜色可以从白色更改为透明,但我不确定是否可以仅更改部分文本的透明度,因此我选择了一个图层来控制我想要淡化的部分。我们完全可以控制它,使其看起来像文本是透明的 ;) 我已经更新了图层,你可以检查一下。 - Temani Afif
如果我使用一个层,我会将h1设置为z-index: 1,以避免层重叠头部;但是我想要的是设置文本的颜色(透明),因为我设置的是一个组件,它需要可重用。 - user8925307
@NikitaSöze 是的,这是我的第一次测试,我没有注意到一切,但我们可以简单地改变图层的位置,使它仅在文本上方(就像你现在看到的) - Temani Afif
然后,如果您将其放在文本上方,就无法再单击它进行编辑了... - user8925307
@NikitaSöze 现在你可以了 :) 检查更新,我还改变了图层的光标,这样我们就看不出区别了 ;) - Temani Afif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接