forked from xingqiao/cLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.html
More file actions
231 lines (216 loc) · 7.11 KB
/
Copy pathtemplate.html
File metadata and controls
231 lines (216 loc) · 7.11 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>自动切图DEMO</title>
<% if (data.type != 1) { %>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0" />
<% } %>
<meta name="format-detection" content="telephone=no">
<meta http-equiv="Content-Language" content="zh-cn" />
<meta name="author" content="Tencent-ISUX-Music" />
<meta name="Copyright" content="Tencent" />
<meta name="description" content="QQ音乐" />
<meta name="keywords" content="QQ音乐" />
<meta name="applicable-device" content="mobile">
<%#
模板数据
data = {
// 页面类型
"type": 0, // 0-H5 1-PC
// 初始化参数
"opts": {},
// 页面大小
"width": 640,
"height": 2645,
// 页面背景色
"backgroundColor": "#f3e3f7",
// PC页背景图
"backgroundImage": {
"width": 960,
"height": 960,
"index": "bg",
"data": {
"src": "data:image/jpeg;base64,/9j/4AAQS……H7q6I//9k="
}
},
// 分割出来的图片元素
"list": [
// 独立图片元素
{
"top": 2376,
"bottom": 2543,
"left": 48,
"right": 591,
"width": 544,
"height": 168,
"index": 11,
// 图像数据,根据分辨率会可能会生成 320、640、原始大小 三种
"data": {
"320": "data:image/jpeg;base64,/9j/4AAQS……3M7N9z/9k=",
"src": "data:image/jpeg;base64,/9j/4AAQS……H7q6I//9k="
}
},
// 雪碧图类型
{
"width": 512,
"height": 48,
// 雪碧图中每个子图像元素定位
"list": [
{
"left": 8,
"top": 8,
"originalLeft": 240,
"originalTop": 2584,
"width": 160,
"height": 32
},
{
"left": 176,
"top": 8,
"originalLeft": 160,
"originalTop": 1032,
"width": 328,
"height": 24
}
],
"backgroundColor": "#f3e3f7",
"index": 12,
"data": {
"320": "data:image/jpeg;base64,/9j/4AAQS……AP/Z",
"src": "data:image/jpeg;base64,/9j/4AAQS……D/2Q=="
}
}
]
}
%>
<%
var opts = data.opts || {};
var pageWidth = data.width || 384;
var css = [];
css.push("body,.bgwrap{width:100%;margin:0;background-color:" + data.backgroundColor + "}");
css.push(".bglist{width:" + pageWidth + "px}");
css.push(".bgimg{position:absolute;background-repeat:no-repeat}");
if (data.list && data.list.forEach) {
data.list.forEach(function(item){
css.push(
".bg-" + item.index + "{"
+ "width:" + item.width + "px;"
+ "height:" + item.height + "px;"
+ "left:" + (item.left || 0) + "px;"
+ "top:" + (item.top || 0) + "px;"
+ "background-image:url(" + item.data.src + ");"
+ "}"
);
if (item.list && item.list.forEach) {
item.list.forEach(function(sub, subIndex){
css.push(
".bg-" + item.index + "-" + subIndex + "{"
+ "width:" + sub.width + "px;"
+ "height:" + sub.height + "px;"
+ "left:" + sub.originalLeft + "px;"
+ "top:" + sub.originalTop + "px;"
+ "background-position:-" + (sub.left || 0) + "px -" + (sub.top || 0) + "px"
+ "}"
);
});
}
});
for (var i = 3; i > 0; i--) {
var w = i * 320;
if (data.width > w) {
var _sub = [];
data.list.forEach(function(item){
if (item.data[w]) {
_sub.push(".bg-" + item.index + "{background-image:url(" + item.data[w] + ")}");
}
});
if (_sub.length) {
css.push("@media (max-width: " + w + "px) {" + _sub.join("") + "}");
}
}
}
}
var bgDom;
if (data.type == 1) {
css.push(".bgwrap{position:relative;width:" + pageWidth + "px;margin:0 auto}");
var bgimg = data.backgroundImage || {};
var bgtype = parseInt(data.opts && data.opts.bgtype || 0);
var background = [];
if (bgimg.data && bgimg.data.src) {
background.push("background:url(" + bgimg.data.src + ") no-repeat 50% 0");
}
if (bgtype & 1) {
bgDom = 1;
}
if (bgtype & 2) {
background.push("background-repeat-y:repeat");
}
if (bgtype & 3) {
background.push("background-size:cover");
}
if (opts.bgcolor) {
background.push("background-color:" + opts.bgcolor);
}
if (background.length) {
css.push((bgDom ? ".bgcover" : "body") + "{" + background.join(";") + "}");
if (bgDom) {
css.push(".bgcover{position:fixed;top:0;bottom:0;left:0;right:0}");
}
}
} else {
css.push(".bglist{transform-origin:0 0}");
}
%>
<style>
<%- css.join("\n") %>
</style>
</head>
<body>
<% if (bgDom) { %>
<div class="bgcover"></div>
<% } %>
<!--背景列表-->
<div class="bgwrap">
<div class="bglist">
<% if (data.list) { %>
<% data.list.forEach(function(item, index){ %>
<% if (item.list) { %>
<% item.list.forEach(function(sub, subIndex){ %>
<div class="bgimg bg-<%- index %> bg-<%- index %>-<%- subIndex %>"></div>
<% }) %>
<% } else { %>
<div class="bgimg bg-<%- index %>"></div>
<% } %>
<% }) %>
<% } %>
</div>
</div>
<% if (data.type != 1) { %>
<!--自动调节背景缩放-->
<script>
;(function(){
var bglist = document.querySelector(".bglist"),
width = <%- pageWidth %>,
curWidth = window.innerWidth,
s = 1;
if (width > 0) {
s = curWidth / width;
if (s != 1) {
bglist.style.zoom = s;
}
} else {
width = curWidth;
bglist.style.width = width + "px";
}
window.addEventListener("resize", function(){
s = window.innerWidth / width;
if (s != 1) {
bglist.style.zoom = s;
}
});
})();
</script>
<% } %>
</body>
</html>