zoukankan      html  css  js  c++  java
  • cs3超简单实现瀑布流布局

    废话少说,coding....

    html部分

    <div class="pubu">
          <div class="p p1">
            243789793
          </div>
          <div class="p p2">
            45634058906834
          </div>
          <div class="p p3">
            563456346
          </div>
          <div class="p p4">
            "市解放路所经历的"
          </div>
          <div class="p p5">
            "的减肥了困难八年级看完"
          </div>
          <div class="p p6">
            "大家啊发来看精灵盛典啊"
          </div>
        </div>
    

      css部分

    .pubu {
        backgroud: #ddd;
      /*分2列*/ column-count: 2;   -moz-column-count:2; /*firefox*/
      -webkit-column-count:2;/*safari chrome*/ } .p { break-inside: avoid; } .p1 { background: #0ba360; with: 50%; height: 50px; } .p2 { background: #0c3483; with: 50%; height: 70px; } .p3 { background: #00a0e9; with: 50%; height: 90px; } .p4 { background: #1B5E20; with: 50%; height: 20px; } .p5 { background: #3C4858; with: 50%; height: 60px; } .p6 { background: #6a00ff; with: 50%; height: 100px; }

      效果图

    瀑布

     什么?发现分布顺序不对?

    【1,2,3,4,5,6】;左边分布是【1,3,5】;右边【2,4,6】;
    很简单,通过js处理下,把【1,2,3,4,5,6】处理成【1,3,5,2,4,6】;

    大致逻辑如下:

    const oldList = [1, 2, 3, 4, 5, 6]
    
    // 使用reduce函数接受一个初始值{ 0: [], 1: [], length: 2 },
    // 初始值包含两个空数组,和一个数组长度(Array.from方法要求将对象转数组时对象内要有这个属性)
    // 在reduce函数内根据索引做余2判断,因为分两,余0的加入第一个数组,余1的加入第二个数组
    // 最后reduce返回遍历完的对象 {0:[1,3,5],1:[2,4,6],length:2}
    // 使用Array.from({0:[1,3,5],1:[2,4,6],length:2}) 得到 数组 [[1,3,5],[2,4,6]]
    // 解构数组 使用concat合并,完事
    const newList = [].concat(...(Array.from(oldList.reduce((total, cur, index) => {
      total[index % 2].push(cur)  
      return total
    }, { 0: [], 1: [], length: 2 }))))
    
    console.log(newList)
    

      



  • 相关阅读:
    太白老师 day06 编码 encode decode
    太白老师day6 1.代码块 2.is==id 3.小数据池
    MySQL 基本语法(1.表字段操作,2表记录管理 3.运算符管理4.SQL查询 5.约束6.索引
    List 接口常用子类及其特点
    Java 集合框架
    Java 常用工具类之基本对象包装类
    Java 常用工具类之 String 类
    Java 多线程间通信
    Java 多线程通信之多生产者/多消费者
    Java 之多线程通信(等待/唤醒)
  • 原文地址:https://www.cnblogs.com/jason-hhc/p/13217550.html
Copyright © 2011-2022 走看看