Skip to content

Commit 00b268e

Browse files
authored
Merge pull request #2917 from adiessl/master
Add demo showcasing how to use gridstack.js attributes in CSS
2 parents bc26fb4 + 18de73b commit 00b268e

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

demo/css_attributes.html

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>CSS & attributes demo</title>
8+
9+
<link rel="stylesheet" href="demo.css"/>
10+
<script src="../dist/gridstack-all.js"></script>
11+
12+
</head>
13+
<body>
14+
<div class="container-fluid">
15+
<h1>Demo showcasing how to use gridstack.js attributes in CSS</h1>
16+
<p>The center of the widget shows its dimensions by purely using CSS, no JavaScript involved.</p>
17+
<div>
18+
<a class="btn btn-primary" onClick="addNewWidget()" href="#">Add Widget</a>
19+
</div>
20+
<br><br>
21+
<div class="grid-stack show-dimensions"></div>
22+
</div>
23+
<script src="events.js"></script>
24+
<script type="text/javascript">
25+
let grid = GridStack.init({
26+
float: true,
27+
resizable: { handles: 'all'}
28+
});
29+
addEvents(grid);
30+
31+
let items = [
32+
{x: 1, y: 1},
33+
{x: 2, y: 2, w: 3},
34+
{x: 4, y: 2},
35+
{x: 3, y: 1, h: 2},
36+
{x: 0, y: 6, w: 2, h: 2}
37+
];
38+
let count = 0;
39+
40+
getNode = function() {
41+
let n = items[count] || {
42+
x: Math.round(12 * Math.random()),
43+
y: Math.round(5 * Math.random()),
44+
w: Math.round(1 + 3 * Math.random()),
45+
h: Math.round(1 + 3 * Math.random())
46+
};
47+
n.content = n.content || String(count);
48+
count++;
49+
return n;
50+
};
51+
52+
addNewWidget = function() {
53+
grid.addWidget(getNode());
54+
};
55+
56+
addNewWidget();
57+
</script>
58+
</body>
59+
</html>

demo/demo.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,27 @@ h1 {
7474
background: none;
7575
inset: 0;
7676
}
77+
78+
.grid-stack.show-dimensions .grid-stack-item:after {
79+
content: '1x1';
80+
position: absolute;
81+
top: 50%;
82+
left: 50%;
83+
transform: translate(-50%, -50%);
84+
padding: 2px;
85+
color: black;
86+
background-color: white;
87+
pointer-events: none; /* to not interfere with dragging the item */
88+
}
89+
90+
.grid-stack.show-dimensions .grid-stack-item[gs-h]::after {
91+
content: '1x' attr(gs-h);
92+
}
93+
94+
.grid-stack.show-dimensions .grid-stack-item[gs-w]::after {
95+
content: attr(gs-w) 'x1';
96+
}
97+
98+
.grid-stack.show-dimensions .grid-stack-item[gs-h][gs-w]::after {
99+
content: attr(gs-w) 'x' attr(gs-h);
100+
}

demo/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ <h1>Demos</h1>
1212
<li><a href="anijs.html">AniJS</a></li>
1313
<li><a href="cell-height.html">Cell Height</a></li>
1414
<li><a href="column.html">Column</a></li>
15+
<li><a href="css_attributes.html">CSS & attributes</a></li>
1516
<!-- <li><a href="esmodule.html">ES Module test</a></li> -->
1617
<li><a href="float.html">Float grid</a></li>
1718
<li><a href="knockout.html">Knockout.js</a></li>

0 commit comments

Comments
 (0)