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

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

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

代码如下:

vue 复制代码
<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>
相关推荐
工业甲酰苯胺4 小时前
TypeScript枚举类型应用:前后端状态码映射的最简方案
javascript·typescript·状态模式
谢尔登8 小时前
【React Native】ScrollView 和 FlatList 组件
javascript·react native·react.js
然我8 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法
kk_stoper9 小时前
如何通过API查询实时能源期货价格
java·开发语言·javascript·数据结构·python·能源
晨枫阳9 小时前
前端VUE项目-day1
前端·javascript·vue.js
颜酱9 小时前
抽离ant-design后台的公共查询设置
前端·javascript·ant design
绅士玖9 小时前
JavaScript 设计模式之单例模式🚀
前端·javascript·设计模式
Dream耀9 小时前
useReducer:React界的"灭霸手套",一个dispatch搞定所有状态乱局
前端·javascript·react.js
余大侠在劈柴10 小时前
pdf.js 开发指南:在 Web 项目中集成 PDF 预览功能
前端·javascript·学习·pdf
拾光拾趣录10 小时前
JavaScript屏幕切换检测方案
前端·javascript