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

关注原作者

相关推荐
人工智能训练师5 小时前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js
Seveny075 小时前
pnpm相对于npm,yarn的优势
前端·npm·node.js
yddddddy6 小时前
css的基本知识
前端·css
昔人'6 小时前
css `lh`单位
前端·css
前端君7 小时前
实现最大异步并发执行队列
javascript
Nan_Shu_6148 小时前
Web前端面试题(2)
前端
知识分享小能手8 小时前
React学习教程,从入门到精通,React 组件核心语法知识点详解(类组件体系)(19)
前端·javascript·vue.js·学习·react.js·react·anti-design-vue
蚂蚁RichLab前端团队9 小时前
🚀🚀🚀 RichLab - 花呗前端团队招贤纳士 - 【转岗/内推/社招】
前端·javascript·人工智能
孩子 你要相信光9 小时前
css之一个元素可以同时应用多个动画效果
前端·css
萌萌哒草头将军9 小时前
Oxc 和 Rolldown Q4 更新计划速览!🚀🚀🚀
javascript·vue.js·vite