欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

js打印纸函数代码(递归)_javascript技巧

程序员文章站 2022-04-06 21:49:20
...
复制代码 代码如下:

//打印纸尺寸,单位MM
//http://en.wikipedia.org/wiki/ISO_216
var page = function() {
var A0 = { name: "A0", width: 841, height: 1189 }; //单位MM
var B0 = { name: "B0", width: 1000, height: 1414 }; //单位MM
var C0 = { name: "C0", width: 917, height: 1297 }; //单位MM
//获取前一个规格
function getPrefixSize(name) {
var list = name.split('');
var series = list[0];
var number = parseInt(list[1]);
if (!isNaN(number) && number > 0) {
return series + (number - 1);
}
}
//获取规格的尺寸
function getSize(name) {
if (name == "C7/6") {
return { name: "C7/6", width: 81, height: 162 };
}
var list = name.split('');
var series = list[0];
var number = parseInt(list[1]);
if (isNaN(number)) {
return;
}
if (number == 0) {
if (series == "A") { return A0; }
else if (series == "B") { return B0; }
else if (series == "C") { return C0; }
}
return { name: name, width: parseInt(getSize(getPrefixSize(name)).height / 2), height: parseInt(getSize(getPrefixSize(name)).width) };
}
return {
getSize: getSize
}
} ();

用法
复制代码 代码如下:

page.getSize('A4')