Skip to content

Grid 网格

代码演示

基础用法

vue
<template>
    <x-grid
        :columns="4"
        :gutter="12">
        <x-grid-item>
            <div class="light-green" />
        </x-grid-item>
        <x-grid-item>
            <div class="green" />
        </x-grid-item>
        <x-grid-item>
            <div class="light-green" />
        </x-grid-item>
        <x-grid-item>
            <div class="green" />
        </x-grid-item>
    </x-grid>
</template>

<script setup></script>

<style lang="less" scoped>
.light-green {
    height: 108px;
    background-color: #e6f4ff;
}

.green {
    height: 108px;
    background-color: #bae0ff;
}
</style>

间隔

vue
<template>
    <x-grid
        :columns="4"
        :gutter="[12, 8]">
        <x-grid-item>
            <div class="light-green" />
        </x-grid-item>
        <x-grid-item>
            <div class="green" />
        </x-grid-item>
        <x-grid-item>
            <div class="light-green" />
        </x-grid-item>
        <x-grid-item>
            <div class="green" />
        </x-grid-item>
        <x-grid-item>
            <div class="light-green" />
        </x-grid-item>
        <x-grid-item>
            <div class="green" />
        </x-grid-item>
        <x-grid-item>
            <div class="light-green" />
        </x-grid-item>
        <x-grid-item>
            <div class="green" />
        </x-grid-item>
    </x-grid>
</template>

<script setup></script>

<style lang="less" scoped>
.light-green {
    height: 108px;
    background-color: #e6f4ff;
}

.green {
    height: 108px;
    background-color: #bae0ff;
}
</style>

偏移

vue
<template>
    <x-grid
        :columns="4"
        :gutter="12">
        <x-grid-item :offset="1">
            <div class="light-green"></div>
        </x-grid-item>
        <x-grid-item :offset="1">
            <div class="green"></div>
        </x-grid-item>
    </x-grid>
</template>

<script setup></script>

<style lang="less" scoped>
.light-green {
    height: 108px;
    background-color: #e6f4ff;
}

.green {
    height: 108px;
    background-color: #bae0ff;
}
</style>

响应式列数

vue
<template>
    <x-grid
        :columns="{ xs: 1, sm: 2, md: 3, lg: 4, xl: 5, xxl: 6 }"
        :gutter="[12, 12]">
        <x-grid-item
            v-for="(item, index) in 6"
            :key="item">
            <div
                :class="{
                    'light-green': index % 2 === 1,
                    green: index % 2 === 0,
                }" />
        </x-grid-item>
    </x-grid>
</template>

<script setup>
import { GridItem as XGridItem } from '@/components'
</script>

<style lang="less" scoped>
.light-green {
    height: 108px;
    background-color: #e6f4ff;
}

.green {
    height: 108px;
    background-color: #bae0ff;
}
</style>

响应式栅格项

vue
<template>
    <x-grid :gutter="[12, 12]">
        <x-grid-item
            v-for="(item, index) in 6"
            :key="item"
            :lg="6"
            :md="8"
            :sm="12"
            :xl="4"
            :xs="24"
            :xxl="2">
            <div
                :class="{
                    'light-green': index % 2 === 1,
                    green: index % 2 === 0,
                }" />
        </x-grid-item>
    </x-grid>
</template>

<script setup>
import { GridItem as XGridItem } from '@/components'
</script>

<style lang="less" scoped>
.light-green {
    height: 108px;
    background-color: #e6f4ff;
}

.green {
    height: 108px;
    background-color: #bae0ff;
}
</style>

折叠

vue
<template>
    <x-grid :columns="2">
        <x-grid-item>
            <a-form-item label="数量">
                <a-input-number
                    v-model:value="gridItemCount"
                    :min="1" />
            </a-form-item>
        </x-grid-item>
        <x-grid-item>
            <a-form-item label="折叠后行数">
                <a-input-number
                    v-model:value="gridCollapsedRows"
                    :min="1" />
            </a-form-item>
        </x-grid-item>
        <x-grid-item>
            <a-form-item label="打开后缀节点">
                <a-switch v-model:checked="showSuffix" />
            </a-form-item>
        </x-grid-item>
        <x-grid-item>
            <a-form-item label="折叠栅格">
                <a-switch v-model:checked="gridCollapsed" />
            </a-form-item>
        </x-grid-item>
    </x-grid>

    <x-grid
        :collapsed="gridCollapsed"
        :collapsed-rows="gridCollapsedRows"
        :columns="5">
        <x-grid-item
            v-for="i in gridItemCount"
            :key="i"
            :class="i % 2 ? 'green' : 'light-green'">
            {{ i }}
        </x-grid-item>
        <x-grid-item
            v-if="showSuffix"
            #="{ overflow }"
            class="suffix"
            suffix>
            {{ overflow ? '存在溢出' : '不存在溢出' }}
        </x-grid-item>
    </x-grid>
</template>

<script setup>
import { ref } from 'vue'

const gridCollapsed = ref(false)
const gridCollapsedRows = ref(1)
const gridItemCount = ref(4)
const showSuffix = ref(true)
</script>

<style lang="less" scoped>
.suffix {
    height: 108px;
    border: 1px solid #bae0ff;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
}

.light-green {
    height: 108px;
    background-color: #e6f4ff;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
}

.green {
    height: 108px;
    background-color: #bae0ff;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
}
</style>

API

Grid Props

名称说明类型默认值
columns显示的栅格数量number Object24
gutter栅格间隔,可以写成像素值设置水平间隔 。或者使用数组形式同时设置 [水平间距, 垂直间距]number array0
collapsed是否默认折叠booleanfalse
collapsed-rows默认展示的行数number1

GridItem Props

名称说明类型默认值
span栅格占据的列数number1
xs<576px 栅格占据的列数number-
sm≥576px 栅格占据的列数number-
md≥768px 栅格占据的列数number-
lg≥992px 栅格占据的列数number-
xl≥1200px 栅格占据的列数number-
xxl≥1600px 栅格占据的列数number-
offset栅格左侧的间隔格数number0
suffix栅格后缀,只能存在一个booleanfalse

本文档内容版权为 XYAdmin 作者所有,保留所有权利。