瀑布流的简单实现(vue3+ts)

效果图:
在这里插入图片描述

1.通过父组件传递基础数据给子组件

<template>
  <waterFall :list="list"></waterFall>
</template>

<script setup lang='ts'>
import waterFall from './components/water-fall.vue';

const list = [
  {
    height: 300,
    background: 'red'
  },
  {
    height: 400,
    background: 'pink'
  },
  {
    height: 500,
    background: 'blue'
  },
  {
    height: 300,
    background: 'gray'
  },
  {
    height: 400,
    background: '#CC00FF'
  },
  {
    height: 200,
    background: 'black'
  },
  {
    height: 100,
    background: '#996666'
  },
  {
    height: 500,
    background: 'skyblue'
  },
  {
    height: 300,
    background: '#993366'
  },
  {
    height: 100,
    background: '#33FF33'
  },
  {
    height: 400,
    background: 'skyblue'
  },
  {
    height: 200,
    background: '#6633CC'
  },
  {
    height: 300,
    background: '#666699'
  },
  {
    height: 300,
    background: '#66CCFF'
  },
  {
    height: 300,
    background: 'skyblue'
  },
  {
    height: 200,
    background: '#CC3366'
  },
  {
    height: 200,
    background: '#CC9966'
  },
  {
    height: 200,
    background: '#FF00FF'
  },
  {
    height: 500,
    background: '#990000'
  },
  {
    height: 400,
    background: 'red'
  },
  {
    height: 100,
    background: '#999966'
  },
  {
    height: 200,
    background: '#CCCC66'
  },
  {
    height: 300,
    background: '#FF33FF'
  },
  {
    height: 400,
    background: '#FFFF66'
  },
  {
    height: 200,
    background: 'red'
  },
  {
    height: 100,
    background: 'skyblue'
  },
  {
    height: 200,
    background: '#33CC00'
  },
  {
    height: 300,
    background: '#330033'
  },
  {
    height: 100,
    background: '#0066CC'
  },
  {
    height: 200,
    background: 'skyblue'
  },
  {
    height: 100,
    background: '#006666'
  },
  {
    height: 200,
    background: 'yellow'
  },
  {
    height: 300,
    background: 'yellow'
  },
  {
    height: 100,
    background: '#33CCFF'
  },
  {
    height: 400,
    background: 'yellow'
  },
]

</script>

<style>
html,
body {
  padding: 0;
  margin: 0;
}
</style>

2.子组件接收数据并展示

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

<script setup lang='ts'>
import { onMounted, reactive } from 'vue'

const props = defineProps<{
  list: any[]
}>()
const waterList = reactive<any[]>([])
const heightList = reactive<Array<number>>([])
// 瀑布流思路:首先算出第一行可以排列的列数,然后后续开始 盒子依次填充到第一行高度最小的盒子下面,维护第一行的高度的变化;
const init = () => {
  const width = 130
  const x = document.body.clientWidth
  const column = Math.floor(x / width)
  console.log('column', 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', current);
      // console.log('index', index);
    }
  }
}

onMounted(() => {
  init()
})
</script>

<style scoped lang="less">
.wraps {
  position: relative;

  .items {
    position: absolute;
    width: 120px;
  }
}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 Vue 3 + TypeScript + Vite 项目,你可以使用 Vue 的官方插件 `vue-axios` 或者 `vue-resource` 来实现网络通信。下面以 `vue-axios` 为例,给出步骤: 1. 安装依赖: ```shell npm install axios vue-axios ``` 2. 在项目的入口文件(一般是 `main.ts`)中引入和使用插件: ```typescript import { createApp } from 'vue' import axios from 'axios' import VueAxios from 'vue-axios' import App from './App.vue' const app = createApp(App) app.use(VueAxios, axios) app.mount('#app') ``` 3. 在需要发送网络请求的组件中使用: ```vue <template> <div> <button @click="fetchData">发送请求</button> <ul> <li v-for="item in dataList" :key="item.id">{{ item.text }}</li> </ul> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' import { AxiosInstance } from 'axios' export default defineComponent({ data() { return { dataList: [] } }, methods: { fetchData() { // 发送 GET 请求示例 this.axios .get('/api/data') .then((response) => { this.dataList = response.data }) .catch((error) => { console.error(error) }) } }, mounted() { // 在 mounted 钩子中,this.axios 可以直接使用 this.fetchData() } }) </script> ``` 在上述示例中,我们通过 `VueAxios` 插件将 `axios` 实例注入到了 Vue 的实例中,在组件中可以通过 `this.axios` 直接使用。你可以根据接口文档的要求来选择合适的请求方法(例如 `get`、`post`、`put` 等),然后处理返回的结果。 当然,如果你对其他网络请求库或者自己封装的网络请求工具库更感兴趣,也可以在 Vue 3 + TypeScript + Vite 项目中自行选择使用。以上仅供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值