输入属性required影响CSS过渡

4
我有一些由CSS样式化的输入框,但是在某个输入框中我需要删除"required"属性。出于某种原因,这会改变CSS动画的行为。当"required"属性被添加到一个输入框时,它会改变动画的行为。你有什么解决方案吗? HTML
<input type="text" class="form-input" autofocus required>
<span class="highlight"></span>
<span class="bar"></span>
<label class="form-label">Name</label>

<input type="text" class="form-input" autofocus>
<span class="highlight"></span>
<span class="bar"></span>
<label class="form-label">Name</label>

CSS

body {
  background-color:#fff;
  padding:40px;
}

.group            { 
  position:relative; 
  margin-bottom:45px; 
}
.form-input                 {
  font-size:18px;
  padding:10px 10px 10px 5px;
  display:block;
  width:100%;
  border:none;
  border-bottom:1px solid #757575;
}
.form-input:focus       { outline:none; }

/* LABEL ======================================= */
.form-label                  {
  color:#999; 
  font-size:18px;
  font-weight:normal;
  position:absolute;
  pointer-events:none;
  left:5px;
  top:10px;
  transition:0.2s ease all; 
  -moz-transition:0.2s ease all; 
  -webkit-transition:0.2s ease all;
}

/* active state */
.form-input:focus ~ .form-label, .form-input:valid ~ .form-label        {
  top:-20px;
  font-size:14px;
  color:#d438c5;
}

/* BOTTOM BARS ================================= */
.bar    { position:relative; display:block; width:100%; }
.bar:before, .bar:after     {
  content:'';
  height:2px; 
  width:0;
  bottom:1px; 
  position:absolute;
  background:#d438c5; 
  transition:0.2s ease all; 
  -moz-transition:0.2s ease all; 
  -webkit-transition:0.2s ease all;
}
.bar:before {
  left:50%;
}
.bar:after {
  right:50%; 
}

/* active state */
.form-input:focus ~ .bar:before, .form-input:focus ~ .bar:after {
  width:50%;
}

/* HIGHLIGHTER ================================== */
.highlight {
  position:absolute;
  height:60%; 
  width:100px; 
  top:25%; 
  left:0;
  pointer-events:none;
  opacity:0.5;
}

/* active state */
.form-input:focus ~ .highlight {
  -webkit-animation:inputHighlighter 0.3s ease;
  -moz-animation:inputHighlighter 0.3s ease;
  animation:inputHighlighter 0.3s ease;
}

.form-select {
    height: 47px;
    background: transparent;
}

/* ANIMATIONS ================ */
@-webkit-keyframes inputHighlighter {
    from { background:#d438c5; }
  to    { width:0; background:transparent; }
}
@-moz-keyframes inputHighlighter {
    from { background:#d438c5; }
  to    { width:0; background:transparent; }
}
@keyframes inputHighlighter {
    from { background:#d438c5; }
  to    { width:0; background:transparent; }
}

请看我的js fiddle。 https://jsfiddle.net/97892awh/

那么您想要移除必需项,以及您的CSS中似乎发生了什么变化? - bhansa
@bhansa 你告诉我吧。我看不到任何变化,但动画变得不同了。检查一下这个fiddle。 - Edvard Åkerberg
我看到动画改变了,因为你正在检查有效条件。 - bhansa
3个回答

2

这是因为这个选择器:

.form-input:valid ~ .form-label

正如你所看到的,你有一个:valid选择器,当输入是有效的(显然),在你的情况下,当输入是required时触发。移除它,它就可以工作了:

.form-input:focus ~ .form-label{
  top:-20px;
  font-size:14px;
  color:#d438c5;
}

jsFiddle上的示例。

这里有一个问题。如果您删除 valid 并在输入框中输入名称,则标签会下移并覆盖您输入的文本。尝试一下 fiddle,您就会看到。 - Edvard Åkerberg
1
当然可以,但你可以使用JavaScript来解决。检查输入框,如果不为空,则添加一个类名 .not-empty 并相应地进行样式设置。 - Florin Pop

1
如果您不想要不同的CSS,请在CSS中删除valid选择器。
/* 激活状态 */
.form-input:focus ~ .form-label, .form-input:valid ~ .form-label {
  top:-20px;
  font-size:14px;
  color:#d438c5;
}

这里有一个问题。如果你移除了“valid”并在输入框中输入一个名字,标签会下移并覆盖你输入的文本。试试这个fiddle,你就会看到。 - Edvard Åkerberg
可以,我能看到。让我再检查一遍,看看需要改什么。 - bhansa

0

这是我解决它的方法。

添加了一个CSS类。

.not-empty {
  top:-20px;
  font-size:14px;
  color:#d438c5;
}

还有一些jQuery

$('#name').focusout(function() {
    var val = this.value;
  if (val.length > 0) {
    $('#name-label').addClass('not-empty');
  } else {
    $('#name-label').removeClass('not-empty');
  }
});

同时也像这样编辑了HTML

<div class="group">
  <input type="text" class="form-input" id="name" autofocus>
  <span class="highlight"></span>
  <span class="bar"></span>
  <label class="form-label" id="name-label">Name</label>
</div>

https://jsfiddle.net/97892awh/3/


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