typing animated text
我有DIV标签,里面有文字。是否可以在一个循环中更改文本内容,并具有键入效果,在该循环中键入内容,然后向后删除字母并从新文本开始?jquery是否可以这样做?
只是一个简单的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $("[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)); }()); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /* 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;} } |
1 2 3 4 | <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"> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> |
所以jquery基本上得到元素的
另请参见:在javascript中生成两个数字之间的随机数
打字效果+擦除效果+闪烁光标
我修改了SimonShahriveriCodepan以进一步增加光标的闪烁效果。它也与IE兼容。最终结果如下:https://codepen.io/jerrygoyal/pen/vrppgo
HTML:
4CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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); } } }; |
jquery的text()方法允许您设置元素的文本。
http://api.jquery.com/text/文本/
您可以通过在循环中调用它来使用它来设置内容的动画,并为它提供您希望在每个帧中看到的内容。
var frames=['t'、'ty'、'typ'、'type']
1 2 3 | // loop over frames ... inner part reads frame and prints it // use setInterval, etc. $('#my-div').text( frames[i] ); |
我已经通过拆分文本元素和操纵字符做了更复杂的事情,但我认为在您的情况下,这将是多余的。