在JavaScript中如何复制 nth-child。

huangapple go评论81阅读模式
英文:

How would I replicate nth-child in JavaScript

问题

以下是您要翻译的内容:

如标题所述,我正在尝试使用JS执行CSS的功能,仅使用映射数组中项目的索引,每个项目都有一个索引号。

computed: {
    parsedItems() {
        return this.items?.map((obj, index) => {
            return {
                ...obj,
            }
        })
    }
}

为了更好地理解,我将尝试解释为什么我要这样做。

我正在构建一个由块组成的网格,这些块具有一些数据(图像、标题、URI、一些文本等),我想要获得:nth-child(7n + 1)的功能,当然也包括+7。

但是使用JS,这个框架是Vue 2。

解析后的项目中包含obj中的图像,自定义组件正在使用这些图像,该自定义组件用于处理包括图像纵横比在内的各种图像操作。

该组件只需取图像的高度除以宽度的结果,并使用该数字设置纵横比。

经过广泛的研究,我了解了CSS中nth-child的工作原理,

:nth-child(3n + 3)

在幕后看起来是这样的:

(3 x 0) + 3 = 3 = 第3个元素
(3 x 1) + 3 = 6 = 第6个元素
(3 x 2) + 3 = 9 = 第9个元素
等等

我尝试做的是以下内容,最明显的是只需将索引乘以7,然后添加1到7的数字。就像这样:

computed: {
    parsedItems() {
        return this.items?.map((obj, index) => {
            return {
                ...obj,
                aspectRatio:
                    7 * index + 1 === 1
                        ? someAspectRatio
                        : 7 * index + 2 === 9
                            ? someAspectRatio
                            ...等等
            }
        })
    }
}

问题在于map方法不会在7处停止,然后再从1开始,所以失败了。

此外,数学计算与我需要的不符。

第二次尝试是使用模数/余数运算符"%"。

computed: {
    parsedItems() {
        return this.items?.map((obj, index) => {
            return {
                ...obj,
                aspectRatio:
                    index % 7 === 0
                        ? someAspectRatio
                        : index % 3 === 0
                            ? someAspectRatio
                            : index % 2 === 0
                                ? someAspectRatio
                                : someAspectRatio
            }
        })
    }
}

我发现这更接近一些,但还不够接近,因为随着项目数量的增加,模数结果开始混合。

英文:

as stated in the title I'm trying to do what css does but in JS using only an index of an item in a mapped array of items, each item get's an index number.

computed: {
        parsedItems() {
            return this.items?.map((obj, index) => {
                return {
                    ...obj,
                }
            })
        }
    }

For better understanding I'll try to explain why I'm doing this.

I'm building a grid of blocks that have some data (image, title, uri, some text, ...etc), and I want to get the functionality of :nth-child(7n + 1), and obviously all the way through to + 7.
But using JS, the framework is vue 2.

Parsed items have images in the obj and a custom component is using those images, that custom component is made to handle all kinds of stuff with images including aspect ratio of the image.
The component just takes the result from height divided by width of the image and uses that number to set the aspect ratio.

After extensive research I learned how nth-child works in css,

:nth-child(3n + 3)

Looks like this behind the scenes:

(3 x 0) + 3 = 3 = 3rd Element
(3 x 1) + 3 = 6 = 6th Element
(3 x 2) + 3 = 9 = 9th Element
etc.

What I tried to do is the following,
most obviously just multiplying index by 7 and adding to that number 1 through 7. like so:

computed: {
        parsedItems() {
            return this.items?.map((obj, index) => {
                return {
                    ...obj,
                    aspectRatio:
                        7 * index + 1 === 1
                            ? someAspectRatio
                            : 7 * index + 2 === 9
                                ? someAspectRatio
                                ...etc
                }
            })
        }
    }

Problem here is the map method doesn't stop at 7 and then starts it from 1 again, so this flunked.
Also, the math doesn't work out to what I need..
Second attempt was using the modulo/remainder operator "%".

computed: {
        parsedItems() {
            return this.items?.map((obj, index) => {
                return {
                    ...obj,
                    aspectRatio:
                        index % 7 === 0
                            ? someAspectRatio
                            : index % 3 === 0
                                ? someAspectRatio
                                : index % 2 === 0
                                    ? someAspectRatio
                                    : someAspectRatio
                }
            })
        }
    }

I found this to be closer but not close enough, as the module results start mixing up as the number of items increases.

答案1

得分: 1

用简单的 for 替代 map() 是使其工作的最简单方式。

function nthChild(array, startIndex, eachIndex) {
   let newArray = []
   for(let i = startIndex; i < array.length; i+=eachIndex) {
       newArray.push(array[i])
   }
   return newArray
}

因此,从 CSS 参考中取得 :nth-child(eachIndex*n + startIndex)

这是一个示例:

let ar = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
nthChild(ar, 3, 3)

输出(6) [3, 6, 9, 12, 15, 18]
英文:

The easiest way to make it work is to use simple for instead of map().

function nthChild(array, startIndex, eachIndex) {
   let newArray = []
   for(let i = startIndex; i &lt; array.length; i+=eachIndex) {
       newArray.push(array[i])
   }
   return newArray
}

So from CSS reference :nth-child(eachIndex*n + startIndex)

Here is an example:

let ar = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
nthChild(ar, 3, 3)

output: (6)&#160;[3, 6, 9, 12, 15, 18]

huangapple
  • 本文由 发表于 2023年6月13日 03:56:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定