JS旋转文本动画为什么不识别空格

huangapple go评论100阅读模式
英文:

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(&#39;word&#39;);
var wordArray = [];
var currentWord = 0;
words[currentWord].style.opacity = 1;
for (var i = 0; i &lt; 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 &lt; cw.length; i++) {
animateLetterOut(cw, i);
}
for (var i = 0; i &lt; nw.length; i++) {
nw[i].className = &#39;letter behind&#39;;
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 = &#39;letter out&#39;;
}, i*80);
}
function animateLetterIn(nw, i) {
setTimeout(function() {
nw[i].className = &#39;letter in&#39;;
}, 340+(i*80));
}
function splitLetters(word) {
var content = word.innerHTML;
word.innerHTML = &#39;&#39;;
var letters = [];
for (var i = 0; i &lt; content.length; i++) {
var letter = document.createElement(&#39;span&#39;);
letter.className = &#39;letter&#39;;
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 &lt;span&gt; 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 === &quot; &quot;)
  nw[i].className = &#39;letter in space-span&#39;;
else
  nw[i].className = &#39;letter in&#39;;

Then similar for 'letter out'. One last note: the CSS line display: inline-block; is what allows you to set this width on &lt;span&gt; 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 &lt;div&gt; element like the following:

&lt;div&gt;
some text here
&lt;/div&gt;

will be displayed as if it were just &lt;div&gt;some text here&lt;/div&gt; (unless you set the values of some css properties).

In your case &lt;span class=&quot;letter&quot;&gt; &lt;/span&gt;, was originally displayed just like &lt;span class=&quot;letter&quot;&gt;&lt;/span&gt; (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(&#39;word&#39;);
var wordArray = [];
var currentWord = 0;
words[currentWord].style.opacity = 1;
for (var i = 0; i &lt; 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 &lt; cw.length; i++) {
animateLetterOut(cw, i);
}
for (var i = 0; i &lt; nw.length; i++) {
nw[i].className = &#39;letter behind&#39;;
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 = &#39;letter out&#39;;
}, i*80);
}
function animateLetterIn(nw, i) {
setTimeout(function() {
nw[i].className = &#39;letter in&#39;;
}, 340+(i*80));
}
function splitLetters(word) {
var content = word.innerHTML;
word.innerHTML = &#39;&#39;;
var letters = [];
for (var i = 0; i &lt; content.length; i++) {
var letter = document.createElement(&#39;span&#39;);
letter.className = &#39;letter&#39;;
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: &#39;Open Sans&#39;, 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 -->

&lt;div class=&quot;text&quot;&gt;
&lt;p&gt;Nachos are&lt;/p&gt;
&lt;p&gt;
&lt;span class=&quot;word wisteria&quot;&gt;very tasty.&lt;/span&gt;
&lt;span class=&quot;word belize&quot;&gt;very good.&lt;/span&gt;
&lt;span class=&quot;word pomegranate&quot;&gt;very tasty.&lt;/span&gt;
&lt;span class=&quot;word green&quot;&gt;very good.&lt;/span&gt;
&lt;span class=&quot;word midnight&quot;&gt;so great.&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 23:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定