实现5*5正方形网格x轴和y轴显示对应数值组件封装

文章描述了如何使用Vue.js和CSS在5x5网格中根据x和y轴值动态显示文本,提供了HTML模板、TypeScript脚本和SCSS样式代码示例。
摘要由CSDN通过智能技术生成

实现5*5正方形网格x轴和y轴显示对应数值组件封装

需求:按5*5的正方形网格,根据目标数据的x和y轴值显示对应的文字,实现效果图如下:(当前目标数值:x=2,y=2)

在这里插入图片描述

代码如下:

<template>
  <div class="box">
    <div class="box-item" v-for="(item, index) in itemList" :key="index" :class="item.className">
      {{ (x === item.x && y === item.y) ? item.label : '' }}
    </div>
  </div>
</template>

<script setup lang="ts">
withDefaults(defineProps<{
  x?: number,
  y?: number
}>(), {
  x: 2,
  y: 2
})

interface itemType {
  className: string;
  label: string;
  x: number;
  y: number;
}
let itemList: itemType[] = Array.from({ length: 25 }).map((_, index) => {
  let result = {
    className: '',
    label: '中',
    x: 0,
    y: 0
  }
  if ([3, 4, 8, 9, 14].includes(index)) {
    result.className = 'blue'
    result.label = '高'
  } else if ([10, 15, 16, 20, 21].includes(index)) {
    result.className = 'orange'
    result.label = '低'
  }
  result.className += ` xy-${index}`
  return result
})
let Y = 10
for (let j = 0; j < 25; j += 5) {
  for (let i = j; i < j + 5; i++) {
    if ([0, 5, 10, 15, 20].includes(i)) {
      itemList[i].x = 2
    } else {
      itemList[i].x = itemList[i - 1].x + 2
    }
    itemList[i].y = Y
  }
  Y -= 2
}
</script>

<style lang="scss" scoped>
.box {
  width: 270px;
  display: flex;
  flex-wrap: wrap;
  position: absolute;
  &::after {
    content: 'x轴';
    position: absolute;
    bottom: -20px;
    right: -10px;
  }
  &::before {
    content: 'y轴';
    position: absolute;
    top: -10px;
    left: -20px;
    // transform: rotate(-90deg);
		writing-mode:vertical-lr;
  }
}

.box-item {
  width: 20%;
  height: 50px;
  border: 1px solid #ff0000;
  box-sizing: border-box;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  &.xy-0::after {
    content: '10';
    position: absolute;
    left: -20px;
  }
  &.xy-5::after {
    content: '8';
    position: absolute;
    left: -20px;
  }
  &.xy-10::after {
    content: '6';
    position: absolute;
    left: -20px;
  }
  &.xy-15::after {
    content: '4';
    position: absolute;
    left: -20px;
  }
  &.xy-20::after {
    content: '2';
    position: absolute;
    left: -20px;
  }
  &.xy-20::before {
    content: '2';
    position: absolute;
    bottom: -20px;
  }
  &.xy-21::before {
    content: '4';
    position: absolute;
    bottom: -20px;
  }
  &.xy-22::before {
    content: '6';
    position: absolute;
    bottom: -20px;
  }
  &.xy-23::before {
    content: '8';
    position: absolute;
    bottom: -20px;
  }
  &.xy-24::before {
    content: '10';
    position: absolute;
    bottom: -20px;
  }
}

.blue {
  background-color: skyblue;
}

.orange {
  background-color: orange;
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King_960725

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值