我试图在前端设计一个带有波浪效果的原木,以使标签的字母以波浪形状移动,但我无法将跨距标签动态添加到字母上 我将先放入HTML和jQuery代码,然后放入CSS代码,最后放入DOM代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>login wave effect</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha512-z3gLpd7yknf1YoNbCzqRKc4qyor8gaKU1qmn+CShxbuBusANI9QpRohGBreCFkKxLhei6S9CQXFEbbKuqLg0DA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <link rel="stylesheet" href="stylesheet.css">
    <script src="code.jquery.com_jquery-3.7.0.js"></script>
</head>
<body>
    <div class="container">
        <h1>Please Login</h1>
        <form>
            <div class="form-container">
                <input type="text" required >
                <label for="input">Email</label>
            </div>
            <div class="form-container">
                <input type="password" required>
                <label for="input">Password</label>
            </div>
            <button class="btn" type="submit">Login</button>
        </form>
        <p class="text">
            Don't have an account? <a href="#">Register</a>
        </p>
    </div>
    <script src="script.js"></script>
    <!--<script>
        $(".form-container label").each(function(){
            $(this).html = $(this)
            .text()
            .split("")
            .map(function(element,idx){
                `<span style="transition-delay:${idx*50}ms">${element}</span>`})
            .join('')
        });
    </script>-->
</body>
</html>

这是DOM代码

var labels = document.querySelectorAll(".form-container label");
labels.forEach(function(label){
    label.innerHTML = innerText
    .split("")
    .map(function(element,idx){
        `<span style="transition-delay:${idx*50}ms">${element}</span>`})
    .join('');
 });

这是一段css代码

@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
*{
    box-sizing: border-box;
}
body{
    font-family:'Muli', sans-serif;
    margin: 0;
    display:flex;
    flex-direction: column;
    align-items: center;
    justify-content:center;
    height: 100vh;
    overflow: hidden;
    background-color: lightblue;
}
.container{
    position: relative;
    background-color: rgb(44, 88, 124);
    color: white;
    padding: 20px 50px;
    border: 0;
    border-radius: 10px;
    position: relative;
}
.btn{
    display: block;
    font-family: inherit;
    width: 100%;
    padding :10px;
    border: 0;
    border-radius: 5px;
    background-color: blueviolet;
    color: aliceblue;
    left: 0;
    margin-bottom: 60px;
}
.btn:active{
    outline: 0;
    transform: scale(0.95);
    background-color: rgb(83, 20, 142);
}
h1{
    text-align: center;
    font-size: 60px;
    margin: 10px 0 30px;
}
.form-container{
    margin:20px 0px 40px;
    padding: 10px;
    width: 300px;
}
.form-container input{
    background-color: transparent;
    border: 0;
    border-bottom: 2px white solid;
    width: 100%;
    height: 30px;
    font-size: 20px;
    color: bisque;
}
.form-container input:focus,
.form-container input:valid{
    outline: 0;
}
.text{
    margin-top: 50px;
}
.text a{
    text-decoration: none;
    color: rgb(30, 3, 8);
}
.form-container label{
    width: 100px;
    height: 50px;
}
.form-container label span{
    position: absolute;
    top: 15px;
    transition: transform 0.3s ease-in-out;
}
.form-container input:focus + label span,
.form-container input:valid + label span{
    transform: translateY(-40px);
}

推荐答案

That's a perfect case for using CSS var() and CSS calc().
Set the --idx CSS var to the element index and multiply it in CSS (animation-delay) by the desired time using calc() like: calc(var(--idx) * 0.2s)

const labels = document.querySelectorAll(".form-container label");

labels.forEach(function(label) {
  label.innerHTML = [...label.textContent].map((char, idx) => {
    return `<span style="--idx:${idx};">${char}</span>`;
  }).join("");
});
form label span {
  display: inline-block;
  animation: wave 1s linear calc(var(--idx) * 0.2s) alternate infinite;
  translate: 0 -30%;
}

@keyframes wave {
  to { translate: 0 30%; }
}
<div class="container">
  <h1>Please Login</h1>
  <form>
    <div class="form-container">
      <input type="text" required>
      <label for="input">Email</label>
    </div>
    <div class="form-container">
      <input type="password" required>
      <label for="input">Password</label>
    </div>
    <button class="btn" type="submit">Login</button>
  </form>
  <p class="text">
    Don't have an account? <a href="#">Register</a>
  </p>
</div>

另外,请注意,.map()应该有一个直接或隐含的return语句.此外,innerText也没有定义和设置.您原本打算使用Element.extContent(BTW比Element.innerText更受欢迎).

Html相关问答推荐

如何才能完成这个背景按钮

html中背景色的全单元格R中的Rmarkdown表

如何防止SVG图标出现断行?

Div内容防止同级大小增加

悬停效果在新西兰表上不起作用

在弹性框布局中的第n个元素后强制换行

默认情况下使用Disbale Scroll,但在单击图标后将其激活

如何使用 Flutter 构建 Chrome 扩展?

为什么可滚动弹性项目需要 flex-basis: 0 ?

margin-top 未应用于无序列表的第一项

为什么溢出时overflow: auto框的底部有一个小间隙?

Google Apps 脚本 - RegEx 适用于小文本,但不适用于大文件?

您如何动态更改 Vue 中更高级别的标签的 CSS

重点/主动输入的概述问题

防止按钮溢出到新行

在 SQL 中合并 HTML 表中的单元格

如何将我的输入值固定到 2022 年 12 月?

基本上,我想要两列包含文本和图像,但第二列是倒置的

所有幻灯片上的 Quarto RevealJS 标题

在 EJS 中将元素放置在彼此下方的列中