实现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>
相关推荐
Mintopia3 分钟前
像素的进化史诗:计算机图形学与屏幕的千年之恋
前端·javascript·计算机图形学
Mintopia6 分钟前
Three.js 中三角形到四边形的顶点变换:一场几何的华丽变身
前端·javascript·three.js
归于尽20 分钟前
async/await 从入门到精通,解锁异步编程的优雅密码
前端·javascript
晓131337 分钟前
JavaScript加强篇——第六章 定时器(延时函数)与JS执行机制
开发语言·javascript·ecmascript
yanlele1 小时前
【实践篇】【01】我用做了一个插件, 点击复制, 获取当前文章为 Markdown 文档
前端·javascript·浏览器
LeeAt1 小时前
手把手教你构建自己的MCP服务器并把它连接到你的Cursor
javascript·cursor·mcp
前端风云志2 小时前
TypeScript枚举类型应用:前后端状态码映射的最简方案
javascript
望获linux2 小时前
【实时Linux实战系列】多核同步与锁相(Clock Sync)技术
linux·前端·javascript·chrome·操作系统·嵌入式软件·软件
markyankee1012 小时前
Vue 组件系统深度解析
vue.js
小飞悟2 小时前
JavaScript 中的“伪私有”与“真私有”:你以为的私有变量真的安全吗?
javascript