简体   繁体   English

如何在@media打印查询中更改CSS?

[英]How to change the css inside a @media print query?

When the contents of a div are too large I need to scale them before printing. 当div的内容太大时,我需要在打印之前缩放它们。 I've followed the helpful advice here and set up printing stylesheets. 我在这里遵循了有用的建议,并设置了打印样式表。 When it comes time to print I find I need to change some of the css inside the @media print query. 当需要打印时,我发现我需要更改@media打印查询中的某些CSS。 Instead of changing the class in the media query, I find a regular CSS selector makes the changes to the div on the screen and not in the print query. 我没有在媒体查询中更改类,而是找到了常规的CSS选择器,在屏幕上而不是在打印查询中对div进行了更改。 Is there a selector I can use to make the changes to the @media print css only? 是否可以使用选择器仅对@media print css进行更改?

Example here: Hitting the 'Change Scale' button immediately zooms the image. 此处的示例:点击“更改比例”按钮可立即缩放图像。 I want the changed scale to only apply upon printout. 我希望更改的比例仅适用于打印输出。

 var canvas = document.getElementById("canvas1"); function draw_a() { var context = canvas.getContext("2d"); // // LEVER //plane context.fillStyle = '#aaa'; context.fillRect (25, 90, 2500, 400); } function changeScale() { var scale = .1; console.log("scale:" + scale); $('.myDivToPrint').css({ '-webkit-transform': 'scale(' + scale + ')', '-moz-transform': 'scale(' + scale + ')', '-ms-transform': 'scale(' + scale + ')', '-o-transform': 'scale(' + scale + ')', 'transform': 'scale(' + scale + ')', }); } $(document).ready(function(){ draw_a(); }); 
 div.sizePage { color: #333; } canvas { border: 1px dotted; } .printOnly { display: none; } @media print { html, body { height: 100%; background-color: yellow; } .myDivToPrint { background-color: yellow; top: 0; left: 0; margin: 0; } .no-print, .no-print * { display: none !important; } .printOnly { display: block; } } @media print and (-ms-high-contrast: active) and (-ms-high-contrast: none) { html, body { height: 100%; background-color: pink; } .myDivToPrint { background-color: yellow; /* -moz-transform: rotate(90deg) scale(0.3); -ms-transform: rotate(90deg) scale(0.3); -o-transform: rotate(90deg) scale(0.3); -webkit-transform: rotate(90deg) scale(0.3); transform: rotate(90deg) scale(0.3); /* Standard Property */ position: fixed; top: 0; left: 0; margin: 0; padding: 15px; font-size: 14px; line-height: 18px; position: absolute; display: flex; align-items: center; justify-content: center; /* -webkit-transform: rotate(90deg); -moz-transform: rotate(90deg); -o-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg); */ } .no-print, .no-print * { display: none !important; } .printOnly { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="window.print();" class="no-print">Print Canvas</button> <button onclick="changeScale();" class="no-print>">Change Scale</button> <div class="myDivToPrint"> <canvas height="2500px" width="4000px" id="canvas1"></canvas> </div> 

Thanks, Matt 谢谢,马特

If I understand you correctly. 如果我正确理解您的意思。 You would you like to edit the Media Query itself. 您想编辑媒体查询本身。 In which case here are a few examples. 在这种情况下,这里有一些示例。 jQuery change media query width value jQuery更改媒体查询宽度值

$('body').append("<style type='text/css'>@media screen and (min-width: 500px) { /*whatever*/ }</style>");

You can also use window.MatchMedia() as show in the sample below! 您还可以使用window.MatchMedia(),如下面的示例所示! https://codepen.io/ravenous/pen/BgGKA https://codepen.io/ravenous/pen/BgGKA

You can just update an existing element (or create one) textContent to contain the rule, which will make it effective in the given context. 您可以只更新一个现有元素(或创建一个)textContent来包含规则,这将使其在给定上下文中有效。 Generating CSS media queries with javascript or jQuery 使用JavaScript或jQuery生成CSS媒体查询

document.querySelector('style').textContent +=
    "@media screen and (min-width:400px) { div { color: red; }}"

There is also a Library which allows the mixture of CSS and jQuery specific for this purpose. 还有一个库,允许为此目的专门混合使用CSS和jQuery。 http://www.learningjquery.com/2017/03/how-to-use-media-queries-with-jquery http://www.learningjquery.com/2017/03/how-to-use-media-queries-with-jquery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM