避免单词拆分
Avoid words split
我试图使用一些淡入/淡出效果逐个字母重复显示一些句子。但是,当尝试这样做时,单词似乎在中间中断,如下所示。如何避免断字?
var quotes = document.getElementsByClassName('quote');
var quoteArray = [];
var currentQuote = 0;
quotes[currentQuote].style.opacity = 0;
for (var i = 0; i < quotes.length; i++) {
splitLetters(quotes[i]);
}
function changeQuote() {
var cw = quoteArray[currentQuote];
var nw = currentQuote == quotes.length-1 ? quoteArray[0] : quoteArray[currentQuote+1];
for (var i = 0; i < cw.length; i++) {
animateLetterOut(cw, i);
}
for (var i = 0; i < nw.length; i++) {
nw[i].className = 'letter behind';
nw[0].parentElement.style.opacity = 1;
animateLetterIn(nw, i);
}
currentQuote = (currentQuote == quoteArray.length-1) ? 0 : currentQuote+1;
}
function animateLetterOut(cw, i) {
setTimeout(function() {
cw[i].className = 'letter out';
}, 0);
}
function animateLetterIn(nw, i) {
setTimeout(function() {
nw[i].className = 'letter in';
}, 340+(i*30));
}
function splitLetters(quote) {
var content = quote.innerHTML;
console.log(quote.innerHTML);
quote.innerHTML = '';
var letters = [];
for (var i = 0; i < content.length; i++) {
var letter = document.createElement('span');
letter.className = 'letter';
letter.innerHTML = content.charAt(i)==' '?' ':content.charAt(i);
quote.appendChild(letter);
letters.push(letter);
}
quoteArray.push(letters);
}
changeQuote();
setInterval(changeQuote, 10000);
body {
font-weight: 600;
font-size: 40px;
}
.text {
position: relative;
}
.quote {
position: absolute;
opacity: 0;
}
.letter {
display: inline-block;
position: relative;
float: left;
-webkit-transform: translateZ(25px);
transform: translateZ(25px);
-webkit-transform-origin: 50% 50% 25px;
transform-origin: 50% 50% 25px;
}
.letter.out {
visibility: hidden;
opacity: 0;
transition: visibility 0s 0.7s, opacity 0.7s linear;
}
.letter.behind {
visibility: hidden;
opacity: 0;
}
.letter.in {
visibility: visible;
opacity: 1;
transition: opacity 0.7s linear;
}
<body>
<div class="text">
<p>
<span class="quote">TEXT ONE(1): For example, if you are designing a brand new website for someone, most times you will have to make sure the prototype looks finished by inserting text or photos or what have you. </span>
<span class="quote">TEXT TWO(2): The purpose of this is so the person viewing the prototype has a chance to actually feel and understand the idea behind what you have created.</span>
</p>
</div>
你的setInterval(changeQuote, 5000)
是你巧妙地开发完成短片的效果的来源。最初我开始使用 5000ms,并将其更改为 15000ms 下降到 8000~10000ms 左右似乎是让它工作最好的方法。
将其更改为setInterval(changeQuote, 9000)
并查看差异。
但是,考虑到可伸缩性,您需要找到一种方法来实现它,以便 setInterval 等到quoteArray
完成推送字母。
编辑
根据评论中的反馈,我确定了以下内容:
- 在 JavaScript 方面,每个字母都是一个
<span>
,这意味着每个字母都作为一个单独的元素。所缺乏的是创建一个单词来包裹每个句子。这将确保每个单词将根据其父容器进行换行。
在CSS方面,表示quote
字母的容器需要样式,以使其能够更好地表示其内容。通过添加white-space: nowrap
,display: block
我设法给它的孩子一个可以根据屏幕宽度进行调整的容器。
请参阅从提供的片段中修复的以下代码片段以供参考。
var quotes = document.getElementsByClassName('quote'),
quoteArray = [],
currentQuote = 0;
quotes[currentQuote].style.opacity = 0;
for (var i = 0; i < quotes.length; i++) {
splitLetters(quotes[i]);
}
function changeQuote() {
var cw = quoteArray[currentQuote];
var nw = currentQuote == quotes.length - 1 ? quoteArray[0] : quoteArray[currentQuote + 1];
for (var i = 0; i < cw.length; i++) {
animateLetterOut(cw, i);
}
for (var i = 0; i < nw.length; i++) {
nw[i].className = 'letter behind';
nw[0].parentElement.style.opacity = 1;
animateLetterIn(nw, i);
}
currentQuote = (currentQuote == quoteArray.length - 1) ? 0 : currentQuote + 1;
}
function animateLetterOut(cw, i) {
setTimeout(function() {
cw[i].className = 'letter out';
}, 0);
}
function animateLetterIn(nw, i) {
setTimeout(function() {
nw[i].className = 'letter in';
}, 340 + (i * 30));
}
function splitLetters(quote) {
var content = quote.innerHTML,
words = [],
word = document.createElement('span');
word.className = "word";
word.innerHTML = "";
quote.innerHTML = "";
for (var i = 0; i < content.length; i++) {
var letter = document.createElement('span');
letter.className = 'letter';
if(content.charAt(i) !== " "){
letter.innerHTML = content.charAt(i);
word.innerHTML = word.innerHTML.concat(letter.innerHTML);
}
else {
letter.innerHTML = " ";
word.innerHTML = word.innerHTML.concat(letter.innerHTML);
quote.appendChild(word);
words.push(word);
word = document.createElement('span');
word.className = "word";
}
}
quoteArray.push(words);
}
changeQuote();
setInterval(changeQuote, 10000);
body {
font-weight: 600;
font-size: 40px;
}
.text {
position: relative;
}
.quote {
position: absolute;
display: block;
opacity: 0;
white-space: nowrap;
}
.letter {
display: inline-block;
position: relative;
float: left;
-webkit-transform: translateZ(25px);
transform: translateZ(25px);
-webkit-transform-origin: 50% 50% 25px;
transform-origin: 50% 50% 25px;
}
.letter.out {
visibility: hidden;
opacity: 0;
transition: visibility 0s 0.7s, opacity 0.7s linear;
}
.letter.behind {
visibility: hidden;
opacity: 0;
}
.letter.in {
visibility: visible;
opacity: 1;
transition: opacity 0.7s linear;
}
<div class="text">
<p>
<span class="quote">TEXT ONE(1): For example, if you are designing a brand new website for someone, most times you will have to make sure the prototype looks finished by inserting text or photos or what have you. </span>
<span class="quote">TEXT TWO(2): The purpose of this is so the person viewing the prototype has a chance to actually feel and understand the idea behind what you have created.</span>
</p>
</div>
相关文章:
- 拆分单词jquery
- Javascript 正则表达式在特定单词或数字之后拆分
- 如何在没有javascript中的split函数的情况下将字符串拆分为单词
- 正则表达式,用于从拉丁文中拆分带有重音字符的单词
- 使用DOM将单词拆分为字母的最佳方式
- 如何将单词列表拆分为数组
- 将单词拆分为数组忽略撇号
- HTML,Javascript - 拆分比iFrame宽的单词,而不是生成滚动条
- 将句子字符串拆分为单词数组,然后将单词数组拆分为单词数组中的字符数组
- Javascript 正则表达式,当不在引号中时按单词边界拆分字符串
- 从字符串中拆分单词,数字,并将其作为JavaScript中的2D数组
- 在javascript中将字符串拆分为单词
- 避免单词拆分
- 溢出隐藏的问号将单词拆分为两行
- 将字符串拆分为带空格的单词,除非在一对双引号之间
- 如何在不拆分单词的情况下将字符限制设置为100
- 使用Regex以各种方式拆分单词
- 拆分单词的第一个字符
- Javascript正则表达式在逗号分隔的字符串中拆分单词
- Javascript替换:不要在连字符(正则表达式)上拆分单词