You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于实现模态框滑块图片与对应描述同步切换的技术咨询

解决滑块图片与描述文本同步切换的问题

Hey there! I see what you're trying to do—right now you're rendering all your descriptions at once, which is why they're all showing up instead of matching the active slide. Let's fix this so your description updates automatically when the slider switches images.

Step 1: Add state to track the current slide index

First, we need to keep track of which slide is active in your component's state. If this is a class component, add this to your constructor:

constructor(props) {
  super(props);
  // Initialize with the first slide as active
  this.state = {
    currentSlideIndex: 0
  };
}

Step 2: Hook into the slider's change event

The AwesomeSlider component provides an onChange callback that fires when the slide changes. We'll use this to update our state with the current slide index:

<AwesomeSlider 
  cssModule={[AwesomeSliderStyles, AwesomeSliderStyles2]} 
  animation="scaleOutAnimation" 
  className="slider-image"
  // Update state whenever the slide changes
  onChange={(currentIndex) => this.setState({ currentSlideIndex: currentIndex })}
>
  {img}
</AwesomeSlider>

Step 3: Render only the active description

Instead of rendering all your descriptions with {descr}, we'll pick the one that matches the current slide index. We'll also add a safety check to avoid errors if the description array is shorter than the images array:

Replace your existing {descr} line with this:

{description && description.length > this.state.currentSlideIndex && (
  <p className="modal-description">{description[this.state.currentSlideIndex]}</p>
)}

You can tidy up your variable declarations to use const/let instead of var, and make the logic cleaner:

const { data } = this.props;
let img = [];
let description = [];

if (data) {
  const { images, description: descArray } = data;
  description = descArray;
  
  if (images) {
    img = images.map((elem, i) => (
      <div key={i} data-src={elem} />
    ));
  }
}

Full modified code snippet

Putting it all together, your component's relevant sections will look like this:

constructor(props) {
  super(props);
  this.state = {
    currentSlideIndex: 0
  };
}

render() {
  const { title, url } = this.props.data || {};
  const { data } = this.props;
  let img = [];
  let description = [];

  if (data) {
    const { images, description: descArray } = data;
    description = descArray;
    
    if (images) {
      img = images.map((elem, i) => (
        <div key={i} data-src={elem} />
      ));
    }
  }

  return (
    <>
      <div>
        <AwesomeSlider 
          cssModule={[AwesomeSliderStyles, AwesomeSliderStyles2]} 
          animation="scaleOutAnimation" 
          className="slider-image"
          onChange={(currentIndex) => this.setState({ currentSlideIndex: currentIndex })}
        >
          {img}
        </AwesomeSlider>
      </div>
      <div className="col-md-10 mx-auto">
        <h3 style={{ padding: "5px 5px 0 5px" }}>
          {title} {url ? (
          <a href={url} target="_blank" rel="noopener noreferrer" className="link-href" >
            <i className="fas fa-external-link-alt" style={{ marginLeft: "10px" }} ></i>
          </a>
          ) : null}
        </h3>
        {/* Only render the description matching the active slide */}
        {description && description.length > this.state.currentSlideIndex && (
          <p className="modal-description">{description[this.state.currentSlideIndex]}</p>
        )}
      </div>
    </>
  );
}

This way, every time the user clicks the slider's left/right buttons, the onChange event updates the current slide index in state, and the description automatically switches to match the active image.

内容的提问来源于stack exchange,提问作者Emm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 12:34:09