简体   繁体   English

我正在创建一个数组以响应打印图像 select 仅基于数字我想要的图像

[英]i am create an array in react to print images select only images i want based on to numbers

i am running into a tricky problem, i am creating an array of 100 elements, and mapping through it an generating an image for each:我遇到了一个棘手的问题,我正在创建一个包含 100 个元素的数组,并通过它映射为每个元素生成一个图像:

 {Array.from({ length: 100 }).map((_, i) => ( <> <img key={i} className={`h-12 object-contain cursor-pointer `} src="/images/panneau.png" alt="" /> </> ))}

here is what i get:这是我得到的: 在此处输入图像描述 what i want:我想要的是:

  • for example i have tow numbers 5 and 6 i want to add opacity-50 to this images in the range of 6 * 5:例如,我有两个数字 5 和 6,我想在 6 * 5 范围内将 opacity-50 添加到此图像中: 在此处输入图像描述

The problem you are tying to solve is converting a flat array index to a 2 dimentional array indexes, then you can use those to apply conditional code.您要解决的问题是将平面数组索引转换为二维数组索引,然后您可以使用它们来应用条件代码。

Let's say you want to have 10 images for each row, I am calling this WIDTH.假设您希望每行有 10 张图像,我称之为 WIDTH。

you can get the current row index with the formula: y = Math.floor(i / WIDTH);您可以使用以下公式获取当前行索引: y = Math.floor(i / WIDTH); and you can get the current index in each row with: x = i % WIDTH;您可以使用以下命令获取每行中的当前索引: x = i % WIDTH;

now that you have those you can do something like:现在你有了那些你可以做的事情:

 function MyComponent() { const WIDTH = 10; //number of images in one row return <> {Array.from({ length: 100 }).map((_, i) => { x = Math.floor(i / WIDTH); y = i % WIDTH; opacity = (x <= 5) && (y <= 4)? ' opacity-50': ''; return ( <img key={i} className={`h-12 object-contain cursor-pointer` + opacity} src="/images/panneau.png" alt="" /> ); })} </> }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有多个图像路径的数组,我想使用循环打印图像 - I have array with multiple images path and i want to print images using loop 我想从包含图像 react-native 的链接/网址的数组中返回平面列表中的图像 - I want to return images in flatlist from an array that contians links/urls for images react-native 想显示投资组合图片,但我遇到了一些错误 - want to show portfolio images but i am facing some error 我只想在 Javascript 中使用 Arrays 打印 1 到 100 个数字 - I Want To Print 1 to 100 Numbers Using Arrays In Javascript Only 我想使用 react 以 3*X 的网格方式显示图像 - I want to display images in a grid fashion of 3*X using react 我想要上传更多图片 - i want more images upload 我想一次更改 2 个图像 - I want to change 2 images at once 我有一个包含图像和文档的对象数组,我想检查 mime_type 并选择要显示的第一个元素(React)<img> 标签 - I have a array of objects containing images and documents, I want to check the mime_type and pick the first element (React) to display in <img> tag 我想在javascript中预加载图像数组并暂停直到所有图像都加载完毕 - I want to preload an array of images in javascript and pause until they are all loaded 我想将图像放入 <div> 代替图像数组 - I want to put my images in a <div> instead of an image array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM