html - CSS "position:fixed"滚动文本重叠问题

标签 html css

在这种情况下如何避免标题文本重叠? 为什么溢出对于上面的部分隐藏而对于下面的部分可见?

演示:https://jsfiddle.net/sftrq1hv/

body {
  margin: 0;
}

section.hero {
  position: relative;
  height: 100vh;
  overflow:hidden;
}

section.hero h1 {
  display: block;
  position: fixed;
  text-align: center;
  width: 100%;
  top: 50%;
}

section :first-child {
  margin-top: 0;
}

.bg1 {
  background: yellow;
}

.bg2 {
  background: cyan;
}

.bg3 {
  background: magenta;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Demo</title>
</head>

<body>
  <section class="hero bg1">
    <h1 class="hero-title">0</h1>
  </section>
  <section class="hero bg2">
    <h1 class="hero-title">1</h1>
  </section>
  <section class="hero bg3">
    <h1 class="hero-title">2</h1>
  </section>
</body>

</html>

最佳答案

应用clip-pathmask来隐藏position:fixed。溢出不适用于 position:fixed

body {
  margin: 0;
}

section.hero {
  position: relative;
  height: 100vh;
  clip-path:polygon(0 0,100% 0,100% 100%,0 100%);
  /*OR
   -webkit-mask:linear-gradient(#fff,#fff);
           mask:linear-gradient(#fff,#fff);
  */
}

section.hero h1 {
  display: block;
  position: fixed;
  text-align: center;
  width: 100%;
  top: 50%;
}

section :first-child {
  margin-top: 0;
}

.bg1 {
  background: yellow;
}

.bg2 {
  background: cyan;
}

.bg3 {
  background: magenta;
}
<section class="hero bg1">
  <h1 class="hero-title">0</h1>
</section>
<section class="hero bg2">
  <h1 class="hero-title">1</h1>
</section>
<section class="hero bg3">
  <h1 class="hero-title">2</h1>
</section>

关于html - CSS "position:fixed"滚动文本重叠问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61021080/

相关文章:

html - 将导航与标题中的 Logo 对齐时出现问题

html - 如何构建对 Angular 英雄部分?

html - 在字段集中的元素顶部显示标签

javascript - 使用for循环多次显示同一个div标签

javascript - flash 横幅在移动和平板设备上不起作用

html - 尝试在 Chrome 与 FF/IE 下为自动高度 div 实现相同的功能

html - 未封闭/错误嵌套的 HTML 标签超出其父标签

javascript - 用于匹配格式 99.99 的数字的正则表达式

html - 为什么我的 css 或 js 不适用于 firefox?

jquery - 实现 CSS 过渡