элементы формы колеблются, когда сфокусировано текстовое поле - PullRequest
0 голосов
/ 24 октября 2018

Я пытаюсь добиться материализации стиля поля ввода, т. Е. При нажатии на текстовое поле метка перемещается вверх и уменьшает размер шрифта с помощью анимации.

Я могу этого добиться, но то, что происходитзаключается в том, что когда я нажимаю на поле ввода, метка перемещается вверх, и в то же время происходит колебание, например, элементы формы движутся вверх на 2 пикселя.

Как я могу решить эту проблему?Это мой текущий код:

$(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>

Ответы [ 3 ]

0 голосов
/ 24 октября 2018

Вы можете избежать флуктуации, сделав метку, расположенную абсолютно по отношению к родительскому контейнеру (родительская позиция относительно).Вам не нужно менять html / js, простое обновление css решит проблему.

Я также создал JsFiddle для лучшего понимания, ссылка: [https://jsfiddle.net/axesvL1q/1/][1]

.contact_form {
      position: relative;
      display: block;
      margin: auto;
      width: 50%;
      height: 500px;
    }

    .form-item label {
      display: block;
      font-size: 18px;
      font-family: roboto;
      position: absolute;
      left: 0;
      top: 0;
      -moz-transition: all ease-in-out 200ms;
      -webkit-transition: all ease-in-out 200ms;
      transition: all ease-in-out 200ms;
    }

    .form-item {
      position: relative;
      margin: 30px 0;
    }

    .form-item input {
      width: 100%;
      outline: none;
      height: 36px;
      border: none;
      border-bottom: solid black 1px;
    }

    .active_text_field {
      transform: translateY(-20px);
      -moz-transition: all ease-in-out 200ms;
      -webkit-transition: all ease-in-out 200ms;
      transition: all ease-in-out 200ms;
      font-size: 16px !important;
    }
0 голосов
/ 24 октября 2018

Просто используйте правильный 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');
            };
        }); 
});
0 голосов
/ 24 октября 2018

Вы можете решить свою проблему с помощью простого CSS, нет необходимости использовать Java-скрипт для плавающих меток:

Вот фрагмент кода:

.field-container {
  position: relative;
  width: 200px;
  margin-top: 20px;
  font-family: 'Roboto', sans-serif;
}

.field {
  display: block;
  width: 100%;
  padding: 15px 10px 0;
  border: none;
  font-size: 14px;
}

.field:focus {
  outline: 0;
}

.floating-label {
  position: absolute;
  pointer-events: none;
  top: 5px;
  left: 10px;
  font-size: 10px;
  opacity: 0;
  background-color: white;
  padding: 0 2px;
  -webkit-transition: 0.2s ease-in-out;
  transition: 0.2s ease-in-out;
}

.field:valid+.floating-label {
  opacity: 1;
  top: -5px;
  color: #9e9e9e;
}

.field:focus+.floating-label {
  color: #03a9f4;
}

.field-underline {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: -5px;
  border: 1px solid #9e9e9e;
  z-index: -1;
  padding: 10px 10px 0;
}

.field:focus+.floating-label+.field-underline {
  border-color: #03a9f4;
}
<div class="field-container">
  <input type="text" class="field" required placeholder="First name" />
  <label class="floating-label">First name</label>
  <div class="field-underline"></div>
</div>
<div class="field-container">
  <input type="text" class="field" required placeholder="Last name" />
  <label class="floating-label">Last name</label>
  <div class="field-underline"></div>
</div>

Источник: плавающие этикетки с использованием css

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...