1
我已经制作了这些表格,可以在填充标签后保持标签可见。我使用绝对定位,它给出了填充左侧是固定距离的问题,而不依赖于标签名的长度。 你可以看到这里的问题:https://jsfiddle.net/eo4uop7g/输入标签和长名称
我试图找出一些其他的解决方案,但没有任何运气。也许你们中的一些人有一个想法如何使这更灵活?
$(document).ready(function() {
$('input').each(function() {
$(this).on('focus', function() {
$(this).parent('.userbasic article').addClass('active');
});
$(this).on('blur', function() {
if ($(this).val().length == 0) {
$(this).parent('.userbasic article').removeClass('active');
}
});
if ($(this).val() != '') $(this).parent('.userbasic article').addClass('active');
});
});
form input {
height: 45px;
width: 100%;
transition: .1s all linear;
}
.userbasic {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.userbasic article {
flex: 1;
margin: 0 .5rem 1rem;
position: relative;
}
.userbasic article:first-child {
margin-left: 0;
}
.userbasic article:last-child {
margin-right: 0;
}
.userbasic article.active input {
padding-left: 80px;
}
.userbasic article.active label {
top: 0;
left: 0;
height: 50px;
padding: 18px;
color: white;
background: #777;
box-sizing: border-box;
}
.userbasic label {
position: absolute;
color: #777;
top: 18px;
left: 15px;
font-size: 12px;
transition: .1s all linear;
cursor: text;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="userbasic">
<article>
<label for="i4">Zip</label>
<input id="i4" type="text">
</article>
<article>
<label for="i5">Very long name</label>
<input id="i5" type="Number">
</article>
</form>