我使用这个打字传送带效果我从codepen获得了我的网站,但是,我需要帮助修改它。现在,它所做的只是在一个单词列表中循环并输入它们。
我想要的效果是,输入的每个单独单词的文本颜色是不同的。单词:
都会有不同的颜色。我怎么能做到这一点?
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.innerHTML = '<span class="wrap">' + this.txt + '</span>';
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() {
var elements = document.getElementsByClassName('txt-rotate');
for (var i = 0; i < elements.length; i++) {
var toRotate = elements[i].getAttribute('data-rotate');
var period = elements[i].getAttribute('data-period');
if (toRotate) {
new TxtRotate(elements[i], JSON.parse(toRotate), period);
}
}
// INJECT CSS
var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid #666 }";
document.body.appendChild(css);
};
&#13;
html,
body {
font-family: 'Raleway', sans-serif;
padding: 1em 2em;
font-size: 18px;
background: #222;
color: #aaa
}
h1,
h2 {
font-weight: 200;
margin: 0.4em 0;
}
h1 {
font-size: 3.5em;
}
h2 {
color: #888;
font-size: 2em;
}
&#13;
<link href="https://fonts.googleapis.com/css?family=Raleway:200,100,400" rel="stylesheet" type="text/css" />
<h1>This pen is
<span class="txt-rotate" data-period="2000" data-rotate='[ "nerdy.", "simple.", "pure JS.", "pretty.", "fun!" ]'></span>
</h1>
<h2>A single <span> is all you need.</h2>
&#13;
答案 0 :(得分:2)
您可以通过定义颜色数组来完成此操作:
var colours = ["red", "green", "blue", "yellow", "orange"]
并将其设置为每个循环:
this.el.innerHTML = '<span class="wrap" style="color: ' + colours[i] + '">'+this.txt+'</span>';
答案 1 :(得分:0)
一种可能的解决方案是添加另一个数据属性,例如数据颜色,然后使用它们旋转以及文本。如果颜色少于文本,使用模数函数将允许您包围颜色。因此,您可以有14个文本条目,但只有两种颜色,因此颜色将在两种颜色之间交替。
我使用您可以使用的非常快速的实现来自定义codepen,但这是我更改内容的基础:
<强> HTML 强>
<span
class="txt-rotate"
data-period="2000"
data-rotate='[ "nerdy.", "simple.", "pure JS.", "pretty.", "fun!" ]'
data-colors='[ "red", "blue", "green" ]'></span>
<强>的Javascript 强>:
window.onload = function() {
var elements = document.getElementsByClassName('txt-rotate');
for (var i=0; i<elements.length; i++) {
var toRotate = elements[i].getAttribute('data-rotate');
var period = elements[i].getAttribute('data-period');
var colors = elements[i].getAttribute('data-colors');
if (toRotate) {
new TxtRotate(elements[i], JSON.parse(toRotate), JSON.parse(colors), period);
}
}
......很多不变的代码......
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
var thisColor = this.colors[this.loopNum % this.colors.length];
... other unchanged code ...
this.el.innerHTML = '<span class="wrap" style="color:'+thisColor+'">'+this.txt+'</span>';
答案 2 :(得分:-1)
这是更新的javascript,它将提供您想要的输出。
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;
};
var colors = ['red','blue','green']; // Array of colours
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.innerHTML = '<span class="wrap" style="color:'+colors[i]+'">'+this.txt+'</span>'; // Changing colours depending on word's index with colors.
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() {
var elements = document.getElementsByClassName('txt-rotate');
for (var i=0; i<elements.length; i++) {
var toRotate = elements[i].getAttribute('data-rotate');
var period = elements[i].getAttribute('data-period');
if (toRotate) {
new TxtRotate(elements[i], JSON.parse(toRotate), period);
}
}
// INJECT CSS
var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid #666 }";
document.body.appendChild(css);
};