css布局方式,实现四宫格,九宫格,16宫格等切换

本文介绍两种实现自适应网格布局的方法:一是通过CSS绝对定位结合浮动布局;二是利用现代的CSS Grid布局特性。前者适用于需要兼容老旧浏览器的情况,后者则提供更简洁强大的网格布局解决方案。

1.借助absolute方位值,实现自适应的网格布局,用伪元素间隔

absolute+四个方位值撑开局面、float+宽度百分比实现横向排列。高度百分比实现自适应。
最外层的父元素使用absolute负责占位,给子元素们把空间拉开,或者固定宽高也行

效果图
在这里插入图片描述

ul{
      height: 100%;
      padding: 0;
      position:absolute;
      left:0;
      top:0px;
      right:0;
      bottom:0;
      li{
        float:left;
        height:33.3%;
        width:33.3%;
        position:relative;
        /*background-color: #FE6278;*/
        &::before{
          content:'';
          position:absolute;
          left:6px;
          right:6px;
          top:6px;
          bottom:6px;
          border-radius:6px;
          background-color: #666666;
        }
      }
    }

2.直接用grid网格布局

效果图

<ul :class="['box', 'box_9']" ref="container">
        <li :class="['item', i==selectedGridIndex?'item_active':'']"
          v-for="(item, i) in grids" >
        </li>
</ul>
.box{
      display: grid;
      box-sizing: border-box;
      height: 100%;
      padding: 0;
      .item{
        box-sizing: border-box;
        background-color: #666666;
        position: relative;
        border: 1px solid rgba(255, 255, 255, 0.21);
        cursor: pointer;
        position: relative;
        &_active{
          border: 1px solid #FE6278;
        }
      }
    }
    .box_1{
      grid-template-columns: repeat(1, 100%); //1列,占100%
      grid-template-rows: repeat(1, 100%); //1行,占100%
    }
    .box_4{
    //四宫格
      grid-template-columns: repeat(2, 50%); //2列,各占50%
      grid-template-rows: repeat(2, 50%);//2行,各占50%
    }
    .box_9{
    //9宫格
      grid-template-columns: repeat(3, 33.33%);//3列,各占33.33%
      grid-template-rows: repeat(3, 33.33%);//3行,各占33.33%
    }
    .box_16{
      grid-template-columns: repeat(4, 25%);//4列,各占25%
      grid-template-rows: repeat(4, 25%);//4行,各占25%
    }
### 实现宫格九宫格之间动态切换 为了实现宫格九宫格之间的动态切换功能,可以基于 Vue 的响应式特性来调整 `gridNum` 和 `gridList` 数据。以下是详细的解决方案: #### HTML 模板部分 通过绑定 `gridNum` 属性控制每行显示的列数,并利用 Vue 的计算属性或方法动态更新数据。 ```html <template> <view class="content"> <!-- 动态切换宫格 --> <ccGridButton :gridTitle="currentGridType === 4 ? '宫格' : '九宫格'" :gridNum="currentGridType" :gridList="gridData" @click="gridClick"> </ccGridButton> <!-- 切换按钮 --> <button @click="toggleGrid">切换到{{ currentGridType === 4 ? '九宫格' : '宫格' }}</button> </view> </template> ``` #### JavaScript 部分 定义一个变量用于存储当前宫格类型(如 `currentGridType`),并通过事件监听器实现切换逻辑。 ```javascript <script> export default { data() { return { currentGridType: 4, // 默认为宫格 gridData: [ { id: 1, name: "菜单项1", icon: "icon1.png" }, { id: 2, name: "菜单项2", icon: "icon2.png" }, { id: 3, name: "菜单项3", icon: "icon3.png" }, { id: 4, name: "菜单项4", icon: "icon4.png" }, { id: 5, name: "菜单项5", icon: "icon5.png" }, // 扩展至9个 { id: 6, name: "菜单项6", icon: "icon6.png" }, { id: 7, name: "菜单项7", icon: "icon7.png" }, { id: 8, name: "菜单项8", icon: "icon8.png" }, { id: 9, name: "菜单项9", icon: "icon9.png" } ] }; }, methods: { toggleGrid() { this.currentGridType = this.currentGridType === 4 ? 9 : 4; }, gridClick(item) { console.log(`点击了 ${item.name}`); } } }; </script> ``` 以上代码实现了通过按钮触发 `toggleGrid` 方法改变 `currentGridType` 值,从而动态调整宫格数量[^1]。 #### CSS 样式优化 如果希望在切换过程中加入动画效果,可以通过 CSS 进一步增强用户体验。例如,在鼠标悬停时放大单元格的效果如下所示: ```css <style scoped> .square { transition: transform 0.3s ease-in-out; } .square:hover { transform: scale(1.5); } </style> ``` 此样式借鉴自提供的 JavaScript 动画示例[^2],但改用更简洁的 CSS 方案完成相同效果。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值