我正在尝试实现输入框的具体化样式,即当单击文本框时,标签将向上移动并通过动画减小其字体大小。
我能够实现它,但是发生的是,当我单击输入框时,标签在向上移动,同时发生了波动,例如表单元素向上移动了2px。
我该如何解决?这是我当前的代码:
$(document).ready(function(){
$("input").focus(function(){
$(this).parent().find("label").addClass('active_text_field');
});
$("input").focusout(function(){
if ($(this).val() == '') {
$(this).parent().find("label").removeClass('active_text_field');
};
});
})
.contact_form{
position: absolute;
display: block;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 50%;
height:500px;
}
.form-item label{
display: block;
font-size: 18px;
font-family: roboto;
position: relative;
top: 30px;
-moz-transition: all ease-in-out 200ms;
-webkit-transition: all ease-in-out 200ms;
transition: all ease-in-out 200ms;
}
.form-item input{
width:100%;
outline:none;
height:36px;
border:none;
border-bottom: solid black 1px;
}
.active_text_field{
transform: translateY(-30px);
-moz-transition: all ease-in-out 200ms;
-webkit-transition: all ease-in-out 200ms;
transition: all ease-in-out 200ms;
font-size: 16px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="contact_form">
<form>
<div class="form-item">
<label for="firstName">
First Name
</label>
<input type="text" name="firstName" class="text-field">
</div>
<div class="form-item">
<label for="lastName">
Last Name
</label>
<input type="text" name="lastName" class="text-field">
</div>
<div class="form-item">
<label for="email">
Email
</label>
<input type="text" name="email" class="text-field">
</div>
<div class="form-item">
<label for="confirmEmail">
Confirm Email
</label>
<input type="text" name="confirmEmail" class="text-field">
</div>
</form>
</div>
最佳答案
只需使用正确的CSS即可制作position
。我认为这是您想通过代码实现的JSFiddle链接。
另外,当有人单击输入框内的标签旁边时,将您的label
display
属性用于inline-block
,以获得更好的浮动标签。
HTML代码-
<div class="contact_form">
<form>
<div class="form-item">
<label for="firstName">
First Name
</label>
<input type="text" name="firstName" class="text-field">
</div>
<div class="form-item">
<label for="lastName">
Last Name
</label>
<input type="text" name="lastName" class="text-field">
</div>
<div class="form-item">
<label for="email">
Email
</label>
<input type="text" name="email" class="text-field">
</div>
<div class="form-item">
<label for="confirmEmail">
Confirm Email
</label>
<input type="text" name="confirmEmail" class="text-field">
</div>
</form>
</div>
CSS代码-
.contact_form{
position: absolute;
display: block;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 50%;
height:500px;
}
.form-item {
margin-bottom: 20px;
position: relative;
}
.form-item label{
display: inline-block;
font-size: 18px;
font-family: roboto;
position: absolute;
top: 10px;
-moz-transition: all ease-in-out 200ms;
-webkit-transition: all ease-in-out 200ms;
transition: all ease-in-out 200ms;
}
.form-item input{
width:100%;
outline:none;
height:36px;
border:none;
border-bottom: solid black 1px;
}
.active_text_field{
transform: translateY(-30px);
-moz-transition: all ease-in-out 200ms;
-webkit-transition: all ease-in-out 200ms;
transition: all ease-in-out 200ms;
font-size: 16px !important;
}
JS代码-
$(document).ready(function(){
$("input").focus(function(){
$(this).parent().find("label").addClass('active_text_field');
});
$("input").focusout(function(){
if ($(this).val() == '') {
$(this).parent().find("label").removeClass('active_text_field');
};
});
});