-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
180 lines (159 loc) · 8.31 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>px/rpx互转工具</title>
<meta name="author" content="{name:'allanwang',site:'allanguys.github.io'}" />
<meta name="Description" content="px/rpx互转工具" />
<meta name="Keywords" content="px,rpx,微信小程序,小程序,样式压缩,tgideas,px转换rpx,在线工具" />
<link rel="stylesheet" href="https://game.gtimg.cn/images/css/bootstrap/3/bootstrap.min.css">
<link rel="stylesheet" href="https://game.gtimg.cn/images/css/bootstrap/3/bootstrap-theme.min.css">
<style type="text/css">
/*当前页面样式*/
.convert_option:after{content:'\20';display:block;height:0;clear:both;}
body {font-family: "font-family:\5FAE\8F6F\96C5\9ED1;";}
ul {list-style: none;padding-left:10px;}
h4{margin-bottom:10px}
.container{padding:10px 0 0;width:1000px}
.alert-info{padding-left: 0}
.alert-danger,.alert-success{position:fixed;top:0;left:0;width:100%;text-align:center; }
textarea {width:100%;height:180px;padding:10px;}
.convert_option{margin-top:20px}
.convert_option li{float:left;margin-right:10px }
.convert_option li .btn{vertical-align:middle;margin-top:-5px;}
.convert_option li:nth-child(n+5){float:right}
.panel{margin-bottom:30px}
</style>
</head>
<body>
<a href="https://github.com/allanguys/px2rpx" target="_blank"><img style="position: absolute; top: 0; right: 0; border: 0; width: 149px; height: 149px;" src="http://aral.github.com/fork-me-on-github-retina-ribbons/right-graphite@2x.png" alt="Fork me on GitHub"></a>
<div class="alert alert-success" style="display:none"></div>
<div class="alert alert-danger" style="display:none">是不是点错转换按钮了?奴家找不到一个可以转换的数值单位 :( </div>
<div class="container">
<div class="panel panel-default">
<div class="panel-body">
<span class="label label-primary">Px</span>/<span class="label label-primary">Rpx</span>(<a href="https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxss.html" title="微信小程序尺寸单位(rpx)" target="_blank">微信小程序尺寸单位)</a>互转工具
</div>
</div>
<h4>转换前代码:</h4>
<textarea id="convert_before"></textarea>
<div class="alert alert-info convert_option">
<ul>
<li>设计稿宽度:
<select id="designWidth">
<option value="720">720px (Galaxy S5)</option>
<option value="824">824px (Nexus 5X)</option>
<option value="640">640px (iPhone5)</option>
<option value="750" selected>750px (iPhone6)</option>
<option value="828">828px (iPhone6 Plus)</option>
</select>
</li>
<li><input type="radio" name="cssbeauty" checked value="format" class="cssbeauty"/>格式化样式</li>
<li><input type="radio" name="cssbeauty" value="format" data-type="oneline" class="cssbeauty"/>格式化样式为横排</li>
<li><input type="radio" name="cssbeauty" value="pack" class="cssbeauty"/>压缩样式</li>
<li><button class="btn btn-danger convert_btn" data-type="px">Rpx转Px</button></li>
<li><button class="btn btn-primary convert_btn" data-type="rpx">Px转Rpx</button></li>
</ul>
</div>
<h4>转换后代码:</h4>
<textarea id="convert_done"></textarea>
</div>
<script src="https://ossweb-img.qq.com/images/js/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
var convertBeforeDom = $('#convert_before');
var convertDoneDom = $('#convert_done');
var convertBeforeVal = '';
var convertDoneVal = '';
var pxs = [];
var splitCach = [];
var errorDom = $('.alert-danger');
var successDom = $('.alert-success');
var CSS = $('.cssbeauty')
//填充本页样式
convertBeforeDom.html($.trim($('style')[0].innerHTML))
$('.convert_btn').click(function(){
Px2Rpx($(this).attr('data-type'));
});
function Px2Rpx(toUnit) {
convertBeforeVal = convertBeforeDom.val();
if (toUnit == 'rpx') {
pxs = convertBeforeVal.match(/\d+(?=px)px/g);
}else{
pxs = convertBeforeVal.match(/\d+(?=rpx)rpx/g);
};
convertDoneVal = '';
if (!pxs) {
tips(errorDom);
return
};
for(var i = 0; i < pxs.length; i++) {
splitCach = convertBeforeVal.split(pxs[i]);
convertDoneVal += splitCach[0];
convertDoneVal += getConvertNum(pxs[i],toUnit);
convertBeforeVal = convertBeforeVal.replace(splitCach[0],'').replace(pxs[i],'');
};
for (var i = 0; i < CSS.length; i++) {
if (CSS[i].checked) {
var cssFn = CSSBeauty[$(CSS[i]).attr('value')];
convertDoneDom.val(cssFn.apply(null, [convertDoneVal+convertBeforeVal,$(CSS[i]).attr('data-type')]));
};
};
if ($('#designWidth').val() == 750) {
tips(successDom,'主人,转换任务已经全部达成 :p。再偷偷告诉你,如果设计稿是750px,可以直接1比1写wxss哦~');
}else{
tips(successDom,'主人,转换任务已经全部达成 :p');
};
};
function getConvertNum(num,toUnit){
num = num.match(/\d+/g)[0];
dw = $('#designWidth').val();
if (toUnit == 'rpx') {
return parseFloat((num * (750/dw)*2).toFixed(2))+'rpx';
}else{
return parseFloat((num * (dw/750)/2).toFixed(2))+'px';
};
}
function tips(dom,text){
dom.stop().fadeIn('200');
if (text != undefined) {
dom.html(text)
};
setTimeout(function(){if (dom.css('display') !== 'none'){dom.fadeOut('200')};},2000) ;
}
//格式化样式
var CSSBeauty = {
format: function (s,oneline) {//格式化
s = s.replace(/\s*([\{\}\:\;\,])\s*/g, "$1");
s = s.replace(/;\s*;/g, ";"); //清除连续分号
s = s.replace(/\,[\s\.\#\d]*{/g, "{");
s = s.replace(/([^\s])\{([^\s])/g, "$1 {\n\t$2");
s = s.replace(/([^\s])\}([^\n]*)/g, "$1\n}\n$2");
s = s.replace(/([^\s]);([^\s\}])/g, "$1;\n\t$2");
if (oneline) {
s = s.replace(/(\r|\n|\t)/g, "");
s = s.replace(/(})/g, "$1\r\n");
};
return s;
},
pack: function (s) {//压缩
s = s.replace(/\/\*(.|\n)*?\*\//g, ""); //删除注释
s = s.replace(/\s*([\{\}\:\;\,])\s*/g, "$1");
s = s.replace(/\,[\s\.\#\d]*\{/g, "{"); //容错处理
s = s.replace(/;\s*;/g, ";"); //清除连续分号
s = s.match(/^\s*(\S+(\s+\S+)*)\s*$/); //去掉首尾空白
return (s == null) ? "" : s[1];
}
};
//console
console.log("\n%cPx/Rpx互转换工具 \n%cTodo:批量转换","font-family:Consolas,Monaco,'Courier New',Helvetica;font-size:30px;color:#000;line-height:25px;","color:#333;line-height:30px")
//pageview
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?ad7ab8f27224c9122425846bc2bed779";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</body>
</html>