vue3:瀑布流

vue3:瀑布流

父组件

javascript 复制代码
<template>
 <waterFallVue :list="list"></waterFallVue>
</template>
<script setup lang='ts'>
import { ref, reactive, onMounted } from 'vue';
import waterFallVue from './components/water-fall.vue';
let list=[
  {
    height:300,
    background:"red"
  },
  {
    height:400,
    background:"pink"
  },
  {
    height:500,
    background:"blue"
  },
  {
    height:200,
    background:"yellow"
  },
  {
    height:100,
    background:"orange"
  },
  {
    height:150,
    background:"black"
  },
  {
    height:350,
    background:"grey"
  },
  {
    height:450,
    background:"brown"
  },
  {
    height:300,
    background:"red"
  },
  {
    height:400,
    background:"pink"
  },
  {
    height:500,
    background:"blue"
  },
  {
    height:200,
    background:"yellow"
  },
  {
    height:100,
    background:"orange"
  },
  {
    height:150,
    background:"black"
  },
  {
    height:350,
    background:"grey"
  },
  {
    height:450,
    background:"brown"
  },{
    height:300,
    background:"red"
  },
  {
    height:400,
    background:"pink"
  },
  {
    height:500,
    background:"blue"
  },
  {
    height:200,
    background:"yellow"
  },
  {
    height:100,
    background:"orange"
  },
  {
    height:150,
    background:"black"
  },
  {
    height:350,
    background:"grey"
  },
  {
    height:450,
    background:"brown"
  },
  {
    height:300,
    background:"red"
  },
  {
    height:400,
    background:"pink"
  },
  {
    height:500,
    background:"blue"
  },
  {
    height:200,
    background:"yellow"
  },
  {
    height:100,
    background:"orange"
  },
  {
    height:150,
    background:"black"
  },
  {
    height:350,
    background:"grey"
  },
  {
    height:450,
    background:"brown"
  },
  {
    height:500,
    background:"blue"
  },
  {
    height:200,
    background:"yellow"
  },
  {
    height:100,
    background:"orange"
  },
  {
    height:150,
    background:"black"
  },
  {
    height:350,
    background:"grey"
  },
  {
    height:450,
    background:"brown"
  },
  {
    height:500,
    background:"blue"
  },
  {
    height:200,
    background:"yellow"
  },
  {
    height:100,
    background:"orange"
  },
  {
    height:150,
    background:"black"
  },
  {
    height:350,
    background:"grey"
  },
  {
    height:450,
    background:"brown"
  },
  {
    height:500,
    background:"blue"
  },
  {
    height:200,
    background:"yellow"
  },
  {
    height:100,
    background:"orange"
  },
  {
    height:150,
    background:"black"
  },
  {
    height:350,
    background:"grey"
  },
  {
    height:450,
    background:"brown"
  },
  {
    height:500,
    background:"blue"
  },
  {
    height:200,
    background:"yellow"
  },
  {
    height:100,
    background:"orange"
  },
  {
    height:150,
    background:"black"
  },
  {
    height:350,
    background:"grey"
  },
  {
    height:450,
    background:"brown"
  }
]
</script>
<style lang="scss">
// #app{
//   @include bfc;
// }
</style>

子组件 water-fall.vue

javascript 复制代码
<template>
  <div class="wrap">
  <div :style="{height:item.height+'px',background:item.background,left:item.left+'px',top:item.top+'px'}" v-for="item in waterList" class="items"></div>
  </div>

</template>
<script setup lang='ts'>
import { ref, reactive, onMounted} from 'vue'
const props=defineProps<{
  list:any[]
}>()
const waterList=reactive<any[]>([]);
const heightList:number[]=[];
const init = () => {
  const width=130;
  const x=document.body.clientWidth;
  const column=Math.floor(x / width);
  console.log(column);

  for(let i=0;i<props.list.length;i++){
    if(i<column){
      props.list[i].left=i*width;
      props.list[i].top=20;
      waterList.push(props.list[i]);
      heightList.push(props.list[i].height);
    }else{
      let current=heightList[0];
      let index=0;
      heightList.forEach((h,i)=>{
        if(current>h){
          current=h;
          index=i;
        }
      })
      props.list[i].top= current + 20;
      props.list[i].left= index * width;
      heightList[index]=heightList[index] + props.list[i].height + 20;
      waterList.push(props.list[i])
      console.log(current);
      
    }
  }
  // console.log(waterList);
  
}

onMounted(()=>{
  init();
})
</script>
<style scoped lang="scss">
.wrap{
  position: relative;
  .items{
    position: absolute;
    width: 120px;
  }
}
</style>

原文:

https://mp.weixin.qq.com/s?__biz=Mzg3NTAzMzAxNA==&mid=2247484363&idx=2&sn=56940d25e196a0b2ecc77ebb1c491f7c&chksm=cec6fb12f9b1720460fd331fcd36c5260d0599bc4aab2c764c3a08c247f724440c8745b43bcf&token=821792727&lang=zh_CN#rd

关注原作者

相关推荐
码哥DFS14 分钟前
Flex布局原理
前端·css·css3
小样还想跑30 分钟前
axios无感刷新token
前端·javascript·vue.js
字节跳跃者31 分钟前
为什么Java已经不推荐使用Stack了?
javascript·后端
字节跳跃者31 分钟前
深入剖析HashMap:理解Hash、底层实现与扩容机制
javascript·后端
Java水解39 分钟前
一文了解Blob文件格式,前端必备技能之一
前端
用户3802258598241 小时前
vue3源码解析:响应式机制
前端·vue.js
bo521001 小时前
浏览器渲染机制详解(包含渲染流程、树结构、异步js)
前端·面试·浏览器
时间会给答案scidag1 小时前
报错 400 和405解决方案
vue.js·spring boot
普通程序员1 小时前
Gemini CLI 新手安装与使用指南
前端·人工智能·后端
Web小助手1 小时前
js高级程序设计(日期)
javascript