-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathgrid-lines.html
More file actions
168 lines (153 loc) · 5.69 KB
/
Copy pathgrid-lines.html
File metadata and controls
168 lines (153 loc) · 5.69 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Grid Lines demo</title>
<link rel="stylesheet" href="demo.css"/>
<script src="../dist/gridstack-all.js"></script>
<style>
.controls {
display: flex;
align-items: center;
gap: 24px;
padding: 12px 0 16px;
flex-wrap: wrap;
}
.control-group {
display: flex;
align-items: center;
gap: 8px;
}
.control-group label {
font-weight: 500;
white-space: nowrap;
}
input[type=range] {
width: 140px;
}
.val-badge {
display: inline-block;
min-width: 2.5em;
font-family: monospace;
font-size: 0.9em;
background: #eee;
border-radius: 4px;
padding: 1px 5px;
text-align: center;
}
/* ── Grid lines ───────────────────────────────────────────────
Uses CSS custom properties already set by GridStack on the
grid element:
--gs-column-width (e.g. 8.333% for a 12-col grid)
--gs-cell-height (e.g. 70px)
Two extra vars control appearance and can be set via JS or
overridden in your own stylesheet:
--gs-line-color default: rgba(0,0,0,0.15)
--gs-line-thickness default: 1px
──────────────────────────────────────────────────────────── */
.grid-stack.gs-show-grid-lines {
background-image:
linear-gradient(
to right,
var(--gs-line-color, rgba(0,0,0,.15)) var(--gs-line-thickness, 1px),
transparent var(--gs-line-thickness, 1px)
),
linear-gradient(
to bottom,
var(--gs-line-color, rgba(0,0,0,.15)) var(--gs-line-thickness, 1px),
transparent var(--gs-line-thickness, 1px)
);
background-size: var(--gs-column-width) var(--gs-cell-height);
background-position: top left;
}
</style>
</head>
<body>
<div style="padding: 0 20px">
<h1>Grid Lines demo</h1>
<p>Toggle grid lines on/off and adjust color and thickness using the controls below.
No JS changes to GridStack itself are needed — the trick relies on the CSS custom
properties <code>--gs-column-width</code> and <code>--gs-cell-height</code> that
GridStack already sets on the element.</p>
<div class="controls">
<div class="control-group">
<label for="toggle">Show lines</label>
<input type="checkbox" id="toggle" checked>
</div>
<div class="control-group">
<label for="colorPicker">Color</label>
<input type="color" id="colorPicker" value="#0000ff">
<input type="range" id="opacity" min="0" max="100" value="20" title="Opacity">
<span class="val-badge" id="opacityVal">20%</span>
</div>
<div class="control-group">
<label for="thickness">Thickness</label>
<input type="range" id="thickness" min="1" max="8" value="1" step="0.5">
<span class="val-badge" id="thicknessVal">1px</span>
</div>
<div class="control-group">
<label for="columns">Columns</label>
<input type="range" id="columns" min="2" max="24" value="12" step="1">
<span class="val-badge" id="columnsVal">12</span>
</div>
</div>
<div class="grid-stack gs-show-grid-lines" id="grid"></div>
</div>
<script>
const grid = GridStack.init({ column: 12, cellHeight: 70, animate: true });
grid.load([
{x:0, y:0, w:2, h:2, content:'A'},
{x:2, y:0, w:3, h:1, content:'B'},
{x:5, y:0, w:2, h:3, content:'C'},
{x:2, y:1, w:2, h:2, content:'D'},
{x:7, y:0, w:5, h:2, content:'E'},
{x:0, y:2, w:2, h:2, content:'F'},
{x:4, y:2, w:3, h:2, content:'G'},
{x:7, y:2, w:3, h:1, content:'H'},
{x:10, y:2, w:2, h:2, content:'I'},
]);
const el = document.getElementById('grid');
const toggleEl = document.getElementById('toggle');
const colorEl = document.getElementById('colorPicker');
const opacityEl = document.getElementById('opacity');
const opacityVal = document.getElementById('opacityVal');
const thickEl = document.getElementById('thickness');
const thickVal = document.getElementById('thicknessVal');
const colEl = document.getElementById('columns');
const colVal = document.getElementById('columnsVal');
function hexToRgba(hex, alpha) {
const r = parseInt(hex.slice(1,3), 16);
const g = parseInt(hex.slice(3,5), 16);
const b = parseInt(hex.slice(5,7), 16);
return `rgba(${r},${g},${b},${alpha})`;
}
function applyLines() {
const alpha = opacityEl.value / 100;
const color = hexToRgba(colorEl.value, alpha);
const thick = thickEl.value + 'px';
el.style.setProperty('--gs-line-color', color);
el.style.setProperty('--gs-line-thickness', thick);
}
toggleEl.addEventListener('change', () => {
el.classList.toggle('gs-show-grid-lines', toggleEl.checked);
});
colorEl.addEventListener('input', applyLines);
opacityEl.addEventListener('input', () => {
opacityVal.textContent = opacityEl.value + '%';
applyLines();
});
thickEl.addEventListener('input', () => {
thickVal.textContent = thickEl.value + 'px';
applyLines();
});
colEl.addEventListener('input', () => {
const n = parseInt(colEl.value);
colVal.textContent = n;
grid.column(n);
});
applyLines();
</script>
</body>
</html>