|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
3 | 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 | 4 | <title>Responsive grid demo</title> |
8 | 5 |
|
9 | 6 | <link rel="stylesheet" href="demo.css"/> |
|
15 | 12 | <script src="../src/gridstack.jQueryUI.js"></script> |
16 | 13 | </head> |
17 | 14 | <body> |
18 | | - <div class="device-xs visible-xs"></div> |
19 | | - <div class="device-sm visible-sm"></div> |
20 | | - <div class="device-md visible-md"></div> |
21 | | - <div class="device-lg visible-lg"></div> |
22 | | - <div class="device-xl visible-xl"></div> |
23 | | - <div class="container-fluid"> |
| 15 | + <div> |
24 | 16 | <h1>Responsive grid demo</h1> |
25 | | - |
26 | 17 | <div> |
27 | | - <span>Number of Columns:</span> <span id="grid-size"></span> |
| 18 | + <span>Number of Columns:</span> <span id="column-text"></span> |
28 | 19 | </div> |
29 | | - |
30 | 20 | <br/> |
31 | | - |
32 | 21 | <div class="grid-stack"> |
33 | 22 | </div> |
34 | 23 | </div> |
35 | 24 |
|
36 | | - |
37 | 25 | <script type="text/javascript"> |
38 | 26 | $(function () { |
39 | | - // thanks to http://stackoverflow.com/a/22885503 |
40 | | - var waitForFinalEvent=function(){var b={};return function(c,d,a){a||(a="I am a banana!");b[a]&&clearTimeout(b[a]);b[a]=setTimeout(c,d)}}(); |
41 | | - var fullDateString = new Date(); |
42 | | - function isBreakpoint(alias) { |
43 | | - return $('.device-' + alias).is(':visible'); |
44 | | - } |
45 | | - |
46 | | - $('.grid-stack').gridstack(); |
| 27 | + var grid = $('.grid-stack').gridstack({ |
| 28 | + disableOneColumnMode: true, // will manually do 1 column |
| 29 | + float: true }).data('gridstack'); |
| 30 | + var $text = $('#column-text'); |
| 31 | + |
47 | 32 | function resizeGrid() { |
48 | | - var grid = $('.grid-stack').data('gridstack'); |
49 | | - if (isBreakpoint('xs')) { |
50 | | - $('#grid-size').text('One column mode'); |
51 | | - } else if (isBreakpoint('sm')) { |
| 33 | + var width = document.body.clientWidth; |
| 34 | + if (width < 700) { |
| 35 | + grid.setColumn(1); |
| 36 | + $text.text(1); |
| 37 | + } else if (width < 850) { |
52 | 38 | grid.setColumn(3); |
53 | | - $('#grid-size').text(3); |
54 | | - } else if (isBreakpoint('md')) { |
| 39 | + $text.text(3); |
| 40 | + } else if (width < 950) { |
55 | 41 | grid.setColumn(6); |
56 | | - $('#grid-size').text(6); |
57 | | - } else if (isBreakpoint('lg')) { |
| 42 | + $text.text(6); |
| 43 | + } else { |
58 | 44 | grid.setColumn(12); |
59 | | - $('#grid-size').text(12); |
| 45 | + $text.text(12); |
60 | 46 | } |
61 | 47 | }; |
62 | 48 | $(window).resize(function () { |
63 | | - waitForFinalEvent(function() { |
64 | | - resizeGrid(); |
65 | | - }, 300, fullDateString.getTime()); |
| 49 | + resizeGrid(); |
66 | 50 | }); |
67 | | - |
68 | | - new function () { |
69 | | - this.serializedData = [ |
70 | | - {x: 0, y: 0, width: 4, height: 2}, |
71 | | - {x: 3, y: 1, width: 4, height: 2}, |
72 | | - {x: 4, y: 1, width: 4, height: 1}, |
73 | | - {x: 2, y: 3, width: 8, height: 1}, |
74 | | - {x: 0, y: 4, width: 4, height: 1}, |
75 | | - {x: 0, y: 3, width: 4, height: 1}, |
76 | | - {x: 2, y: 4, width: 4, height: 1}, |
77 | | - {x: 2, y: 5, width: 4, height: 1}, |
78 | | - {x: 0, y: 6, width: 12, height: 1} |
79 | | - ]; |
80 | | - |
81 | | - this.grid = $('.grid-stack').data('gridstack'); |
82 | | - |
83 | | - this.loadGrid = function () { |
84 | | - this.grid.removeAll(); |
85 | | - var items = GridStackUI.Utils.sort(this.serializedData); |
86 | | - this.grid.batchUpdate(); |
87 | | - items.forEach(function (node, i) { |
88 | | - this.grid.addWidget($('<div><div class="grid-stack-item-content">' + i + '</div></div>'), node); |
89 | | - }.bind(this)); |
90 | | - this.grid.commit(); |
91 | | - return false; |
92 | | - }.bind(this); |
93 | | - |
94 | | - this.loadGrid(); |
| 51 | + $(document).ready(function() { |
95 | 52 | resizeGrid(); |
96 | | - }; |
| 53 | + }); |
| 54 | + |
| 55 | + var items = [ |
| 56 | + {x: 0, y: 0, width: 2, height: 2}, |
| 57 | + {x: 2, y: 0, width: 2, height: 1}, |
| 58 | + {x: 5, y: 0, width: 1, height: 1}, |
| 59 | + {x: 1, y: 3, width: 4, height: 1}, |
| 60 | + {x: 5, y: 2, width: 2, height: 1}, |
| 61 | + {x: 0, y: 4, width: 12, height: 1} |
| 62 | + ]; |
| 63 | + grid.batchUpdate(); |
| 64 | + items.forEach(function (node, index) { |
| 65 | + grid.addWidget($('<div><div class="grid-stack-item-content">' + index + '</div></div>'), node); |
| 66 | + }); |
| 67 | + grid.commit(); |
97 | 68 | }); |
98 | 69 | </script> |
99 | 70 | </body> |
|
0 commit comments