【问题标题】:Text animation in contact form - HTML/CSS联系表单中的文本动画 - HTML/CSS
【发布时间】:2021-12-30 20:48:02
【问题描述】:

我有以下代码:

/* Contact Form */

input[type=text],
[type=email],
select,
textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #555;
  margin-top: 6px;
  margin-bottom: 16px;
  resize: vertical;
}

input[type=submit] {
  background-color: #0563bb;
  color: white;
  padding: 12px 20px;
  border: none;
  cursor: pointer;
}

input[type="text"]:focus,
input[type="email"]:focus,
#subject:focus {
  background: var(--bgFormElsFocus);
  transform: scale(1.02);
  transition: transform 0.2s ease-in-out;
}

input[type=submit]:hover {
  opacity: 0.9;
}

.contactform {
  position: relative;
  border-radius: 50px;
  background-color: #f2f2f2;
  padding: 5px;
  z-index: 2;
  display: block;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: auto;
  margin-top: 1%;
  width: 100%;
  animation-name: gradient;
  animation-duration: 3s;
  animation-iteration-count: infinite;
}

.contactform:hover {
  animation-name: gradient;
  animation-duration: 15s;
  animation-iteration-count: infinite;
}

.column {
  float: center;
  width: 50%;
  margin-top: 6px;
  padding: 20px;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

@media screen and (max-width: 600px) {
  .column,
  input[type=submit] {
    width: auto;
    margin-top: 0;
  }
}

.shakingErr {
  border-color: red;
  animation: shake 0.82s forwards;
}

@keyframes shake {
  10%,
  90% {
    transform: translate3d(-1px, 0, 0);
  }
  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }
  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }
  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
}
<section id="contact">
  <div class="container" data-aos="fade-up">
    <div class="contactform">
      <div style="text-align:center">
        <div class="section-title">
          <h2><br/>Get In Touch</h2>
        </div>
        <p>Feel Free To Reach Out To Me Through This Form! </p>
      </div>
      <div class="row">
        <div class="column">
          <form name="myform" action="https://formspree.io/f/xr123gjbqpq" id="my-form" method="POST" novalidate>
            <label for="firstname">First Name</label>
            <input type="text" id="first name" name="firstname" placeholder="Your First Name.." required>
            <label for="lastname">Last Name</label>
            <input type="text" id="lastname" name="lastname" placeholder="Your Last Name.." required>
            <label for="email">Email:</label>
            <input type="email" id="email" name="email" placeholder="Your Email.." required>
            <label for="subject">Subject</label>
            <textarea id="subject" name="subject" placeholder="Lets Collaborate.." style="height:170px" required></textarea>
            <input type="submit" value="Submit">
          </form>
        </div>
      </div>
    </div>
  </div>
</section>

我想在输入字段上显示文本动画。例如,我希望我的预期输出是这样的:

https://watch.screencastify.com/v/3mlMie2rx0UzhdymHYCR

如您所见,我希望诸如“名字”之类的文本位于输入字段内,并且当用户单击输入字段时,文本应具有动画效果并位于顶部。我怎样才能完成这项任务?有什么建议吗?

【问题讨论】:

    标签: javascript html jquery css


    【解决方案1】:

    如何做到这一点有很多变种,其中一种是使用extra-div进行输入和标签,浮动文本的作用将由标签发挥:

    .wrapper {
      padding: 20px;
    }
    
    .input-data {
      height: 40px;
      width: 100%;
      position: relative;
    }
    
    .input-data input {
      height: 100%;
      width: 100%;
      border: none;
      font-size: 17px;
      border-bottom: 2px solid silver;
      transition: border-color .4s;
      outline: none;
    }
    
    .input-data input:focus~label,
    .input-data input:valid~label {
      transform: translateY(-20px);
      font-size: 13px;
      color: #4158d0;
    }
    
    .input-data label {
      position: absolute;
      bottom: 10px;
      left: 0;
      color: grey;
      pointer-events: none;
      transition: all 0.3s ease;
    }
    
    .input-data input:focus,
    .input-data input:valid {
      border-color: #4158d0;
    }
    <div class="wrapper">
      <div class="input-data">
        <input type="text" required>
        <label>Name</label>
      </div>
    </div>

    【讨论】:

    • 你有一个父 div input-data 稍后用于动画,我将在我的代码中使用哪个类?
    • input-data 只是输入和标签的容器,将它们保持在一起并设置元素的大小
    【解决方案2】:

    我尝试为您创建一个测试版,使用您链接的视频作为参考

    仅使用 HTML 和 CSS...

    这里的诀窍是创建一个 Extra Div 作为父级,因此我们可以将标签和输入放入其中。

    现在将父级设置为position: relative;,因此标签将为absolute

    现在我们可以成功地使用 TOP、LEFT、RIGHT、BOTTOM 了(将标签定位为占位符)

    逻辑

    我把逻辑放在这里.input-container input:focus~label

    确保标签在 HTML 中的输入之后,并且在父级内部

    删除焦点边框

    请记住,如果输入焦点,它会自动像边框一样...

    要删除此边框,请使用outline: none;,因为该边框不能使用border: none;删除


    就是这样,测试我为你写的代码!我希望这会有所帮助

    这里是代码

    * {
      --input-height: 3rem;
      --light-black: rgba(0, 0, 0, 0.3);
      --medium-black: rgba(0, 0, 0, 0.6);
    }
    
    section {
      height: 100vh;
      width: 100vw;
      display: grid;
      place-content: center;
    }
    
    .input-container input {
      height: var(--input-height);
      width: 80vw;
      font-size: 2rem;
      border: none;
      border-bottom: 2px solid var(--light-black);
    }
    
    .input-container {
      position: relative;
      display: grid;
      place-items: center start;
    }
    
    .input-container label {
      position: absolute;
      left: 1rem;
      font-size: 1.5rem;
      color: var(--medium-black);
      transition-duration: 0.5s;
    }
    
    .input-container input:focus~label {
      position: absolute;
      font-size: 1rem;
      top: -1rem;
      left: 0;
      transition-duration: 0.2s;
      z-index: 2;
    }
    
    .input-container input:focus {
      outline: none;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <link rel="stylesheet" href="style.css">
    </head>
    
    <body>
      <section>
        <form action="">
          <div class="input-container">
            <input type="text" name="" id="my-input">
            <label for="my-input">name</label>
          </div>
        </form>
      </section>
    </body>
    
    </html>

    【讨论】:

    • 标签的过渡非常难看。
    • @RokoC.Buljan 现在我改成了 h5,你能给我解释一下 “这里是代码”的最佳标题
    • 不要使用标题。这是一个像任何其他句子一样的句子“这是代码示例:”.
    • 好的@RokoC.Buljan
    • 我用了你的逻辑,但它没有用,它一直在搞乱我的整个联系表格,比如它的位置......等等
    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2014-04-10
    • 2021-09-14
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多