我试着做一个VN,我试着做一个浮动,因为我需要文本进入两个图像之间,但我认为这会导致按钮在出现时稍微向右移动. 我不断地看到其他方法的推荐,比如Display:Flex;但我似乎不能在这种情况下使用它.

document.addEventListener("DOMContentLoaded", () => {
  let index = 1;
  const button = document.querySelector('button');
  const sections = document.querySelectorAll('section');
  button.addEventListener('click', function() {
    if (sections[index]) {
      if (hasImg(index) && hasImg(index - 1)) {
        sections[index - 1].classList.remove("d-block");
      }
      sections[index].classList.add("d-block");
      typeWriter(sections[index]);
    }
    if (!sections[++index]) {
      button.disabled = true;
    }
  });

  function hasImg(index) {
    return sections[index].querySelector('img');
  }

  typeWriter(sections[0]);
});

function typeWriter(el) {
  const content = el.innerHTML;
  el.innerHTML = '';
  let i = 0;
  const typeInterval = setInterval(() => {
    if (content[i] === '<' && content[i + 1] === 'i' && content[i + 2] === 'm' && content[i + 3] === 'g') {
      let endIdx = content.indexOf('>', i) + 1;
      el.innerHTML += content.substring(i, endIdx);
      i = endIdx;
    } else {
      el.innerHTML += content[i];
      i++;
    }
    if (i >= content.length) {
      clearInterval(typeInterval);
    }
  }, 95);
}
section {
  display: none;
  text-align: center;
}

.d-block {
  display: block;
}

button {
  margin: 0 auto;
  display: block;
}

.portrait {
  animation: fadeIn 5s;
}

@keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

.happy {
  float: left;
}

.sad {
  float: right;
}
<section class="text d-block">First</section>
<section class="text">Second</section>
<section class="text">
  <img class="portrait happy" src="https://cdn.iconscout.com/icon/premium/png-256-thumb/arrow-small-right-4241483-3517873.png">
  <img class="portrait sad" src="https://cdn.iconscout.com/icon/free/png-256/free-small-diamond-geometric-blue-38006.png">
</section>
<section>Las</section>
<section>Ursa</section>
<button>next</button>

推荐答案

我建议避免使用float: leftfloat: right,因为它们给元素的高度为0,这使得不可能防止元素在动态添加过程中 skip .

您可以使用display: flex,也可以指定图像的大小,以防止部分加载图像时出现 skip .

可以这样实现:

document.addEventListener("DOMContentLoaded", () => {
  let index = 1;
  const button = document.querySelector('button');
  const sections = document.querySelectorAll('section');
  button.addEventListener('click', function() {
    if (sections[index]) {
      if (hasImg(index) && hasImg(index - 1)) {
        sections[index - 1].classList.remove("d-block");
      }
      sections[index].classList.add("d-block");
      typeWriter(sections[index]);
    }
    if (!sections[++index]) {
      button.disabled = true;
    }
  });

  function hasImg(index) {
    return sections[index].querySelector('img');
  }

  typeWriter(sections[0]);
});

function typeWriter(el) {
  el = el.querySelector('span') ? el.querySelector('span') : el;
  const content = el.innerHTML;
  el.innerHTML = '';
  let i = 0;
  const typeInterval = setInterval(() => {
    if (content.substring(i, i + 4) === '<img' || content.substring(i, i + 5) === '<span') {
      let endIdx = content.indexOf('>', i) + 1;
      el.innerHTML += content.substring(i, endIdx);
      i = endIdx;
    } else {
      el.innerHTML += content[i];
      i++;
    }
    if (i >= content.length) {
      clearInterval(typeInterval);
    }
  }, 95);
}
section {
  display: none;
  text-align: center;
}

section span {
  align-self: center;
}

.d-block {
  display: flex;
  justify-content: space-between;
}

button {
  margin: 0 auto;
  display: block;
}

.portrait {
  animation: fadeIn 5s;
}

@keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

.happy {
  float: left;
}

.sad {
  float: right;
}

.text {
  justify-content: center;
}
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<section class="text d-block">First</section>
<section class="text">Second</section>
<section>
  <img class="portrait happy" src="https://cdn.iconscout.com/icon/premium/png-256-thumb/arrow-small-right-4241483-3517873.png" width="256" height="256">
  <span>Some text is here</span>
  <img class="portrait sad" src="https://cdn.iconscout.com/icon/free/png-256/free-small-diamond-geometric-blue-38006.png" width="256" height="256">
</section>
<section class="text">Las</section>
<section class="text">Ursa</section>
<button>next</button>

</html>

请让我知道如果这有帮助.

Javascript相关问答推荐

如何提取Cypress中文本

主要内部的ExtJS多个子应用程序

如何使用JavaScript用等效的功能性HTML替换标记URL格式?

我试图实现用户验证的reduxstore 和操作中出了什么问题?

我不知道为什么setwritten包装promise 不能像我预期的那样工作

无法在nextjs应用程序中通过id从mongoDB删除'

如何粗体匹配的字母时输入搜索框使用javascript?

无法检测卡片重叠状态的问题

Websocket错误—有一个或多个保留位开启:reserved1 = 1,reserved2 = 0,reserved3 = 0

如果Arrow函数返回函数,而不是为useEffect返回NULL,则会出现错误

闭包是将值复制到内存的另一个位置吗?

使用getBorbingClientRect()更改绝对元素位置

类构造函数忽略Reaction Native中的可选字段,但在浏览器中按预期工作

如何使用TypeScrip设置唯一属性?

是否可以将异步调用与useState(UnctionName)一起使用

Plotly.js栏为x轴栏添加辅助文本

在每次重新加载页面时更改指针光标

检测带有委托的元素内部的点击,以及元素何时按其类名被选中

使用导航时,路径的所有子组件都必须是路径

如何使用Angular JS双击按钮