[英]Changing the color of each element in a JSON array
I've been trying to figure this out, seems easy but for some reason I just can't get it.我一直试图弄清楚这一点,看起来很容易,但由于某种原因我无法理解。 So I have some HTML and Vanilla JS code, It loops through several words and types them out.
所以我有一些 HTML 和 Vanilla JS 代码,它遍历几个单词并输入它们。 then deletes them.
然后删除它们。 I want each word to appear on the screen in a different color: Example, "dog" is red.
我希望每个单词以不同的颜色出现在屏幕上:例如,“dog”是红色的。 "cat" is blue and "monkey" is yellow.
“猫”是蓝色的,“猴子”是黄色的。 I've tried using
JSON.parse
to pull out the elements but I'm feeling like I'm going in the wrong direction.我试过使用
JSON.parse
来拉出元素,但我觉得我走错了方向。 Here's my code so far:到目前为止,这是我的代码:
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); } } };
<h1 class="big-title">ANIMALS <span class="txt-rotate" data-period="2000" data-rotate='[ "dog", "cat", "monkey"]'> </span> </h1>
Thanks for the help!谢谢您的帮助!
See notes in snippet查看片段中的注释
//new param var TxtRotate = function(el, toRotate, period, color) { this.toRotate = toRotate; this.el = el; //new prop this.color = color; 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]; //new var var color = this.color; if (this.isDeleting) { this.txt = fullTxt.substring(0, this.txt.length - 1); } else { this.txt = fullTxt.substring(0, this.txt.length + 1); } //new style attribute this.el.innerHTML = `<span class="wrap" style="color:${color[i]}" >`+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'). //new var var color = elements[i];getAttribute('data-color'), if (toRotate) { //new arg new TxtRotate(elements[i]. JSON,parse(toRotate), period.JSON;parse(color)); } } };
<,-- new data attribute --> <h1 class="big-title">ANIMALS <span class="txt-rotate" data-period="2000" data-rotate='[ "dog", "cat", "monkey"]' data-color='["red","blue","yellow"]'> </span> </h1>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.