Анимация прогрессбара в css в соответствии с вводом формы (возможно Jquery) - PullRequest
0 голосов
/ 04 ноября 2019

progress {
width: 100%;
background: #333;
height: 20px;
border: 0;
  
}
#progress-message {
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<progress max="100" value="0" id="progress"></progress>
    <div class="progress-message" id="progress-message">This form, it wants you.</div>  
    <form action="MY ACTION" id="contactForm" method="post">
<input type="text" name="name" id="name" title="Name" value class="input-text reqyured-entry" placeholder="Your Name" required="required" autocomplete="off" />
<input type="email" name="email" id="email" title="Email" value class="input-text required-entry validate-email" placeholder="Email Address" required="required" pattern="^\S+@\S+\.\S+$" autocomplete="off" />
<input type="tel" name="telephone" id="telephone" title="Telephone" value class="input-text" placeholder="Telephone No" required="required" min="11" pattern="[0-9]*" autocomplete="off" />  
<textarea type="text" name="comment" id="comment" title="Comment" class="required-entry input-text" placeholder="Type your Message" style="resize: vertical;" min="10" rows="7" required="required"></textarea>
<input type="submit" value="SEND" title="Submit" class="button" />
</form>

<script>
$("#contactForm input, #contactForm textarea").keyup(function() {
  
var numValid = 0;
$("#contactForm input[required],#contactForm textarea[required]").each(function() {
    if (this.validity.valid) {
        numValid++;
    }
});

var progress = $("#progress"),
    progressMessage = $("#progress-message");

if (numValid == 0) {
    progress.attr("value", "0");
    progressMessage.text("This form, it wants you.");
}
if (numValid == 1) {
    progress.attr("value", "20");
    progressMessage.text("There you go, great start!");
}
if (numValid == 2) {
    progress.attr("value", "40");
    progressMessage.text("Nothing can stop you now.");
}
if (numValid == 3) {
    progress.attr("value", "80");
    progressMessage.text("You're basically a hero, right?");
}
if (numValid == 4) {
    progress.attr("value", "95");
    progressMessage.text("SO CLOSE. PRESS THE THING.");
}
  
});
</script>

Уважаемый Stackoverflow,

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

Так что вместо мгновенного прыжка он будет медленно заполнять

Ответы [ 2 ]

1 голос
/ 04 ноября 2019

Если вы добавите следующие правила CSS, это «оживит» прогресс. (Остальная часть кода такая же, как у вас, без изменений)

::-webkit-progress-value {
  background-color: green;
}
::-moz-progress-bar {
  background-color: green;
}
::-ms-fill {
  background-color: green;
}
::-webkit-progress-value {
  transition: width 1s;
}

см. Демонстрацию ниже, и вы можете прочитать больше об этом здесь

$("#contactForm input, #contactForm textarea").keyup(function() {
  
var numValid = 0;
$("#contactForm input[required],#contactForm textarea[required]").each(function() {
    if (this.validity.valid) {
        numValid++;
    }
});

var progress = $("#progress"),
    progressMessage = $("#progress-message");

if (numValid == 0) {
    progress.attr("value", "0");
    progressMessage.text("This form, it wants you.");
}
if (numValid == 1) {
    progress.attr("value", "20");
    progressMessage.text("There you go, great start!");
}
if (numValid == 2) {
    progress.attr("value", "40");
    progressMessage.text("Nothing can stop you now.");
}
if (numValid == 3) {
    progress.attr("value", "80");
    progressMessage.text("You're basically a hero, right?");
}
if (numValid == 4) {
    progress.attr("value", "95");
    progressMessage.text("SO CLOSE. PRESS THE THING.");
}
  
});
progress {
width: 100%;
background: #333;
height: 20px;
border: 0;
}
#progress-message {
text-align: center;
}

::-webkit-progress-value {
  background-color: green;
}
::-moz-progress-bar {
  background-color: green;
}
::-ms-fill {
  background-color: green;
}
::-webkit-progress-value {
  transition: width 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<progress max="100" value="0" id="progress"></progress>
    <div class="progress-message" id="progress-message">This form, it wants you.</div>  
    <form action="MY ACTION" id="contactForm" method="post">
<input type="text" name="name" id="name" title="Name" value class="input-text reqyured-entry" placeholder="Your Name" required="required" autocomplete="off" />
<input type="email" name="email" id="email" title="Email" value class="input-text required-entry validate-email" placeholder="Email Address" required="required" pattern="^\S+@\S+\.\S+$" autocomplete="off" />
<input type="tel" name="telephone" id="telephone" title="Telephone" value class="input-text" placeholder="Telephone No" required="required" min="11" pattern="[0-9]*" autocomplete="off" />  
<textarea type="text" name="comment" id="comment" title="Comment" class="required-entry input-text" placeholder="Type your Message" style="resize: vertical;" min="10" rows="7" required="required"></textarea>
<input type="submit" value="SEND" title="Submit" class="button" />
</form>
1 голос
/ 04 ноября 2019

Вы можете сделать что-то вроде этого:

function updateBar(percent) {
  var elem = $("#progress");
  var width = elem.attr("value");
  var id = setInterval(frame, 20);

  function frame() {
    if (width >= 100 || width >= percent) {
      clearInterval(id);
    } else {
      width++;
      elem.attr("value", width);
    }
  }
}

Затем мы заменим progress.attr("value", "20"); на updateBar(20)

Демо

$("#contactForm input, #contactForm textarea").keyup(function() {

  var numValid = 0;
  $("#contactForm input[required],#contactForm textarea[required]").each(function() {
    if (this.validity.valid) {
      numValid++;
    }
  });

  var progress = $("#progress"),
    progressMessage = $("#progress-message");

  if (numValid == 0) {
    updateBar(0)
    progressMessage.text("This form, it wants you.");
  }
  if (numValid == 1) {
    updateBar(20)
    progressMessage.text("There you go, great start!");
  }
  if (numValid == 2) {
    updateBar(40)
    progressMessage.text("Nothing can stop you now.");
  }
  if (numValid == 3) {
    updateBar(80)
    progressMessage.text("You're basically a hero, right?");
  }
  if (numValid == 4) {
    updateBar(95)
    progressMessage.text("SO CLOSE. PRESS THE THING.");
  }

});

function updateBar(percent) {
  var elem = $("#progress");
  var width = elem.attr("value");
  var id = setInterval(frame, 20);

  function frame() {
    if (percent <= width) {
      if (width == percent) {
        clearInterval(id);
      } else {
        width--;
        elem.attr("value", width);
      }
    } else {
      if (width >= 100 || width >= percent) {
        clearInterval(id);
      } else {
        width++;
        elem.attr("value", width);
      }
    }

  }
}
progress {
  width: 100%;
  background: #333;
  height: 20px;
  border: 0;
}

#progress-message {
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<progress max="100" value="0" id="progress"></progress>
<div class="progress-message" id="progress-message">This form, it wants you.</div>
<form action="MY ACTION" id="contactForm" method="post">
  <input type="text" name="name" id="name" title="Name" value class="input-text reqyured-entry" placeholder="Your Name" required="required" autocomplete="off" />
  <input type="email" name="email" id="email" title="Email" value class="input-text required-entry validate-email" placeholder="Email Address" required="required" pattern="^\S+@\S+\.\S+$" autocomplete="off" />
  <input type="tel" name="telephone" id="telephone" title="Telephone" value class="input-text" placeholder="Telephone No" required="required" min="11" pattern="[0-9]*" autocomplete="off" />
  <textarea type="text" name="comment" id="comment" title="Comment" class="required-entry input-text" placeholder="Type your Message" style="resize: vertical;" min="10" rows="7" required="required"></textarea>
  <input type="submit" value="SEND" title="Submit" class="button" />
</form>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...