输入动画文字

时间:2013-02-21 23:14:51

标签: jquery animation text typing

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

3 个答案:

答案 0 :(得分:36)

只是一个简单的方法:

$("[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,逐个字符地插入,并且脉冲“插入符号”不是该SPAN的CSS3动画:after元素。

另请参阅:Generate random number between two numbers in JavaScript

答案 1 :(得分:0)

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

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

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

var frames = ['t_','ty_','typ_','type _']

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

我通过拆分文本元素和操纵角色来完成更复杂的事情,但我认为在你的情况下它会有点矫枉过正。

答案 2 :(得分:0)

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

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

<强> HTML:

{
"sentences": [
    {
        "index": 0,
        "tokens": [
            {
                "index": 1,
                "word": "mi",
                "originalText": "mi",
                "lemma": "mi",
                "characterOffsetBegin": 0,
                "characterOffsetEnd": 2,
                "pos": "O",
                "ner": "O"
            },
            {
                "index": 2,
                "word": "chiamo",
                "originalText": "chiamo",
                "lemma": "chiamo",
                "characterOffsetBegin": 3,
                "characterOffsetEnd": 9,
                "pos": "O",
                "ner": "O"
            },
            {
                "index": 3,
                "word": "vincenzo",
                "originalText": "vincenzo",
                "lemma": "vincenzo",
                "characterOffsetBegin": 10,
                "characterOffsetEnd": 18,
                "pos": "O",
                "ner": "NAME"
            },
            {
                "index": 4,
                "word": "monaco",
                "originalText": "monaco",
                "lemma": "monaco",
                "characterOffsetBegin": 19,
                "characterOffsetEnd": 25,
                "pos": "O",
                "ner": "NAME"
            }
        ]
    }
]

<强> CSS:

<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>

<强> JS:

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;
  }
}