13

我有 DIV 标签,里面有文字。是否可以在具有打字效果的循环中更改文本内容,在其中键入,然后向后删除字母并从新文本重新开始?这可以用jquery吗?

4

3 回答 3

38

只是一个简单的方法:

$("[data-typer]").attr("data-typer", function(i, txt) {

  var $typer = $(this),
    tot = txt.length,
    pauseMax = 300,
    pauseMin = 60,
    ch = 0;

  (function typeIt() {
    if (ch > tot) return;
    $typer.text(txt.substring(0, ch++));
    setTimeout(typeIt, ~~(Math.random() * (pauseMax - pauseMin + 1) + pauseMin));
  }());

});
/* PULSATING CARET */
[data-typer]:after {
  content:"";
  display: inline-block;
  vertical-align: middle;
  width:1px;
  height:1em;
  background: #000;
          animation: caretPulsate 1s linear infinite; 
  -webkit-animation: caretPulsate 1s linear infinite; 
}
@keyframes caretPulsate {
  0%   {opacity:1;}
  50%  {opacity:1;}
  60%  {opacity:0;}
  100% {opacity:0;}
}
@-webkit-keyframes caretPulsate {
  0%   {opacity:1;}
  50%  {opacity:1;}
  60%  {opacity:0;}
  100% {opacity:0;}
}
<span data-typer="Hi! My name is Al. I will guide you trough the Setup process."></span>
<h3 data-typer="This is another typing animated text"></h3>

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

所以基本上 jQuery 获取data-text你的元素,逐个字符地插入,而:after那个 SPAN 的 CSS3 动画元素没有什么是脉动的“插入符号”。

另请参阅:在 JavaScript 中生成两个数字之间的随机数

于 2013-02-21T23:20:38.037 回答
0

Jquery 的 text() 方法允许您设置元素的文本。

http://api.jquery.com/text/

您可以通过在循环中调用 this 来使用它来为内容设置动画,并为其提供您希望在每一帧中看到的内容。

var frames = ['t_', 'ty_', 'typ_', 'type_']

// loop over frames ... inner part reads frame and prints it
// use setInterval, etc.
$('#my-div').text( frames[i] );

通过拆分文本元素和操纵字符,我做了更复杂的事情,但我认为在你的情况下这将是矫枉过正。

于 2013-02-21T23:20:36.563 回答
0

打字效果+擦除效果+闪烁光标

我修改了Simon Shahriveri代码盘以进一步添加光标的闪烁效果。它也与 IE 兼容。
这是最终结果: https ://codepen.io/jerrygoyal/pen/vRPpGO

HTML:

<h1>
  <a href="" class="typewrite" data-period="2000" data-type='[ "Hi, I am Jerry.", "I am Creative.", "I Love Design.", "I Love to Develop." ]'>
    <span class="wrap"></span>
  </a>
</h1>

CSS:

body {
  background-color:#ce6670;
  text-align: center;
  color:#fff;
  padding-top:10em;
}

* { color:#fff; text-decoration: none;}

.wrap{
   animation: caret 1s steps(1) infinite;
   border-right: 0.08em solid #fff;
  padding-right: 1px;
}
@keyframes caret {
  50% {
    border-color: transparent;
  }
}

JS:

var TxtType = 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;
    };

    TxtType.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.innerHTML = '<span class="wrap">'+this.txt+'</span>';

        var that = this;
        var delta = 200 - 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() {
        var elements = document.getElementsByClassName('typewrite');
        for (var i=0; i<elements.length; i++) {
            var toRotate = elements[i].getAttribute('data-type');
            var period = elements[i].getAttribute('data-period');
            if (toRotate) {
              new TxtType(elements[i], JSON.parse(toRotate), period);
            }
        }
  
    };

另一种仅使用函数而不是 js 原型的方法:https ://codepen.io/jerrygoyal/pen/vRPpGO

于 2018-04-11T09:09:34.200 回答