-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
317 lines (272 loc) · 6.89 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>散列画廊效果</title>
<style type="text/css">
*{margin: 0;padding: 0;}
body{
color: #555;
font-size: 14px;
font-family: "Avenir Next","Lantinghei SC";
-webkit-font-smoothing:antialiased;
}
.wrap{
width: 100%;
height: 600px;
position: absolute;
top: 50%;
margin-top: -300px;
overflow: hidden;
background-color: gray;
-webkit-perspective:800px;
-moz-perspective:800px;
-ms-perspective:800px;
-o-perspective:800px;
}
/*海报样式*/
.photo{
width: 260px;
height: 320px;
position: absolute;
z-index: 1;
box-shadow: 0 0 1px rgba(0,0,0,.01);
transition: all 1s;
}
.photo .side{
width: 100%;
height: 100%;
background-color: #eee;
position: absolute;
top: 0;
right: 0;
padding: 20px;
box-sizing: border-box;
}
.photo .side-front{}
.photo .side-front .image{
width: 100%;
height: 250px;
line-height: 250px;
overflow: hidden;
}
.photo .side-front .image img{
width: 100%;
}
.photo .side-front .caption{
font-size: 16px;
line-height: 25px;
text-align: center;
}
.photo .side-back{}
.photo .side-back .desc{
color: #666;
font-size: 14px;
line-height: 1.5em;
}
/*当前选中海报样式*/
.photo-center{
top: 50%;
left: 50%;
margin:-160px 0 0 -130px;
z-index: 999;
}
/*防止居中样式消失后画面闪动*/
.photo{
top: 50%;
left: 50%;
margin:-160px 0 0 -130px;
}
/*海报翻转*/
.photo-wrap{
position: absolute;
width: 100%;
height: 100%;
transform-style:preserve-3d;
transition:all 1s;
transform-origin:0% 50%;
}
.photo-wrap .side{
backface-visibility:hidden;
}
.photo-wrap .side-front{
transform:rotateY(0deg);
}
.photo-wrap .side-back{
transform:rotateY(180deg);
}
.photo_front .photo-wrap{
transform:translate(0px,0px) rotateY(0deg);
}
.photo_back .photo-wrap{
transform:translate(260px,0px) rotateY(180deg);
}
/*翻转控制按钮样式*/
.nav{
width: 80%;
height: 30px;
line-height: 30px;
position: absolute;
left: 10%;
bottom: 20px;
z-index: 999;
text-align: center;
}
.nav .i{
width: 30px;
height: 30px;
border-radius: 50%;
display: inline-block;
cursor: pointer;
transform: scale(.5);
background-color: #aaa;
transition: all .5s;
}
/*当前按钮样式*/
.nav .i_current{
transform: scale(.7);
}
/*翻转后按钮样式*/
.nav .i_back{
background-color: #555;
transform: ratateY(-180deg);
}
</style>
</head>
<body>
<div class="wrap" id="wrap">
<!--div.photo负责平移和旋转-->
<div class="photo photo_front" onclick="turn(this)" id="photo_{{index}}">
<!--div.photo-wrap负责翻转-->
<div class="photo-wrap">
<div class="side side-front">
<!--将具体内容用模拟字符串{{}}表示-->
<p class="image"><img src="img/{{img}}"></p>
<p class="caption">{{caption}}</p>
</div>
<div class="side side-back">
<p class="desc">{{desc}}</p>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="js/data.js"></script>
<script type="text/javascript">
//2.内容输出
function g(selector){
//通用函数
var method = selector.substr(0,1)=='.' ? 'getElementsByClassName' : 'getElementById'
return document[method](selector.substr(1))
}
//生成随机数random(min,max)
function random(range){
var max = Math.max(range[0],range[1])
var min = Math.min(range[0],range[1])
var diff = max-min
var number = Math.ceil(Math.random()*diff+min)
return number
}
//3.输出所有海报
var data = data
function addPhotos(){
var template = g("#wrap").innerHTML
var html = []
var nav = []
for(s in data){
var _html = template
.replace('{{index}}',s)
.replace('{{img}}',data[s].img)
.replace('{{caption}}',data[s].caption)
.replace('{{desc}}',data[s].desc)
html.push(_html)
nav.push('<span id="nav_'+s+'" class="i" onclick="turn(g(\'#photo_'+s+'\'))"> </span>')
}
html.push('<div class="nav">'+nav.join('')+'</div')
g("#wrap").innerHTML = html.join('')
rsort(random([0,data.length-1]))
}
addPhotos()
//4.排序海报
function rsort(n){
//先选中所有图片清除样式
var _photo = g(".photo")
var photos = []
for(var i=0,len=_photo.length; i<len; i++){
_photo[i].className = _photo[i].className.replace(/\s*photo-center\s*/,' ')
_photo[i].className = _photo[i].className.replace(/\s*photo_back\s*/,' ')
_photo[i].style.left = ""
_photo[i].style.top = ""
_photo[i].style['transform'] = "rotate(0deg) scale(1.3)"
photos.push(_photo[i])
}
//为选中的photo_n添加居中样式
var photo_center = g("#photo_"+n)
photo_center.className += " photo-center "
//为居中海报对应的按钮添加i_current样式
var navs = g('.i')
for(var i=0,len=navs.length;i<len;i++){
navs[i].className = navs[i].className.replace(/\s*i_current\s*/,' ')
navs[i].className = navs[i].className.replace(/\s*i_back\s*/,' ')
}
g("#nav_"+n).className += " i_current "
//为除居中图片外的图片分区
photos_center = photos.splice(n,1)[0]
var photo_left = photos.splice(0,Math.ceil(photos.length/2))
var photo_right = photos
var ranges = range()
//为左右分区图片添加随机位置、旋转样式
for(i in photo_left){
photo_left[i].style.left = random(ranges.left.x)+"px"
photo_left[i].style.top = random(ranges.left.y)+"px"
photo_left[i].style['transform'] = 'rotate('+random([-150,150])+'deg) scale(1)'
}
for(i in photo_right){
photo_right[i].style.left = random(ranges.right.x)+"px"
photo_right[i].style.top = random(ranges.right.y)+"px"
photo_right[i].style['transform'] = 'rotate('+random([-150,150])+'deg) scale(1)'
}
}
//5.计算左右分区范围
function range(){
var range = {
left:{
x:[],
y:[]
},
right:{
x:[],
y:[]
}
}
var wrapW = g("#wrap").clientWidth,
wrapH = g("#wrap").clientHeight,
photoW = g(".photo")[0].clientWidth,
photoH = g(".photo")[0].clientHeight
range.left.x = [0-photoW,wrapW/2-photoW/2]
range.left.y = [0-photoH,wrapH]
range.right.x = [wrapW/2+photoW/2,wrapW+photoW]
range.right.y = range.left.y
return range
}
//1.翻面控制
function turn(ele){
var cls = ele.className
var n = ele.id.split('_')[1]
//判断翻面海报是否已经是居中海报,如果不是,重新排序
if(!/photo-center/.test(cls)){
return rsort(n)
}
if(/photo_front/.test(cls)){
//利用正则判断className中是否包括photo_front,若有则替换成photo_back
cls = cls.replace(/photo_front/,'photo_back')
g('#nav_'+n).className += ' i_back '
}else{
//若包括的是photo_back,则替换成photo_front
cls = cls.replace(/photo_back/,'photo_front')
g('#nav_'+n).className = g('#nav_'+n).className.replace(/\s*i_back\s*/,' ')
}
return ele.className = cls
}
</script>
</body>
</html>