-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
137 lines (116 loc) · 3.9 KB
/
scripts.js
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
//DOM Stored in Variable
let frameDOM =document.querySelector(".imageFrame"),
contentDOM = document.querySelector(".content"),
fullDOM = document.querySelector(".fullContent");
//Variable declaration
var width=6, height=2,flag=1, total, left=100, topp=50, size=150;
var screenWidth = 1100, currSWidth, maxWidth, element, styleObj, contHeight;
//Calculate current width of full content
function currentWidth(){
var styleObj = window.getComputedStyle(fullDOM),
curr_width = styleObj.getPropertyValue('width');
maxWidth = parseInt(curr_width);
}
//Get current Height of content
function contentHeight(){
var paraDOM = document.querySelector(".para"),
styleP = window.getComputedStyle(paraDOM);
curr_height = styleP.getPropertyValue('height');
contHeight = parseInt(curr_height);
}
//initial state
currentWidth();
responsiveFull();
//responsive design
function responsiveFull(){
function responsiveDo(x) {
if (x.matches && currSWidth!==screenWidth) {
//For Small size screen
width = 6+ (screenWidth-currSWidth)/100,
size = maxWidth/width;
height = Math.ceil((contHeight+50)/size);
contentReady(width, height, size);
}
else if(currSWidth===screenWidth)
{
//Default size for Desktop/Laptop
width = 6;
height = 2;
size = maxWidth/width;
contentReady(width, height, size);
}
}
//Current window size
for(var i=screenWidth;i>=0;i-=100)
{
var x = window.matchMedia("(max-width: " + i + "px)");
currSWidth = i;
contentHeight();
currentWidth();
responsiveDo(x)
x.addListener(responsiveDo)
}
}
//Responsive while resizing browser
$(window).on('resize', responsiveFull);
//load image
function contentReady(width, height, size){
frameDOM.innerHTML = "";
flag =1;
total=2*(width+height);
for(var i=0; i<total; i++)
{
//load Image from files
frameDOM.insertAdjacentHTML("afterbegin", "<div class=\"image\" data-index=\"" + i + "\" style=\"width: " + size + "px; height: " + size + "px; left: " + left + "px; top: " + topp + "px; display: block; position: absolute;\"> <img src=\"photos/" + i%54 + ".jpg\" > </div>");
//left top measurement
if(flag<width)
{
left+=size;
}
else if(flag<=(width+height))
{
topp+=size;
}
else if(flag<(2*width+height))
{
left-=size;
}
else topp-=size;
flag++;
}
//content positioning
contentDOM.style.left = left+size + "px";
contentDOM.style.top = topp+size + "px";
contentDOM.style.height = height*size + "px";
contentDOM.style.width = (width-2)*size + "px";
fullDOM.style.height = (height+2)*size + "px";
}
//animation
let adjacent, index;
setInterval(animation, 1500);
function animation(){
//genarating random number
adjacent = (Math.random()*10)<5? -1: 1;
index = Math.floor(Math.random()*total);
//Corner case handling
if(adjacent === -1 && index === 0)
adjacent = total-1;
//select two photo for swap
var dataDOM= document.querySelector("[data-index=\"" + index + "\"]")
var dataSDOM = document.querySelector("[data-index=\"" + (index+adjacent)%total + "\"]")
//Swaping image attribute
var temp = dataDOM.style.left;
dataDOM.style.left = dataSDOM.style.left;
dataSDOM.style.left = temp;
temp= dataDOM.style.top;
dataDOM.style.top = dataSDOM.style.top;
dataSDOM.style.top = temp;
//swaping data index
temp = (index+adjacent)%total;
dataSDOM.setAttribute('data-index', '100');
dataDOM.setAttribute('data-index', temp);
document.querySelector("[data-index='100']").setAttribute('data-index', index);
//slide animation
dataDOM.classList.add('slideAnimation');
dataSDOM.classList.add('slideAnimation');
}