Skip to content

Instantly share code, notes, and snippets.

@uguisu-an
Created June 26, 2019 17:12
Show Gist options
  • Save uguisu-an/01aca2e2f8a14164b1132b9c090d90f5 to your computer and use it in GitHub Desktop.
Save uguisu-an/01aca2e2f8a14164b1132b9c090d90f5 to your computer and use it in GitHub Desktop.
動的に@pageを書き換える例
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style id="style-main"></style>
<title>Document</title>
</head>
<body>
<button id="btn-toggle-print-size">
Toggle
</button>
<div style="text-align: center; font-size: 6rem;">
Test
</div>
<script>
const PRINT_SIZE_LANDSCAPE = "landscape";
const PRINT_SIZE_PORTRAIT = "portrait";
const setPageSize = (style, size) => {
style.innerHTML = [
"@page {",
` size: ${size}`,
"}"
].join("\n");
style.dataset.printSize = size;
}
const togglePageSize = (style) => {
if (style.dataset.printSize === PRINT_SIZE_LANDSCAPE) {
setPageSize(style, PRINT_SIZE_PORTRAIT)
} else {
setPageSize(style, PRINT_SIZE_LANDSCAPE);
}
}
document.addEventListener("DOMContentLoaded", () => {
const btn = document.getElementById("btn-toggle-print-size");
btn.addEventListener("click", () => {
const style = document.getElementById("style-main");
togglePageSize(style);
})
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment