Сохранить позицию метки после ввода текста - PullRequest
0 голосов
/ 26 сентября 2018

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

Я попытался изменить значения преобразования длякласс label-content, однако это не имеет значения, я не уверен, что какой-нибудь java-скрипт будет лучшим решением, чем чистый css.

#contactform,
.form {
  margin-top: 2rem;
}

.field {
  border: none;
  position: relative;
  z-index: 1;
  margin: 0 1em 0 0;
  width: 100%;
  vertical-align: top;
  overflow: hidden;
}

.field:nth-child(3) {
  margin-right: 0;
}

.input {
  position: relative;
  display: block;
  float: right;
  border: none;
  border-radius: 0;
  -webkit-appearance: none;
  width: 100%;
  background: transparent;
  padding: 0.5em;
  margin-bottom: 2em;
  z-index: 100;
  height: 2rem;
}

textarea.input {
  resize: none;
  padding-bottom: 0;
}

.input:focus {
  outline: none;
}

.label {
  display: inline-block;
  float: right;
  color: #0C6333;
  font-weight: bold;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  width: 100%;
  position: absolute;
  text-align: left;
  padding: 0.5em 0;
  pointer-events: none;
  font-size: 1em;
}

.label::before,
.label::after {
  content: '';
  position: absolute;
  width: 100%;
  left: 0;
}

.label::before {
  height: 100%;
  background: #fff;
  top: 0;
  -webkit-transform: translate3d(0, -100%, 0);
  transform: translate3d(0, -100%, 0);
  -webkit-transition: -webkit-transform 0.3s;
  transition: transform 0.3s;
}

.label::after {
  height: 2px;
  background: #0C6333;
  top: 100%;
  -webkit-transition: opacity 0.3s;
  transition: opacity 0.3s;
}

.label-content {
  position: relative;
  display: block;
  width: 100%;
  padding: 0;
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
  -webkit-transition: -webkit-transform 0.3s, color 0.3s;
  transition: transform 0.3s, color 0.3s;
}

.input:focus,
.input--filled .input {
  opacity: 1;
  -webkit-transition: opacity 0s 0.3s;
  transition: opacity 0s 0.3s;
}

.input:focus+.label::before,
.input--filled .label::before {
  -webkit-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
}

.input:focus+.label .label-content,
.input--filled .label .label-content {
  color: #cbc4c6;
  -webkit-transform: translate3d(0, 2.1em, 0) scale3d(0.65, 0.65, 1);
  transform: translate3d(0, 2.1em, 0) scale3d(0.65, 0.65, 1);
}

.button,
input[type=submit] {
  -webkit-appearance: none;
  border: none;
  border-radius: 0.25rem;
  padding: 0.5em;
  color: #fff;
  background-color: #0C6333;
  transition: all 0.3s ease-out;
  -webkit-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.18), 0 2px 10px 0 rgba(0, 0, 0, 0.15);
  -moz-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.18), 0 2px 10px 0 rgba(0, 0, 0, 0.15);
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.18), 0 2px 10px 0 rgba(0, 0, 0, 0.15);
}

.button:hover,
.button:focus,
.button:active {
  -webkit-box-shadow: 0 5px 11px 0 rgba(0, 0, 0, 0.23), 0 4px 15px 0 rgba(0, 0, 0, 0.20);
  -moz-box-shadow: 0 5px 11px 0 rgba(0, 0, 0, 0.23), 0 4px 15px 0 rgba(0, 0, 0, 0.20);
  box-shadow: 0 5px 11px 0 rgba(0, 0, 0, 0.23), 0 4px 15px 0 rgba(0, 0, 0, 0.20);
}
<form class="form" id="contactform" method="POST">
  <fieldset class="field">
    <input class="input" type="text" name="name" id="inputForm" required>
    <label class="label" for="name"><span name="label" class="label-content">Your name</span></label>
  </fieldset>
  <fieldset class="field">
    <input class="input" type="email" id="inputForm" name="_replyto" required>
    <label class="label" for="_replyto"><span class="label-content">Your email</span></label>
  </fieldset>
  <fieldset class="field">
    <textarea class="input" name="message" id="inputForm" rows="1" required></textarea>
    <label class="label" for="message"><span class="label-content">Your message</span></label>
  </fieldset>
  <input class="hidden" type="text" name="_gotcha" style="display:none">
  <input class="hidden" type="hidden" name="_subject" value="Message via contact form">
  <input type="hidden" name="_next" value="" />

  <fieldset class="field">

    <input class="button submit" type="submit" value="Send">
  </fieldset>
</form>

Ответы [ 3 ]

0 голосов
/ 26 сентября 2018

Я думаю, что это может быть простым решением для вас.

var inputs = document.getElementsByClassName('input');

for(var i = 0; i < inputs.length; i++){
  inputs[i].onchange = function(){ 
    (this.value) ? this.className = 'input filled' : this.className = 'input';
  }  
}
#contactform,
.form {
  margin-top: 2rem;
}

.field {
  border: none;
  position: relative;
  z-index: 1;
  margin: 0 1em 0 0;
  width: 100%;
  vertical-align: top;
  overflow: hidden;
}

.field:nth-child(3) {
  margin-right: 0;
}

.input {
  position: relative;
  display: block;
  float: right;
  border: none;
  border-radius: 0;
  -webkit-appearance: none;
  width: 100%;
  background: transparent;
  padding: 0.5em;
  margin-bottom: 2em;
  z-index: 100;
  height: 2rem;
}

textarea.input {
  resize: none;
  padding-bottom: 0;
}

.input:focus {
  outline: none;
}

.label {
  display: inline-block;
  float: right;
  color: #0C6333;
  font-weight: bold;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  width: 100%;
  position: absolute;
  text-align: left;
  padding: 0.5em 0;
  pointer-events: none;
  font-size: 1em;
}

.label::before,
.label::after {
  content: '';
  position: absolute;
  width: 100%;
  left: 0;
}

.label::before {
  height: 100%;
  background: #fff;
  top: 0;
  -webkit-transform: translate3d(0, -100%, 0);
  transform: translate3d(0, -100%, 0);
  -webkit-transition: -webkit-transform 0.3s;
  transition: transform 0.3s;
}

.label::after {
  height: 2px;
  background: #0C6333;
  top: 100%;
  -webkit-transition: opacity 0.3s;
  transition: opacity 0.3s;
}

.label-content {
  position: relative;
  display: block;
  width: 100%;
  padding: 0;
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
  -webkit-transition: -webkit-transform 0.3s, color 0.3s;
  transition: transform 0.3s, color 0.3s;
}

.input:focus,
.input--filled .input {
  opacity: 1;
  -webkit-transition: opacity 0s 0.3s;
  transition: opacity 0s 0.3s;
}

.input:focus+.label::before,
.input.filled+.label::before {
  -webkit-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
}

.input:focus+.label .label-content,
.input.filled+.label .label-content {
  color: #cbc4c6;
  -webkit-transform: translate3d(0, 2.1em, 0) scale3d(0.65, 0.65, 1);
  transform: translate3d(0, 2.1em, 0) scale3d(0.65, 0.65, 1);
}

.button,
input[type=submit] {
  -webkit-appearance: none;
  border: none;
  border-radius: 0.25rem;
  padding: 0.5em;
  color: #fff;
  background-color: #0C6333;
  transition: all 0.3s ease-out;
  -webkit-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.18), 0 2px 10px 0 rgba(0, 0, 0, 0.15);
  -moz-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.18), 0 2px 10px 0 rgba(0, 0, 0, 0.15);
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.18), 0 2px 10px 0 rgba(0, 0, 0, 0.15);
}

.button:hover,
.button:focus,
.button:active {
  -webkit-box-shadow: 0 5px 11px 0 rgba(0, 0, 0, 0.23), 0 4px 15px 0 rgba(0, 0, 0, 0.20);
  -moz-box-shadow: 0 5px 11px 0 rgba(0, 0, 0, 0.23), 0 4px 15px 0 rgba(0, 0, 0, 0.20);
  box-shadow: 0 5px 11px 0 rgba(0, 0, 0, 0.23), 0 4px 15px 0 rgba(0, 0, 0, 0.20);
}
<form class="form" id="contactform" method="POST">
  <fieldset class="field">
    <input class="input" type="text" name="name" id="inputFormName" required>
    <label class="label" for="name" id="name"><span name="label" class="label-content">Your name</span></label>
  </fieldset>
  <fieldset class="field">
    <input class="input" type="email" id="inputFormEmail" name="_replyto" required>
    <label class="label" for="_replyto" id="_replyto"><span class="label-content">Your email</span></label>
  </fieldset>
  <fieldset class="field">
    <textarea class="input" name="message" id="inputFormMessage" rows="1" required></textarea>
    <label class="label" for="message" id="message"><span class="label-content">Your message</span></label>
  </fieldset>
  <input class="hidden" type="text" name="_gotcha" style="display:none">
  <input class="hidden" type="hidden" name="_subject" value="Message via contact form">
  <input type="hidden" name="_next" value="" />

  <fieldset class="field">

    <input class="button submit" type="submit" value="Send">
  </fieldset>
</form>
0 голосов
/ 26 сентября 2018

Я очень близок с этим решением: CodePen Here!

Что я сделал, так это добавил настройки преобразования в элемент input:valid:

input:valid + .label .label-content, .input--filled .label .label-content {
color: #cbc4c6;

-webkit-transform: translate3d(0, 2.1em, 0) scale3d(0.65, 0.65, 1);
transform: translate3d(0, 2.1em, 0) scale3d(0.65, 0.65, 1);

}

это делает то, что требуется, но ТОЛЬКО если добавленная информация верна, например, адрес электронной почты выглядит так: example@example.com, я также не смог заставить работать текстовую область, возможно, стоит использовать<input type="text"> вместо <textarea>

Я буду продолжать над этим работать, но, честно говоря, решения JavaScript будут работать лучше

0 голосов
/ 26 сентября 2018

Проблема вашего решения может быть решена с помощью небольшого количества Javascript, так как CSS не поддерживает состояние ввода, т. Е. Пусто или заполнено.

Вы можете добавить пустой атрибут value к вашему input и textarea и добавьте прослушиватель onkeyup, чтобы заполнить атрибут value пользовательским вводом.Таким образом, поле ввода будет выглядеть следующим образом:

<input class="input"
       type="text"
       name="name"
       id="inputForm"
       onkeyup="this.setAttribute('value', this.value);"
       value="" required>

Теперь вы можете выбрать «непустой» стиль ввода, как вам нравится, вы можете проверить CSS ниже для примера

input:not([value=""]):not(:focus) + label .label-content,
textarea:not([value=""]):not(:focus) + label .label-content {
  -webkit-transform: translate3d(0, -100%, 0);
  transform: translate3d(0, -100%, 0);
  font-size: 10px;
}

Вы можете найти рабочую скрипку здесь , и я буду рад ответить на любые другие ваши вопросы.

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