英文:
Why won't JS rotating text animation recognize white spaces
问题
我正在尝试修改这个动画 https://codepen.io/FTP-Group/pen/GoEZGZ。我想要显示一个由两个单词组成的短语,而不是一个单词。基本上,它没有识别到空格,而是将这两个单词连在一起(变成了"twowords"而不是"two words")。这里是JS代码:
var words = document.getElementsByClassName('word');
var wordArray = [];
var currentWord = 0;
words[currentWord].style.opacity = 1;
for (var i = 0; i < words.length; i++) {
splitLetters(words[i]);
}
function changeWord() {
var cw = wordArray[currentWord];
var nw = currentWord == words.length - 1 ? wordArray[0] : wordArray[currentWord + 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);
}
currentWord = (currentWord == wordArray.length - 1) ? 0 : currentWord + 1;
}
function animateLetterOut(cw, i) {
setTimeout(function() {
cw[i].className = 'letter out';
}, i * 80);
}
function animateLetterIn(nw, i) {
setTimeout(function() {
nw[i].className = 'letter in';
}, 340 + (i * 80));
}
function splitLetters(word) {
var content = word.innerHTML;
word.innerHTML = '';
var letters = [];
for (var i = 0; i < content.length; i++) {
var letter = document.createElement('span');
letter.className = 'letter';
letter.innerHTML = content.charAt(i);
word.appendChild(letter);
letters.push(letter);
}
wordArray.push(letters);
}
changeWord();
setInterval(changeWord, 2000);
delay(7000);
当使用空格来分隔两个单词时,动画没有将空格识别为字符。我尝试使用不间断空格,但它只会将代码拼写出来。我对JS非常陌生,所以任何帮助将不胜感激。
英文:
I am trying to alter this animation https://codepen.io/FTP-Group/pen/GoEZGZ. I would like to display a two word reveal phrase instead of a single word. It is basically not recognizing a space and smooshes the words together (twowords instead of two words). Here is the JS:
var words = document.getElementsByClassName('word');
var wordArray = [];
var currentWord = 0;
words[currentWord].style.opacity = 1;
for (var i = 0; i < words.length; i++) {
splitLetters(words[i]);
}
function changeWord() {
var cw = wordArray[currentWord];
var nw = currentWord == words.length-1 ? wordArray[0] : wordArray[currentWord+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);
}
currentWord = (currentWord == wordArray.length-1) ? 0 : currentWord+1;
}
function animateLetterOut(cw, i) {
setTimeout(function() {
cw[i].className = 'letter out';
}, i*80);
}
function animateLetterIn(nw, i) {
setTimeout(function() {
nw[i].className = 'letter in';
}, 340+(i*80));
}
function splitLetters(word) {
var content = word.innerHTML;
word.innerHTML = '';
var letters = [];
for (var i = 0; i < content.length; i++) {
var letter = document.createElement('span');
letter.className = 'letter';
letter.innerHTML = content.charAt(i);
word.appendChild(letter);
letters.push(letter);
}
wordArray.push(letters);
}
changeWord();
setInterval(changeWord, 2000);
delay(7000);
When using a space to separate two words, the animation did not recognize the space as a character. I tried using a non-breaking space, but it just spelled out the code. I am very new to JS and any help is greatly appreciated.
答案1
得分: 0
Your JS is fine. You need to adjust the CSS so that the <span>
with the space character has width even without content. For instance, you could add this minimum width to all the letters:
.letter
{
min-width: 20px;
}
For more control over that specific though, it would make sense to define a new CSS class to manage your <span>
elements with spaces, and use JS to add that class to any letter span (when you add the other classes) that doesn't have text content:
.space-span
{
min-width: 20px;
}
if(nw[i].innerText === " ")
nw[i].className = 'letter in space-span';
else
nw[i].className = 'letter in';
Then similar for 'letter out'. One last note: the CSS line display: inline-block;
is what allows you to set this width on <span>
elements, so don't remove that or this won't work.
英文:
Your JS is fine. You need to adjust the CSS so that the <span> with the space character has width even without content. For instance, you could add this minimum width to all the letters:
.letter
{
min-width: 20px;
}
For more control over that specific though, it would make sense to define a new CSS class to manage your <span>
elements with spaces, and use JS to add that class to any letter span (when you add the other classes) that doesn't have text content:
.space-span
{
min-width: 20px;
}
if(nw[i].innerText === " ")
nw[i].className = 'letter in space-span';
else
nw[i].className = 'letter in';
Then similar for 'letter out'. One last note: the CSS line display: inline-block;
is what allows you to set this width on <span>
elements, so don't remove that or this won't work.
答案2
得分: 0
以下是代码中需要翻译的部分:
What to do: <br>将white-space: pre;
添加到.letter
的CSS规则中。
Explanation:<br>
.letter
使用display: inline-block
,对于这个值以及其他一些display
的默认值,前导和尾随空格会被忽略。
这也是为什么像下面的<div>
元素会显示得就像<div>some text here</div>
一样(除非您设置了一些CSS属性的值)。
在您的情况下,<span class="letter"> </span>
最初的显示方式就像<span class="letter"></span>
(即没有空格)。
但如果添加white-space: pre
,那么空格和换行符将不会被忽略,而会像输入一样显示。
英文:
What to do: <br>Add white-space: pre;
to the css rules for .letter
.
Explanation:<br>
.letter
has display: inline-block
, and for this and a few other values of display
, by default the leading and trailing spaces are ignored.
This is also the same reason why a <div>
element like the following:
<div>
some text here
</div>
will be displayed as if it were just <div>some text here</div>
(unless you set the values of some css properties).
In your case <span class="letter"> </span>
, was originally displayed just like <span class="letter"></span>
(i.e. without space).
But if you add white-space: pre
, then the spaces and new line characters will not be ignored, but will be displayed just like the input.
I've copied the code from your codepen here, but I replaced some text and added .white-space: pre
. Also, your delay
is undefined so I commented it out.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var words = document.getElementsByClassName('word');
var wordArray = [];
var currentWord = 0;
words[currentWord].style.opacity = 1;
for (var i = 0; i < words.length; i++) {
splitLetters(words[i]);
}
function changeWord() {
var cw = wordArray[currentWord];
var nw = currentWord == words.length-1 ? wordArray[0] : wordArray[currentWord+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);
}
currentWord = (currentWord == wordArray.length-1) ? 0 : currentWord+1;
}
function animateLetterOut(cw, i) {
setTimeout(function() {
cw[i].className = 'letter out';
}, i*80);
}
function animateLetterIn(nw, i) {
setTimeout(function() {
nw[i].className = 'letter in';
}, 340+(i*80));
}
function splitLetters(word) {
var content = word.innerHTML;
word.innerHTML = '';
var letters = [];
for (var i = 0; i < content.length; i++) {
var letter = document.createElement('span');
letter.className = 'letter';
letter.innerHTML = content.charAt(i);
word.appendChild(letter);
letters.push(letter);
}
wordArray.push(letters);
}
changeWord();
setInterval(changeWord, 2000);
// delay(7000);
<!-- language: lang-css -->
@import url(https://fonts.googleapis.com/css?family=Open+Sans:600);
body {
font-family: 'Open Sans', sans-serif;
font-weight: 600;
font-size: 40px;
}
.text {
position: absolute;
width: 450px;
left: 50%;
margin-left: -225px;
height: 40px;
top: 50%;
margin-top: -20px;
}
p {
display: inline-block;
vertical-align: top;
margin: 0;
}
.word {
position: absolute;
width: 220px;
opacity: 0;
}
.letter {
display: inline-block;
position: relative;
float: left;
transform: translateZ(25px);
transform-origin: 50% 50% 25px;
white-space: pre;
}
.letter.out {
transform: rotateX(90deg);
transition: transform 0.42s cubic-bezier(0.55, 0.055, 0.675, 0.19);
}
.letter.behind {
transform: rotateX(-90deg);
}
.letter.in {
transform: rotateX(0deg);
transition: transform 0.48s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.wisteria {
color: #8e44ad;
}
.belize {
color: #2980b9;
}
.pomegranate {
color: #c0392b;
}
.green {
color: #16a085;
}
.midnight {
color: #2c3e50;
}
<!-- language: lang-html -->
<div class="text">
<p>Nachos are</p>
<p>
<span class="word wisteria">very tasty.</span>
<span class="word belize">very good.</span>
<span class="word pomegranate">very tasty.</span>
<span class="word green">very good.</span>
<span class="word midnight">so great.</span>
</p>
<p></p>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论