简体   繁体   English

在输入框中创建占位符的打字和删除效果动画

[英]create a typing and deleting effect animation of placeholder in an input box

I'm trying to create an animated placeholder for an input box which looks like the placeholder is typed and deleted again (like in https://www.squarespace.com/templates ).我正在尝试为一个输入框创建一个动画占位符,该占位符看起来像是再次输入并删除(如在https://www.squarespace.com/templates 中)。

I tried for a single string but failed to do so for an array, I have commented the part I tried to change.我尝试了单个字符串但未能为数组这样做,我已经评论了我试图更改的部分。 I am new to Javascript:我是 Javascript 的新手:

const words = [
    "Injection",
    "Broken Authentication",
    "Sensitive Data Exposure",
    "XML External Entities (XXE)",
    "Broken Access Control",
    "Security Misconfiguration",
    "Cross-Site Scripting (XSS)",
    "Insecure Deserialization",
    "Using Components with Known Vulnerabilities",
    "Insufficient Logging&Monitoring"
];
let i = 0;
let timer;

function typingEffect() {
    let word = words[i].split("");
    var loopTyping = function () {
        if (word.length > 0) {
            document.getElementById('word').innerHTML += word.shift();
        } else {
            deletingEffect();
            return false;
        };
        timer = setTimeout(loopTyping, 200);
    };
    loopTyping();
};

function deletingEffect() {
    let word = words[i].split("");
    var loopDeleting = function () {
        if (word.length > 0) {
            word.pop();
            // document.getElementById('word').style.font = inherit;
            document.getElementById('word').innerHTML = word.join("");
        } else {
            if (words.length > (i + 1)) {
                i++;
            } else {
                i = 0;
            };
            typingEffect();
            return false;
        };
        timer = setTimeout(loopDeleting, 100);
    };
    loopDeleting();
};

typingEffect();

I wanted this to happen in the placeholder of the input tag.我希望这发生在输入标签的占位符中。 Can you help me please?你能帮我吗?

You can use .setAttribute() and .getAttribute() to set and append to the placeholder text:您可以使用.setAttribute().getAttribute()来设置和附加到占位符文本:

typingEffect():打字效果():

let elem = document.getElementById('typer');
elem.setAttribute('placeholder', elem.getAttribute('placeholder') + word.shift());

deletingEffect():删除效果():

document.getElementById('typer').setAttribute('placeholder', word.join(""));

See example below:请参阅下面的示例:

 const words = ["Injection", "Broken Authentication", "Sensitive Data Exposure", "XML External Entities (XXE)", "Broken Access Control", "Security Misconfiguration", "Cross-Site Scripting (XSS)", "Insecure Deserialization", "Using Components with Known Vulnerabilities", "Insufficient Logging&Monitoring" ]; let i = 0; let timer; function typingEffect() { let word = words[i].split(""); var loopTyping = function() { if (word.length > 0) { let elem = document.getElementById('typer'); elem.setAttribute('placeholder', elem.getAttribute('placeholder') + word.shift()); } else { deletingEffect(); return false; }; timer = setTimeout(loopTyping, 200); }; loopTyping(); }; function deletingEffect() { let word = words[i].split(""); var loopDeleting = function() { if (word.length > 0) { word.pop(); document.getElementById('typer').setAttribute('placeholder', word.join("")); } else { if (words.length > (i + 1)) { i++; } else { i = 0; }; typingEffect(); return false; }; timer = setTimeout(loopDeleting, 100); }; loopDeleting(); }; typingEffect();
 #typer { font-size: 30px; }
 <input type="text" id="typer" placeholder=""></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM