简体   繁体   English

根据滑块索引位置更改类名

[英]Change classname based on slider index position

I want to change the className based on the selected slider data-index position in https://stackblitz.com/edit/react-slick-slider-issues which is making use of React-slick-slider .我想根据使用React-slick-slider 的https://stackblitz.com/edit/react-slick-slider-issues 中选定的滑块数据索引位置更改className

Suppose if the user hovers on the first image, the image data-index value is 0 and I want to push the image above the slider and onMouseOut the image will go back to its original place in the slider and do this for the last image as well.假设如果用户将鼠标悬停在第一张图像上,图像数据索引值为 0,我想将图像推到滑块上方,并且onMouseOut图像将返回到滑块中的原始位置并对最后一张图像执行此操作好。

For all the images in between the first and last images, they will be pushed down.对于第一张和最后一张图像之间的所有图像,它们将被下推。 currently hovering on an image they will be pushed down and onMouseOut they will go back to its original place in the slider.当前悬停在图像上,它们将被按下, onMouseOut它们将返回到滑块中的原始位置。

If className by default is unique-image can it be changed for the First Image as unique-image First , if its the last image then className should be unique-image Last and for all the in-between images it should be unique-image between .如果className默认为unique-image可以将 First Image 更改为unique-image First ,如果是最后一张图像,则className应为unique-image Last并且对于所有中间图像,它应为unique-image between . is this something that can be achieved?这是可以实现的吗?

As you are mapping over the images, you could use a tertiary operator:当您映射图像时,您可以使用三级运算符:

const images = [...something];

images.map((image, key) => <img className={`unique-image ${key === 0 ? "First" : key == images.length - 1 ? "Last" : ""}`} {...otherProps} />

So you use the key from the map function to check for the first and last element and add the respective classes when reached.因此,您使用 map 函数中的键来检查第一个和最后一个元素,并在到达时添加相应的类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM