gpt4 book ai didi

javascript - 如何为每个有大写字母的单词添加不同的颜色?

转载 作者:行者123 更新时间:2023-11-28 15:14:30 25 4
gpt4 key购买 nike

如何为每个以大写字母开头的单词添加不同的颜色?

示例:Justin Creative Design Develope

这是我的代码

 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.lastDeletingStatus=0;
this.isDeleting = 0;
};
var timer;
TxtType.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];

if (this.isDeleting===1) {
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===1) { delta /= 2; }

if (this.isDeleting===0 && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = 1;
} else if (this.isDeleting===1 && this.txt === '') {
this.isDeleting = 0;
this.loopNum++;
delta = 500;
}

if(this.isDeleting!==2){
timer=setTimeout(function() {
that.tick();
}, delta);
}
};

TxtType.prototype.toggleStart=function(){
//start back up
if(this.isDeleting===2){
this.isDeleting=this.lastDeletingStatus;
this.lastDeletingStatus=2;
}
//stop
else{
this.lastDeletingStatus=this.isDeleting;
this.isDeleting=2;
clearTimeout(timer);
}
}
var toggleStart=function(){
txtType.toggleStart();
txtType.tick();
}
var txtType;

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) {
txtType=new TxtType(elements[i], JSON.parse(toRotate), period);
}

}
// INJECT CSS
var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = ".typewrite > .wrap { border-right: 0.08em solid #fff}";
document.body.appendChild(css);
};
body {
background-color:#ce3635;
text-align: center;
color:#fff;
padding-top:10em;
font-family:Helvetica;
}

* { color:#fff; text-decoration: none;}
<div class="type-wrap">
<h2>
<a href="" class="typewrite" data-period="2000" data-type='[ "Hi, my name is Justin.", "I am Creative.", "I love Design with CSS.", "I love to Develop with javascript." ]'>
<span class="wrap"></span></a>
</h2>

</div>

最佳答案

将以下内容添加到 tick()功能:

  var rgx = /\b([A-Z].*?)\b/g;
var rep = `<span class='cap'>$1</span>`;
var fullTxt = fullTxt.replace(rgx, rep);

它的作用是接受字符串 fullTxt并使用 replace()和一个正则表达式,用相同的单词替换任何匹配项(以大写字母开头的单词):

<span class='cap'> </span>

另外不要忘记声明 .capcolor CSS 的属性和值。

演示

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.lastDeletingStatus = 0;
this.isDeleting = 0;
};
var timer;
TxtType.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
var rgx = /\b([A-Z].*?)\b/g;
var rep = `<span class='cap'>$1</span>`;
var fullTxt = fullTxt.replace(rgx, rep);

if (this.isDeleting === 1) {
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 === 1) {
delta /= 2;
}

if (this.isDeleting === 0 && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = 1;
} else if (this.isDeleting === 1 && this.txt === '') {
this.isDeleting = 0;
this.loopNum++;
delta = 500;
}

if (this.isDeleting !== 2) {
timer = setTimeout(function() {
that.tick();
}, delta);
}
};

TxtType.prototype.toggleStart = function() {
//start back up
if (this.isDeleting === 2) {
this.isDeleting = this.lastDeletingStatus;
this.lastDeletingStatus = 2;
}
//stop
else {
this.lastDeletingStatus = this.isDeleting;
this.isDeleting = 2;
clearTimeout(timer);
}
}
var toggleStart = function() {
txtType.toggleStart();
txtType.tick();
}
var txtType;

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) {
txtType = new TxtType(elements[i], JSON.parse(toRotate), period);
}

}
// INJECT CSS
var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = ".typewrite > .wrap { border-right: 0.08em solid #fff}";
document.body.appendChild(css);
};
body {
background-color: #ce3635;
text-align: center;
color: #fff;
padding-top: 10em;
font-family: Helvetica;
}

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

.cap {
color: yellow;
}
<div class="type-wrap">
<h2>
<a href="" class="typewrite" data-period="2000" data-type='[ "Hi, my name is Justin.", "I am Creative.", "I love Design with CSS.", "I love to Develop with javascript." ]'>
<span class="wrap"></span></a>
</h2>

</div>

关于javascript - 如何为每个有大写字母的单词添加不同的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47564569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com