js 生成多行多列的宫格
时间: 2023-08-06 20:07:56 浏览: 45
好的,以下是一个生成多行多列的宫格的纯 JS 实现:
HTML 代码:
```
<div id="grid"></div>
```
CSS 代码:
```
#grid {
display: grid;
grid-template-columns: repeat(4, 1fr); /* 设定4列 */
grid-template-rows: repeat(3, 100px); /* 设定3行 */
grid-gap: 10px; /* 设定单元格间距 */
}
.cell {
background-color: lightgray;
text-align: center;
}
```
JS 代码:
```
const grid = document.getElementById('grid');
const rows = 3;
const cols = 4;
for (let i = 0; i < rows * cols; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.innerText = i + 1;
grid.appendChild(cell);
}
```
这个示例代码使用了 CSS grid 布局来实现宫格的布局。JS 代码通过使用循环来生成单元格元素,并将它们添加到页面上。在生成单元格时,我们可以给它们添加一些样式,如背景颜色、文本对齐等。
相关问题
生成一个宫格视图的代码
下面是一个简单的 Python 代码示例,用于生成一个 9x9 的宫格视图。你可以根据需要进行修改和扩展。
```python
def print_grid(grid):
for i in range(9):
if i % 3 == 0 and i != 0:
print("- - - - - - - - - - - -")
for j in range(9):
if j % 3 == 0 and j != 0:
print("|", end=" ")
print(grid[i][j], end=" ")
print()
# 示例用法
grid = [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9]
]
print_grid(grid)
```
这个代码将打印出以下宫格视图:
```
5 3 0 | 0 7 0 | 0 0 0
6 0 0 | 1 9 5 | 0 0 0
0 9 8 | 0 0 0 | 0 6 0
- - - - - - - - - - - -
8 0 0 | 0 6 0 | 0 0 3
4 0 0 | 8 0 3 | 0 0 1
7 0 0 | 0 2 0 | 0 0 6
- - - - - - - - - - - -
0 6 0 | 0 0 0 | 2 8 0
0 0 0 | 4 1 9 | 0 0 5
0 0 0 | 0 8 0 | 0 7 9
```
UICollectionView 多宫格
UICollectionView 是一种非常灵活的视图容器,可以用于创建多种类型的布局和多宫格布局。以下是实现 UICollectionView 多宫格布局的步骤:
1. 创建 UICollectionView:使用 UICollectionViewFlowLayout 作为布局对象创建 UICollectionView。
```swift
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
```
2. 配置 UICollectionViewFlowLayout:设置 UICollectionViewFlowLayout 的 itemSize 和 minimumInteritemSpacing 属性来定义每个 item 的大小和间距。同时,可以设置 sectionInset 属性来定义每个 section 的内边距。
```swift
layout.itemSize = CGSize(width: 80, height: 100)
layout.minimumInteritemSpacing = 10
layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
```
3. 实现 UICollectionViewDataSource 协议:提供数据源方法,包括 numberOfSections(in:)、collectionView(_:numberOfItemsInSection:) 和 collectionView(_:cellForItemAt:)。
```swift
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = UIColor.red
return cell
}
```
4. 注册 UICollectionViewCell:使用 register(_:forCellWithReuseIdentifier:) 方法注册 UICollectionViewCell。
```swift
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
```
5. 添加到视图中:将 UICollectionView 添加到视图层次结构中。
```swift
view.addSubview(collectionView)
```
以上是实现 UICollectionView 多宫格布局的基本步骤。需要根据具体的需求进行调整和优化。