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

关注原作者

相关推荐
F-2H35 分钟前
C语言:指针4(常量指针和指针常量及动态内存分配)
java·linux·c语言·开发语言·前端·c++
苹果酱056738 分钟前
「Mysql优化大师一」mysql服务性能剖析工具
java·vue.js·spring boot·mysql·课程设计
gqkmiss1 小时前
Chrome 浏览器插件获取网页 iframe 中的 window 对象
前端·chrome·iframe·postmessage·chrome 插件
m0_748247553 小时前
Web 应用项目开发全流程解析与实战经验分享
开发语言·前端·php
m0_748255024 小时前
前端常用算法集合
前端·算法
真的很上进4 小时前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
web130933203984 小时前
vue elementUI form组件动态添加el-form-item并且动态添加rules必填项校验方法
前端·vue.js·elementui
NiNg_1_2345 小时前
Echarts连接数据库,实时绘制图表详解
前端·数据库·echarts
如若1235 小时前
对文件内的文件名生成目录,方便查阅
java·前端·python
滚雪球~6 小时前
npm error code ETIMEDOUT
前端·npm·node.js