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