-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
click-grid-to-expand.html
145 lines (133 loc) · 4.75 KB
/
click-grid-to-expand.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Improved Interactive CSS Grid Layout with Symmetric Animation</title>
<style>
body {
font-family: Arial, sans-serif;
}
.grid-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(2, 100px);
gap: 10px;
padding: 10px;
background-color: #f0f0f0;
width: 800px;
height: 210px;
position: relative;
}
.grid-item {
background-color: #ffffff;
border: 2px solid #333;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
font-size: 16px;
font-weight: bold;
text-align: center;
}
.item1 {
grid-column: span 2;
grid-row: span 2;
background-color: #ffcccb;
}
.item2 {
grid-column: span 1;
grid-row: span 2;
background-color: #c1e1c1;
cursor: pointer;
}
.item3 {
grid-column: span 1;
grid-row: span 1;
background-color: #add8e6;
}
.item4 {
grid-column: span 1;
grid-row: span 1;
background-color: #fffacd;
}
.expanded-overlay {
position: absolute;
background-color: #c1e1c1;
border: 2px solid #333;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
font-size: 16px;
font-weight: bold;
text-align: center;
cursor: pointer;
transition: all 0.5s ease;
}
</style>
</head>
<body>
<div class="grid-container" id="gridContainer">
<div class="grid-item item1">2x2</div>
<div class="grid-item item2" id="expandable">1x2<br>(Click to expand)</div>
<div class="grid-item item3">1x1</div>
<div class="grid-item item4">1x1</div>
</div>
<script>
const gridContainer = document.getElementById('gridContainer');
const expandable = document.getElementById('expandable');
let isExpanded = false;
let overlay;
let originalRect;
function createOverlay(element) {
originalRect = element.getBoundingClientRect();
const containerRect = gridContainer.getBoundingClientRect();
overlay = document.createElement('div');
overlay.className = 'expanded-overlay';
overlay.textContent = 'Expanded!\nClick to shrink';
overlay.style.left = `${originalRect.left - containerRect.left}px`;
overlay.style.top = `${originalRect.top - containerRect.top}px`;
overlay.style.width = `${originalRect.width}px`;
overlay.style.height = `${originalRect.height}px`;
gridContainer.appendChild(overlay);
// Force reflow
overlay.offsetHeight;
// Animate to full size
overlay.style.left = '0';
overlay.style.top = '0';
overlay.style.width = '100%';
overlay.style.height = '100%';
overlay.addEventListener('click', toggleExpansion);
}
function removeOverlay() {
if (overlay) {
const containerRect = gridContainer.getBoundingClientRect();
// Animate back to original size
overlay.style.left = `${originalRect.left - containerRect.left}px`;
overlay.style.top = `${originalRect.top - containerRect.top}px`;
overlay.style.width = `${originalRect.width}px`;
overlay.style.height = `${originalRect.height}px`;
// Remove overlay after animation completes
overlay.addEventListener('transitionend', function handler() {
overlay.removeEventListener('transitionend', handler);
overlay.removeEventListener('click', toggleExpansion);
gridContainer.removeChild(overlay);
overlay = null;
expandable.style.visibility = 'visible';
});
}
}
function toggleExpansion() {
if (!isExpanded) {
expandable.style.visibility = 'hidden';
createOverlay(expandable);
} else {
removeOverlay();
}
isExpanded = !isExpanded;
}
expandable.addEventListener('click', toggleExpansion);
</script>
</body>
</html>