首页 > 其他分享 >瀑布流练习

瀑布流练习

时间:2024-06-03 17:43:17浏览次数:25  
标签:originList const 练习 height 瀑布 background heightList 方块

瀑布流练习题

0. 瀑布流的形成

各种高度不同的方块平铺。需要注意的是,第二行开始,选高度最小的方块位置插入新的方块,即需要记住当前高度数组。

1. 父组件 瀑布流的容器

这里的number是为了验证是否如开头所言,插入选取位置最矮的

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

<script setup lang="ts">
import { ref, reactive } from 'vue'
import WaterFall from '@/components/WaterFallCom/WaterFall/index.vue'

const list = [
  { height: 300, background: 'red' },
  { height: 400, background: 'pink' },
  { height: 100, background: 'black' },
  { height: 200, background: 'purple' },
  { height: 700, background: 'yellow' },
  { height: 800, background: 'blue' },
  { height: 900, background: 'grey' },
  // 第二轮
  { height: 110, background: 'grey' },
  { height: 420, background: 'black' },
  { height: 210, background: 'pink' },
  { height: 290, background: 'blue' },
  { height: 510, background: 'yellow' },
  { height: 300, background: 'green' },
  { height: 100, background: 'red' },
  // 第三轮
  { height: 800, background: 'red' },
  { height: 100, background: 'pink' },
  { height: 50, background: 'black' },
  { height: 120, background: 'purple' },
  { height: 300, background: 'yellow' },
  { height: 400, background: 'blue' },
  { height: 510, background: 'grey' },
  // 第四轮
  { height: 300, background: 'red' },
  { height: 400, background: 'pink' },
  { height: 100, background: 'black' },
  { height: 200, background: 'purple' },
  { height: 700, background: 'yellow' },
  { height: 800, background: 'blue' },
  { height: 900, background: 'grey' }
].map((item, i) => ({ ...item, number: i }))
</script>

<style scoped lang="scss"></style>

2. 子组件 绘制瀑布流方块

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

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
// 从父组件中获得值
const props = defineProps<{ list: any[] }>()
const originList = props.list
const waterFallList = reactive([])

const onInit = () => {
  const heightList: any[] = []
  const width = 130
  // 获得可视宽度
  const x = document.body.clientWidth
  // 一行能容纳的个数
  const column = Math.floor(x / width)
  const originTop = 10

  for (let i = 0; i < originList.length; i++) {
    // 第一排
    const isFirstFloor = i < column
    if (isFirstFloor) {
      originList[i].top = originTop
      originList[i].left = i * width
      // 顶部高度
      heightList.push(originList[i].height + originTop)
      // 放入瀑布流数组中
      waterFallList.push(originList[i])
    } else {
      // 往后的需要注意维护高度数组,找最小的高度插入
      // 找出当前最小的高度,插入新的方块
      let current = heightList[0] // 比大小第一个
      let minIndex = 0 // 默认序号是0
      heightList.forEach((h, j) => {
        // 将最小的保存
        if (h < current) {
          current = h
          minIndex = j
        }
      })
      // 当前找出的最小高度 + 顶格间距
      originList[i].top = current + originTop
      // 这里的左侧间距,是放置在最矮方块的下一行
      originList[i].left = minIndex * width
      // 同列上一个最小高度方块的高度 + 这一个方块的高度 + 两者之间间隔
      heightList[minIndex] = heightList[minIndex] + originList[i].height + 20
      // 将定位完成的方块 放入瀑布流中
      waterFallList.push(originList[i])
    }
  }
}

onMounted(() => {
  window.onresize = () => onInit()
  onInit()
})
</script>

<style scoped lang="scss">
// 子绝父相能够使子节点按照父节点的标准进行绝对定位.
// 父级需要占有位置,因此是相对定位,子盒子不需要占有位置,则是绝对定位
.wraps {
  position: relative;
  .items {
    position: absolute;
    width: 120px;
  }
}
</style>

3. 实际图

pkJCfqx.md.png

pkJCfqx.png

标签:originList,const,练习,height,瀑布,background,heightList,方块
From: https://www.cnblogs.com/lepanyou/p/18229355

相关文章

  • 一些题目练习。
    picoctf_2018_echoback32位格式化字符串,可写got表。fmtstr_payload写puts@got为vuln、printf@got为system@plt一把梭frompwncyimport*context(log_level="debug",arch="i386")filename="PicoCTF_2018_echo_back"remote_libc="/home/tw0/D......
  • C语言-for循环之穷举法练习
    //需求:求出一个偶数纸币需要多少张50元,20元,10元来配合include<stdio.h>intmain(void){intn=0;//存储你输入的数值inti=0;//for循环使用intj=0;intk=0;intw=50;定义50元的数值inte=20;定义20元的数值ints=10;定义10元的数值printf("pleaseente......
  • 【跟着例子学MySQL】学以致用 -- 综合练习
    文章目录前言回顾租赁系统数据库练习高级练习前言举例子,是最简单有效的学习方法。本系列文章以一个贯穿始终的场景,结合多个实例讲解MySQL的基本用法。❔为什么要写这个系列?模仿是最好的老师,实践是检验成果的方法。本系列以实操样例和应用场景为核心,将MySQL基本......
  • 双指针练习:盛水最多的容器
    题目链接:11.盛水最多的容器题目描述:给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i,0) 和 (i,height[i]) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。返回容器可以储存的最大水量。说明:你不能倾斜......
  • 双指针练习:复写0
    1.题目链接:1089.复写零2.题目描述:给你一个长度固定的整数数组 arr ,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移。注意:请不要在超过该数组长度的位置写入元素。请对输入的数组 就地 进行上述修改,不要从函数返回任何东西。3.解法(原地复写-双指针):算法......
  • C语言练习题之——从简单到烧脑(13)(每日两道)
    打印爱心1.1:普通输出爱心#include<stdio.h>intmain(){ printf("******************\n");//7(代表边上的空格) printf("******************************\n");//4 printf("************************************\n&quo......
  • 螺旋矩阵练习
    59.螺旋矩阵II题目介绍:给你一个正整数n,生成一个包含1到n2所有元素,且元素按顺时针顺序螺旋排列的nxn正方形矩阵matrix。示例1:输入:n=3输出:[[1,2,3],[8,9,4],[7,6,5]]示例2:输入:n=1输出:[[1]]提示:1<=n<=20思路:本题主要就是模拟螺旋......
  • 动手学深度学习4.6 暂退法-笔记&练习(PyTorch)
    以下内容为结合李沐老师的课程和教材补充的学习笔记,以及对课后练习的一些思考,自留回顾,也供同学之人交流参考。本节课程地址:丢弃法_哔哩哔哩_bilibili本节教材地址:4.6.暂退法(Dropout)—动手学深度学习2.0.0documentation(d2l.ai)本节开源代码:...>d2l-zh>pytorch>chapter......
  • C++Primer Plus第十一章类的使用,课后练习2,还是醉汉回家的故事 3,最慢和最快及平均概率
    修改程序清单11.15,使之报告N次测试中的最高、最低和平均步数(其中N是用户输入的整数)而不是报告每次测试的结果。头文件和实现文件不变,这里为大家方便还是贴上代码//vect.h--Vectorclasswith<<,modestate#if1#ifndef VECTOR_H_ #defineVECTOR_H_#include<io......
  • C++Primer Plus第十一章类的使用,课后练习1,还是醉汉回家的故事
    编程练习11.91.修改程序清单11.5,使之将一系列连续的随机漫步者位置写入到文件中。对于每个位置,用步号进行标示。另外,让该程序将初始条件(目标距离和步长)以及结果小结写入到该文件中。该文件的内容与下面类似:TargetDistance:100,stepSize:200:(xy)=(0,0)1:(x,y)=(-11.4......