CSS固定位置页脚在打印时重叠。

3
也许有人可以解释一下这里发生了什么,因为我已经在这个问题上思考了一段时间,并且我的网络搜索也没有找到一个实用的答案。下面是一个带有自动生成页码的示例HTML页面:

function pageCount() {
  console.log(document.getElementsByClassName('page').length);
  return document.getElementsByClassName('page').length;
}
document.querySelectorAll(".total").forEach(function(el, i) {
  el.innerHTML = pageCount();
});
.page {
  width: 8in;
  height: 1in;
  padding: 0em;
  margin-top: .5in;
  margin-left: 0in;
  margin-right: 0in;
  margin-bottom: 0.25in;
}

body {
  color: black;
  counter-reset: pagen;
  font-family: "Courier New", Courier, monospace;
  font-size: 10pt;
}

.footer {
  counter-increment: pagen;
  margin: auto;
  width: 8in;
  display: table;
}

#footer-left:before {
  content: "Page " counter(pagen) " of ";
}

.ptable {
  page-break-before: always;
  page-break-inside: avoid;
}

@media print {
  .footer {
    position: fixed;
    bottom: 0;
  }
}
<html>
<head>
  <style></style>
</head>
<body>
  <div class="page">
    <div class="footer">
      <div id="footer-left"><span class="total"></span></div>
    </div>
  </div>
  <div class="page">
    <table class="ptable"></table>
    <div class="footer">
      <div id="footer-left"><span class="total"></span></div>
    </div>
  </div>
  <div class="page">
    <table class="ptable"></table>
    <div class="footer">
      <div id="footer-left"><span class="total"></span></div>
    </div>
  </div>
</body>
<script></script>
</html>

在屏幕上,我们看到我们单独的页脚字段显示页面编号。但是,当您打印它时,所有页脚最终都堆叠在一起,形成一个巨大的混乱。 position: fixed; bottom: 0; 导致页脚元素堆叠在一起,将 fixed 更改为 absolute 表示它们仅在第一页上堆叠,而更改为 relative 只会将页脚移动到内容底部。有关如何纠正此行为的任何建议?测试使用的浏览器是Chrome。


尝试过使用 position: absolute; 吗? - Ajit Panigrahi
您想让页脚固定在底部而不重叠吗? - Rishab Tyagi
@AjitZero 是的,在我的评论底部有。Absolute会导致相同的问题,只限于第1页(第2页和第3页没有使用absolute的页脚)。 - Chaosbydesign
@RishabhdevTyagi 是的,页脚需要跑到页面底部,无论页面上有多少内容,不应该让所有的页脚元素都堆在一起。 - Chaosbydesign
id为footer-left的元素必须是唯一的。 - Rishab Tyagi
我已经添加了一个答案。请检查它。 - Rishab Tyagi
1个回答

1

position:fixed; 是浏览器CSS中定义的。但是你可以覆盖它。这意味着控制台不是网站的一部分。JavaScript在其中运行。您需要为每个浏览器的控制台添加样式。

function pageCount() {
  console.log(document.getElementsByClassName('page').length);
  return document.getElementsByClassName('page').length;
}
document.querySelectorAll(".total").forEach(function(el, i) {
  el.innerHTML = pageCount();
});
.page {
  width: 8in;
  height: 1in;
  padding: 0em;
  margin-top: .5in;
  margin-left: 0in;
  margin-right: 0in;
  margin-bottom: 0.25in;
}

body {
  color: black;
  counter-reset: pagen;
  font-family: "Courier New", Courier, monospace;
  font-size: 10pt;
}

.footer {
  position:fixed;
  margin-top:340px;
  width: 8in;
   
}

#footer-left:before {
  content: "Page " counter(pagen) " of ";
}

.ptable {
  page-break-before: always;
  page-break-inside: avoid;
}

@media print {
  .footer {
    position:fixed;
    bottom: 0;
  }
}
<html>
<head>
  <style></style>
</head>
<body>
  <div class="page">
    <div class="footer">
      <div id="footer-left"><span class="total"></span></div>
    </div>
  </div>
  <div class="page">
    <table class="ptable"></table>
    <div class="footer">
      <div id="footer-left"><span class="total"></span></div>
    </div>
  </div>
  <div class="page">
    <table class="ptable"></table>
    <div class="footer">
      <div id="footer-left"><span class="total"></span></div>
    </div>
  </div>
</body>
<script></script>
</html>


浏览器没有从文档中的任何其他位置获取位置的CSS。所有样式都是内联定义的,因此在@media print下没有position: fixed;,页脚默认为相对定位,并最终出现在每个页面的顶部。 - Chaosbydesign
仍然导致我的本地测试文件中的打印问题。 - Chaosbydesign
请给我提供网站链接。 - Rishab Tyagi
这不是一个网站,而是一个本地页面。请返回首个帖子的修订版本,提取HTML页面的非片段版本,将其转储到文本编辑器中并另存为HTML格式,然后在浏览器中打开。当你进入该页面的打印预览时,你会明白我在说什么。你也可以通过复制片段部分,并将CSS放置到HTML代码中所示的样式标签之间,将脚本放置到底部的脚本标签之间来重新组装它,然后按照上述方法保存并在浏览器中打开。 - Chaosbydesign
它把页脚下移了,会干扰内容。打印时的堆叠问题仍然存在。此外,这个最新的更正现在已经导致页面增量中断,因此每个页面都显示为3的0。目标是不影响页面内容,单独编号的页面(在屏幕上看起来像一个连续的页面),并且打印预览正确地呈现页脚文本在每个页面的底部,没有任何分层或重复。在计数器失效之前备份,现有的打印将在每个页面上将所有页脚层叠在一起。 - Chaosbydesign
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接