javascript - 如何在输入占位符内制作打字轮播?

标签 javascript jquery css placeholder

我希望我的输入占位符有一个输入轮播,类似于 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/

相关文章:

javascript - Animate.css 摇动效果每次都不起作用

javascript - 使用纯js/跨浏览器解决方案检查元素中是否存在伪类(:after, :before, ...)

javascript - 在 Angular 2 中处理 CometD channel 不起作用

php - 什么时候使用 document.write()?

jquery - Backbone 未将事件绑定(bind)到 jQuery Popover

jQuery 验证框架 + qTip 在提交时未触发

javascript - 动态添加和删除行

javascript - Wordpress 主题中古怪的 JS 代码

javascript - 在 React Native 中的两个组件之间传递数据

javascript - 在 Chrome 中将光标属性重置为 "grab"不起作用