首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React中组件的旋转木马?

React中组件的旋转木马?
EN

Stack Overflow用户
提问于 2020-02-28 03:03:01
回答 1查看 58关注 0票数 0

我正在用React构建一个网站,对于“新闻”部分,我有一个代表不同新闻的3个组件的列表。它们属于flexbox,我正在努力使它们具有响应性。对于移动设备,我希望只显示3个组件中的一个,并具有单击2个箭头浏览新闻的能力。与普通的图像传送带类似,但由组件组成。我如何才能做到这一点?These are the components

我把所有“新闻”放在那里的代码

代码语言:javascript
复制
  render() {
let content = <div>Loading...</div>;
if (!this.state.isLoading) {
  content = (
    <Aux>
      <New
        image={img1}
        title={this.state.news[0].title}
        text={this.state.news[0].text}
        date={this.state.news[0].date}
        classes="new-1"
      />
      <New
        image={img2}
        title={this.state.news[1].title}
        text={this.state.news[1].text}
        date={this.state.news[1].date}
        classes="new-2"
      />
      <New
        image={img3}
        title={this.state.news[2].title}
        text={this.state.news[2].text}
        date={this.state.news[2].date}
      />
    </Aux>
  );
}
return content;

}

这是“新”组件

代码语言:javascript
复制
   const New = props => {
  const imageStyle = {
    backgroundImage: `url(${props.image})`
  };

  return (
    <div className={`new-wrapper ${props.classes}`}>
      <div className="new-image" style={imageStyle}></div>
      <div className="new-content">
        <h4>{props.title}</h4>

        <div className="text-wrapper">
          <p>{props.text}</p>
        </div>

        <div className="date">
          <span>{props.date}</span>
        </div>
      </div>
    </div>
  );
};
EN

回答 1

Stack Overflow用户

发布于 2020-02-28 05:47:12

为了达到你想要的效果,我会使用像https://react-slick.neostack.com/这样的旋转木马插件

你可以设置它在桌面上显示三张幻灯片,在移动设备上只显示一张幻灯片,这样你就可以让箭头穿过新闻卡片。

我还会通过使用map函数来呈现所有新闻来循环数组的每个元素。这样,它将为状态或数组中的每个元素动态创建一个新闻卡片。请参阅此示例How to render an array of objects in React?

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60440093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档