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

关注原作者

相关推荐
oil欧哟3 分钟前
🧐 AI 批量检查数千份技术文档,如何实现高效文档纠错?
前端·人工智能·ai编程
江城开朗的豌豆5 分钟前
Vue组件data必须用函数?这个设计暗藏玄机!
前端·javascript·vue.js
前端小巷子12 分钟前
web域名解析
前端·javascript·面试
LaoZhangAI13 分钟前
沉浸式翻译API深度解析:500万用户的翻译神器如何配置[2025完整指南]
前端·后端
然我20 分钟前
链表指针玩不转?从基础到双指针,JS 实战带你破局
前端·数据结构·算法
江城开朗的豌豆20 分钟前
组件封装实战:如何设计灵活又好用的前端组件?
前端·javascript·vue.js
EndingCoder28 分钟前
算法与前端的可访问性
前端·算法·递归·树形结构
brzhang35 分钟前
别再梭哈 Curosr 了!这 AI 神器直接把需求、架构、任务一条龙全干了!
前端·后端·架构
Kagol44 分钟前
TinyEditor v4.0 alpha 版本发布,更强大的表格、更丰富的表情、体验更好的图片/视频/文件上传功能
前端·开源
The_era_achievs_hero1 小时前
微信131~140
开发语言·javascript·微信