打印網(wǎng)頁時(shí),在保證打印效果的前提下,前端要怎么寫呢
1.在打印時(shí)調(diào)用的樣式(px--->pt(打印時(shí)使用pt)、樣式表、媒體查詢)
//打印時(shí)example.css生效
<link href="/example.css" media="print" rel="stylesheet" />
//媒體查詢
h1 {
font-size: 14px;
}
//打印時(shí)的樣式
@media print {
h1 {
font-size: 20px;
}
}
用js控制
window.matchMedia 測(cè)試媒體查詢接口
function myFunction(x) {
if (x.matches) { // 媒體查詢
document.body.style.backgroundColor = "yellow";
} else {
document.body.style.backgroundColor = "block";
}
}
var x = window.matchMedia("(max-width: 700px)")
myFunction(x) // 執(zhí)行時(shí)調(diào)用的監(jiān)聽函數(shù)
x.addListener(myFunction) // 狀態(tài)改變時(shí)添加監(jiān)聽器
2.手動(dòng)調(diào)用打印方法
window.print()
1.window.print()
或
2.document.execCommand(”print”)