【问题标题】:How can I add a newline after my "&" characters, instead of the issue shown in my code snippet (adding extra space, not line breaks)?如何在“&”字符后添加换行符,而不是代码片段中显示的问题(添加额外空格,而不是换行符)?
【发布时间】:2020-09-02 19:26:42
【问题描述】:

我正在尝试在三个span 元素中的“&”字符后添加换行符。

具体来说,这里:

<span class="word pomegranate text_animation">creativity &#65286;
 categories</span>
<span class="word wisteria text_animation">form &#65286;
 function</span>
<span class="word belize text_animation">style &#65286;
 systems</span>

在上面的代码中,&amp;#65286 是与号 (&)。我想在这个&符号之后直接创建一个换行符(并尝试使用空格和上面显示的物理换行符)。

我创建了this lovely codepen 的个性化版本,但遇到了问题。我正在使用white-space: pre-wrap; 在每个span 中添加一个换行符,并带有旋转文本。我之所以使用这个特殊的 css 属性是因为我已经尝试过使用 &lt;br&gt;\n\r\n&amp;#9252;&amp;#x2424;&amp;#013;&amp;#010;,但没有运气(应用和不应用 white-space 属性)。我已经设法用下面的 sn-p 重新创建了这个问题。我错过了什么吗?在不使用white-space 属性的情况下,有没有更好的方法来解决这个问题?

TLDR;我无法在span 中的 & 符号后创建换行符。 span 是动画的。

编辑:我在 sn-p 中添加了一些 cmets,以突出显示我认为是导致此问题的代码。请参阅 .js 和 .css sn-ps 顶部的 cmets。

//not necessary to read through all of this, only thing that might be important is that 
//each character in the original span is taken as a single letter for animation purposes
// (see line 25 for change word function)

var creativityAndCategories = document.querySelector('.pomegranate');
var styleAndSystems = document.querySelector('.belize');
var formAndFunction = document.querySelector('.wisteria');

factorForWidth();

function factorForWidth() {
  var mywidth = window.innerWidth;
  if (mywidth < 1170) {}
}

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;
    if (nw.length == 15) {
      if (i < 5) {
        nw[i].style.color = "#d67c5c";
      }
      if (i == 5 || i == 6) {
        nw[i].style.color = "black";
      }
      if (i >= 7) {
        nw[i].style.color = "#71acc1";
      }
    }
    if (nw.length == 23) {
      if (i < 11) {
        nw[i].style.color = "#d67c5c";
      }
      if (i == 11) {
        nw[i].style.color = "black";
      }
      if (i >= 12) {
        nw[i].style.color = "#71acc1";
      }
    }
    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, 5000);
/* ---------------- css that relates to the issue ------------------*/
.word {
  position: absolute;
  width: 100%;
  opacity: 0;
  white-space: pre;
}

.text_animation {
  font-weight: 600 !important;
  margin: 0 !important;
  line-height: 5.944rem;
}


/* ---------------- css you don't have to worry about --------------------- */
/* ------------- but is included for snippet functionality --------------- */

@import url(https://fonts.googleapis.com/css?family=Open+Sans:600);
.invisible_text {
  color: transparent !important;
  line-height: 5.944rem;
  font-weight: 600 !important;
  margin: 0;
}

.rotating_text_container {
  position: relative;
  width: 100%;
}

.my_rotating_text {
  position: absolute;
  width: 100%;
  left: 0;
  height: 400px;
  top: 12px;
}

.kb_rotating {
  line-height: 5.944rem;
}

.kb_text {
  display: block;
  vertical-align: top;
  margin: 0;
  font-weight: 600;
  font-size: 4.833rem;
}

@media screen and (max-width: 990px) {
  .kb_text {
    font-size: 4.278rem;
  }
  .text_animation,
  .kb_rotating,
  .invisible_text {
    line-height: 5.278rem;
  }
}

@media screen and (max-width: 765px) {
  .kb_text {
    font-size: 3.722rem;
  }
  .text_animation,
  .kb_rotating,
  .invisible_text {
    line-height: 4.5rem;
  }
}

@media screen and (max-width: 550px) {
  .kb_text {
    font-size: 3.278rem;
  }
  .text_animation,
  .kb_rotating,
  .invisible_text {
    line-height: 4.5rem;
  }
}

.letter {
  display: inline-block;
  position: relative;
  float: left;
  transform: translateZ(25px);
  transform-origin: 50% 50% 25px;
}

.letter.out {
  transform: rotateX(90deg);
  transition: transform 0.32s cubic-bezier(0.55, 0.055, 0.675, 0.19);
}

.letter.behind {
  transform: rotateX(-90deg);
}

.letter.in {
  transform: rotateX(0deg);
  transition: transform 0.38s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.wisteria {
  color: gray;
}

.belize {
  color: black;
}

.pomegranate {
  color: red;
}

.green {
  color: #16a085;
}

.midnight {
  color: #2c3e50;
}
<div class="rotating_text_container">
  <p class="invisible_text kb_text">Solving problems at the intersection of creativity & categories</p>
  <div class="my_rotating_text">
    <p class="text_animation kb_text">Solving problems at the intersection of</p>
    <p class="kb_text kb_rotating">
      <span class="word pomegranate text_animation">creativity &#65286;
        categories</span>
      <span class="word wisteria text_animation">form &#65286; 
        function</span>
      <span class="word belize text_animation">style &#65286;
        systems</span>
    </p>
  </div>
</div>

【问题讨论】:

  • 你能把这个减少到证明问题所需的最低限度吗?

标签: javascript html css newline


【解决方案1】:

white-space: pre 保留所有空白。额外的空格是因为您在换行符之后缩进了单词。 `

使用white-space: pre-line 保留换行符,但折叠其他空格。

var creativityAndCategories = document.querySelector('.pomegranate');
var styleAndSystems = document.querySelector('.belize');
var formAndFunction = document.querySelector('.wisteria');

factorForWidth();

function factorForWidth() {
  var mywidth = window.innerWidth;
  if (mywidth < 1170) {}
}

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;
    if (nw.length == 15) {
      if (i < 5) {
        nw[i].style.color = "#d67c5c";
      }
      if (i == 5 || i == 6) {
        nw[i].style.color = "black";
      }
      if (i >= 7) {
        nw[i].style.color = "#71acc1";
      }
    }
    if (nw.length == 23) {
      if (i < 11) {
        nw[i].style.color = "#d67c5c";
      }
      if (i == 11) {
        nw[i].style.color = "black";
      }
      if (i >= 12) {
        nw[i].style.color = "#71acc1";
      }
    }
    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, 5000);
@import url(https://fonts.googleapis.com/css?family=Open+Sans:600);
.invisible_text {
  color: transparent !important;
  line-height: 5.944rem;
  font-weight: 600 !important;
  margin: 0;
}

.rotating_text_container {
  position: relative;
  width: 100%;
}

.my_rotating_text {
  position: absolute;
  width: 100%;
  left: 0;
  height: 400px;
  top: 12px;
}

.kb_rotating {
  line-height: 5.944rem;
}

.kb_text {
  display: block;
  vertical-align: top;
  margin: 0;
  font-weight: 600;
  font-size: 4.833rem;
}

.text_animation {
  font-weight: 600 !important;
  margin: 0 !important;
  line-height: 5.944rem;
}

@media screen and (max-width: 990px) {
  .kb_text {
    font-size: 4.278rem;
  }
  .text_animation,
  .kb_rotating,
  .invisible_text {
    line-height: 5.278rem;
  }
}

@media screen and (max-width: 765px) {
  .kb_text {
    font-size: 3.722rem;
  }
  .text_animation,
  .kb_rotating,
  .invisible_text {
    line-height: 4.5rem;
  }
}

@media screen and (max-width: 550px) {
  .kb_text {
    font-size: 3.278rem;
  }
  .text_animation,
  .kb_rotating,
  .invisible_text {
    line-height: 4.5rem;
  }
}

.word {
  position: absolute;
  width: 100%;
  opacity: 0;
  white-space: pre-line;
}

.letter {
  display: inline-block;
  position: relative;
  float: left;
  transform: translateZ(25px);
  transform-origin: 50% 50% 25px;
}

.letter.out {
  transform: rotateX(90deg);
  transition: transform 0.32s cubic-bezier(0.55, 0.055, 0.675, 0.19);
}

.letter.behind {
  transform: rotateX(-90deg);
}

.letter.in {
  transform: rotateX(0deg);
  transition: transform 0.38s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.wisteria {
  color: gray;
}

.belize {
  color: black;
}

.pomegranate {
  color: red;
}

.green {
  color: #16a085;
}

.midnight {
  color: #2c3e50;
}
<div class="rotating_text_container">
  <p class="invisible_text kb_text">Solving problems at the intersection of creativity & categories</p>
  <div class="my_rotating_text">
    <p class="text_animation kb_text">Solving problems at the intersection of</p>
    <p class="kb_text kb_rotating">
      <span class="word pomegranate text_animation">creativity &#65286;
        categories</span>
      <span class="word wisteria text_animation">form &#65286; 
        function</span>
      <span class="word belize text_animation">style &#65286;
        systems</span>
      <!--<span class="word green">beautiful.</span>
      <span class="word midnight">cheap.</span>-->
    </p>
  </div>
</div>

【讨论】:

  • 这会在浏览器中的“&”字符后创建一个换行符吗?这就是我要找的东西,如果我的问题不清楚,我深表歉意(我会添加它并消除臃肿)
  • 标题中提到了多余的空格。问题本身并不清楚您要解决什么问题,所以我只是解决了标题。
【解决方案2】:

对于遇到这个问题的其他人来说,我想通了:

不要使用换行符,因为每个单独的字母都是span,您应该将新行中所需的span 设置为display: block;display: block; span 之后和之前的每个跨度都应设置为display: inline-block;(如果您正在寻找如下所示的输出)。我将包含代码 sn-p 作为示例,其中&amp; 之后的单词在窗口大小低于 900 像素后位于换行符上。

另外,对于这个方法,我根本没有使用white-space 属性。

 var creativityAndCategories = document.querySelector('.pomegranate');
  var styleAndSystems = document.querySelector('.belize');
  var formAndFunction = document.querySelector('.wisteria');
  var invisibleText = document.querySelector('.invisible_text');
  var width;
  window.addEventListener("resize", factorWidth);
  factorWidth();
  function factorWidth(){
    width = window.innerWidth;
  }
  
  
  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;
      if(nw.length == 19 && nw[0].innerHTML == "s"){
      if(i < 5){
        nw[i].style.color = "#d67c5c";
      }
      if(i == 9){
        nw[i].style.color = "black";
      }
      if(i == 11 && width < 900){
        nw[i].style.display = "block";
        invisibleText.innerHTML = "Solving problems at the intersection of<br>creativity &<br>categories";
      }
        if(i == 11 && width > 900){
        nw[i].style.display = "inline-block";
        invisibleText.innerHTML = "Solving problems at the intersection of<br>creativity & categories";
      }
      if(i >= 10){
        nw[i].style.color = "#71acc1";
      }
      }
    if(nw.length == 19 && nw[0].innerHTML == "f"){
      if(i < 5){
        nw[i].style.color = "#d67c5c";
      }
      if(i == 7){
        nw[i].style.color = "black";
      }
      if(i == 10 && width < 900){
        nw[i].style.display = "block";
        invisibleText.innerHTML = "Solving problems at the intersection of<br>creativity &<br>categories";
      }
      if(i == 10 && width > 900){
        nw[i].style.display = "inline-block";
        invisibleText.innerHTML = "Solving problems at the intersection of<br>creativity & categories";
      }
      
      if(i >= 11){
        nw[i].style.color = "#71acc1";
      }
    }
      if(nw.length == 27){
        if(i < 11){
          nw[i].style.color = "#d67c5c";
        }
        if(i == 13){
          nw[i].style.color = "black";
        }
      if(i == 16 && width < 900){
        nw[i].style.display = "block";
        invisibleText.innerHTML = "Solving problems at the intersection of<br>creativity &<br>categories";
      }
        if(i == 16 && width > 900){
        nw[i].style.display = "inline-block";
        invisibleText.innerHTML = "Solving problems at the intersection of<br>creativity & categories";
      }
        if(i >= 14){
          nw[i].style.color = "#71acc1";
        }
      }
      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, 5000);
  @import url(https://fonts.googleapis.com/css?family=Open+Sans:600);

  
  .invisible_text{
    color: transparent !important;
    line-height: 5.944rem;
    font-weight: 600 !important;
    margin: 0;
  }
  
  .rotating_text_container{
    position: relative;
    width: 100%;
  }

  .my_rotating_text {
    position: absolute;
    width: 100%;
    left: 0;
    height: 400px;
    top: 12px;
  }
  
  .kb_rotating{
    line-height: 5.944rem;
  }

   .kb_text{
    display: block;
    vertical-align: top;
    margin: 0;
    font-weight: 600;
    font-size: 4.833rem;
  }
  
  .text_animation{
    font-weight: 600 !important;
    margin: 0 !important;
    line-height: 5.944rem;
  }
  
  @media screen and (max-width: 990px){
    .kb_text{
      font-size: 4.278rem;
    }
    .text_animation, .kb_rotating, .invisible_text{
      line-height: 5.278rem;
    }
  }
  @media screen and (max-width: 765px){
    .kb_text{
      font-size: 3.722rem;
    }
    .text_animation, .kb_rotating, .invisible_text{
      line-height: 4.5rem;
    }
  }
  @media screen and (max-width: 550px){
    .kb_text{
      font-size: 3.278rem;
    }
    .text_animation, .kb_rotating, .invisible_text{
      line-height: 4.5rem;
    }
  }


  .word {
    position: absolute;
  display: block;
    width: 100%;
    opacity: 0;
  }

  .letter {
    display: inline-block;
    position: relative;
    transform: translateZ(25px);
    transform-origin: 50% 50% 25px;
  }

  .letter.out {
    transform: rotateX(90deg);
    transition: transform 0.32s cubic-bezier(0.55, 0.055, 0.675, 0.19);
  }

  .letter.behind {
    transform: rotateX(-90deg);
  }

  .letter.in {
    transform: rotateX(0deg);
    transition: transform 0.38s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  }

  .wisteria {
    color: gray;
  }

  .belize {
    color: black;
  }

  .pomegranate {
    color: transparent;
  }
  .green {
    color: #16a085;
  }

  .midnight {
    color: #2c3e50;
  }
<div class="rotating_text_container"> 
  <p class="invisible_text kb_text">Solving problems at the intersection of<br>creativity & categories</p>
  <div class="my_rotating_text">
    <p class="text_animation kb_text">Solving problems at the intersection of</p>
    <p class="kb_text kb_rotating">
      <span class="word pomegranate text_animation">creativity &#8239; &#65286; &#8239; categories</span>
      <span class="word wisteria text_animation">form &#8239; &#65286; &#8239; function</span>
      <span class="word belize text_animation">style &#8239; &#65286; &#8239; systems</span>
      <!--<span class="word green">beautiful.</span>
      <span class="word midnight">cheap.</span>-->
    </p>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 2010-12-30
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多