如何在Array.map中为特定实体应用样式属性?

问题描述 投票:0回答:1
const imageHtml = imageDescription.map((article, index) => {
            return (
                <div style={{ backgroundImage: "url(/images/Large/" + article.imagePath + ".jpg)" }}>

                </div>
            )
        })

我正在做一个滑块的事情,但我遇到了一个问题,如何应用特定文章的左侧属性?例如,仅对索引1应用left = 20px?

javascript reactjs jsx
1个回答
1
投票

你可以给其余的左= 0:

<div style={{backgroundImage: ..., left: (index === 1 ? 20 : 0)}} />

或者,如果您不想包括它:

<div style={Object.assign({backgroundImage: ...}, (index === 1 ? {left: 20} : {}))} />
© www.soinside.com 2019 - 2024. All rights reserved.