简体   繁体   English

<span>不显示包含单个空格的</span>内联块

[英]Inline-block <span> containing a single space is not displaying

I have an animated <h1> in which the subtitle flies in from the right, letter by letter. 我有一个动画<h1> ,其中副标题从右边逐字逐句飞过。

The subtitle is divided into 2 parts. 副标题分为两部分。

  • In the first part (before the bullet), I wish to eradicate all the spaces. 在第一部分(在子弹之前),我希望根除所有空间。

  • In the second part (after the bullet), I wish to retain all the spaces. 在第二部分(子弹之后),我希望保留所有空间。

Curiously though, as the letters and spaces fly in, the on-screen spaces are invisible . 奇怪的是,随着字母和空格的飞入,屏幕上的空间是不可见的

Where there are spaces, I want to see the letters visibly separated by those spaces as they fly in. 在有空间的地方,我希望看到这些空间在飞入时明显分开的字母。

The spaces do exist in the DOM - you can see <span> </span> in the DOM Inspector in both Chrome and Firefox. 这些空间确实存在于DOM中 - 您可以在Chrome和Firefox的DOM Inspector中看到<span> </span> But, despite having a display style of 但是,尽管有显示风格

display: inline-block;

the spaces show up as zero width on the screen. 空格在屏幕上显示为零宽度。 (And they are grayed-out even in Firebug). (即使在Firebug中它们也是灰色的)。

Here is the jsFiddle : https://jsfiddle.net/2u6uLn7d/ 这是jsFiddlehttps//jsfiddle.net/2u6uLn7d/

And here is a working example: 这是一个有效的例子:

 var subtitle = document.getElementsByClassName('subtitle')[0]; subtitle.style.opacity = '0'; function activateSubtitle() { if (subtitle.textContent.indexOf('\•') !== -1) { var bulletIndex = (subtitle.textContent.indexOf('\•') - 1); var beforeBullet = subtitle.textContent.substr(0,bulletIndex); var afterBullet = subtitle.textContent.substr(bulletIndex); beforeBullet = beforeBullet.replace(/ /g, ''); subtitle.textContent = beforeBullet + afterBullet; } else {subtitle.textContent = subtitle.textContent.replace(/ /g, '');} var subtitleLength = subtitle.textContent.length; var heading = subtitle.parentNode; subtitle.style.opacity = ''; subtitle.style.position = 'fixed'; heading.classList.add('stilts'); for (var i = 0; i < subtitleLength; i++) { subtitle.innerHTML += '<span>' + subtitle.textContent[i] + '</span>'; } subtitle.innerHTML = subtitle.innerHTML.substr(subtitleLength); var eachLetter = 50; var allLetters = ((subtitleLength + 3) * eachLetter); var i = 0; var spans = subtitle.getElementsByTagName('span'); var enterSubtitle = setInterval(function(){ if (i > (subtitleLength - 2)) {clearInterval(enterSubtitle);} spans[i].style.WebkitTransform = 'translate(0,0)'; spans[i].style.transform = 'translate(0,0)'; i++; },eachLetter); setTimeout(function(){ for (var i = 0; i < subtitleLength; i++) { var letter = spans[0].childNodes[0]; subtitle.insertBefore(letter,spans[0]); subtitle.removeChild(spans[0]); } subtitle.removeAttribute('style'); heading.classList.remove('stilts'); if (heading.getAttribute('class') === '') { heading.removeAttribute('class');} },allLetters); } window.addEventListener('load',activateSubtitle,false); 
 h1 { font-size: 72px; font-weight: normal; } h1 .subtitle { display: block; font-size: 36px; } .subtitle span { display: inline-block; transform: translate(100vw,0); transition: all .2s ease-in; } 
 <h1><strong>Page</strong> Heading <span class="subtitle">Subtitle Part 1 &bull; Part Two of the Subtitle</span> </h1> 

NB I should probably add, if you want to slow the animation down so that you can examine it in a DOM Inspector, change the line NB 我应该添加,如果你想减慢动画速度,以便你可以在DOM Inspector中检查它,更改行

var eachLetter = 50; /* 0.05 seconds */

to something much higher, like: 更高的东西,比如:

var eachLetter = 1500; /* 1.5 seconds */

To prevent whitespace collapsing you can use css white-space property: https://developer.mozilla.org/en/docs/Web/CSS/white-space 要防止空格崩溃,可以使用css white-space属性: https//developer.mozilla.org/en/docs/Web/CSS/white-space

Here is table of available values from link above: 以下是上面链接中的可用值表:

Value       New lines       Spaces and tabs     Text wrapping
==============================================================
normal      Collapse        Collapse            Wrap
nowrap      Collapse        Collapse            No wrap
pre         Preserve        Preserve            No wrap
pre-wrap    Preserve        Preserve            Wrap
pre-line    Preserve        Collapse            Wrap

In your case suitable values are pre and pre-wrap : 在您的情况下,合适的值是prepre-wrap

.subtitle span {
  display: inline-block;
  transform: translate(100vw,0);
  transition: all .2s ease-in;
  white-space: pre;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM