Can JavaScript change the value of @page CSS?

前端 未结 2 478
鱼传尺愫
鱼传尺愫 2020-12-02 23:22

The following CSS affects whether a page prints in portrait or landscape by default.

@page {
   size: landscape;
}

I realize that this only

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 00:10

    One simple way is to create a separate style for @page and change it:

    var cssPagedMedia = (function () {
        var style = document.createElement('style');
        document.head.appendChild(style);
        return function (rule) {
            style.innerHTML = rule;
        };
    }());
    
    cssPagedMedia.size = function (size) {
        cssPagedMedia('@page {size: ' + size + '}');
    };
    
    cssPagedMedia.size('landscape');
    

提交回复
热议问题