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

关注原作者

相关推荐
IT女孩儿26 分钟前
CSS查缺补漏(补充上一条)
前端·css
吃杠碰小鸡1 小时前
commitlint校验git提交信息
前端
天天进步20152 小时前
Vue+Springboot用Websocket实现协同编辑
vue.js·spring boot·websocket
虾球xz2 小时前
游戏引擎学习第20天
前端·学习·游戏引擎
我爱李星璇2 小时前
HTML常用表格与标签
前端·html
疯狂的沙粒2 小时前
如何在Vue项目中应用TypeScript?应该注意那些点?
前端·vue.js·typescript
小镇程序员2 小时前
vue2 src_Todolist全局总线事件版本
前端·javascript·vue.js
野槐2 小时前
前端图像处理(一)
前端
程序猿阿伟2 小时前
《智能指针频繁创建销毁:程序性能的“隐形杀手”》
java·开发语言·前端
疯狂的沙粒3 小时前
对 TypeScript 中函数如何更好的理解及使用?与 JavaScript 函数有哪些区别?
前端·javascript·typescript