gpt4 book ai didi

javascript - 聚焦文本框时表单元素会波动

转载 作者:太空宇宙 更新时间:2023-11-04 01:02:24 31 4
gpt4 key购买 nike

我正在尝试实现输入框的具体化样式,即当单击文本框时,标签会向上移动并通过动画减小其字体大小。

我能够实现它,但发生的事情是,当我点击输入框时,标签向上移动,同时发生波动,如表单元素向上移动 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 以便当有人在输入框内的标签旁边单击时更好地 float 标签。

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');
};
});
});

关于javascript - 聚焦文本框时表单元素会波动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52962733/

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