我希望我的输入占位符有一个输入轮播,类似于 this
我想要那个dynamic writing在我的 #myInput
占位符上
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input">
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
关于如何完成此操作的任何想法?谢谢
最佳答案
您实际上只需要用您的输入元素和文本创建一个新的 TxtRotate
对象。
new TxtRotate(element, textArray, waitTimeMs);
然后,要在占位符中获取文本,只需在元素上设置占位符文本即可。
this.el.placeholder = this.txt;
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.placeholder = this.txt;
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) {
delta /= 2;
}
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
new TxtRotate(document.getElementById('myInput'), ["PariMaria", "GentianAnnas", "LinneaSteliana", "UlisesCristobal", "SimonidesLonny"], 2000);
};
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input">
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
关于javascript - 如何在输入占位符内制作打字轮播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45617439/