React轮播图优化:通过延迟 + 动画的组合,彻底消除视觉上的闪烁感

  1. 添加 showCarousel 状态:控制轮播图何时显示
  2. 延迟显示轮播图:数据加载完成后,等待 300ms 再显示轮播图,让 loading 至少显示一段时间
  3. 淡入动画:轮播图出现时带有 0.5 秒的淡入效果,使过渡更加平滑自然
  4. 双重保障:通过延迟 + 动画的组合,彻底消除视觉上的闪烁感

这样修改后,即使数据加载很快,用户也会看到 loading 短暂显示,然后轮播图以淡入的方式平滑出现,不会有突兀的闪烁感了。

js 复制代码
// ... existing code ...

export default class Index extends React.Component {
  state = {
    swipers: [],
    isSwiperLoaded: false,
    showCarousel: false,
  };

  async getSwipers() {
    const res = await axios.get("http://localhost:8080/home/swiper");

    this.setState({
      swipers: res.data.body,
      isSwiperLoaded: true,
    });

    setTimeout(() => {
      this.setState({
        showCarousel: true,
      });
    }, 300);
  }

  componentDidMount() {
    this.getSwipers();
  }

  renderSwipers() {
    return this.state.swipers.map((item) => (
      <a
        key={item.id}
        href="http://www.alipay.com"
        style={{
          display: "inline-block",
          width: "100%",
          height: 212,
        }}
      >
        <img
          src={`http://localhost:8080${item.imgSrc}`}
          alt=""
          style={{ width: "100%", verticalAlign: "top" }}
        />
      </a>
    ));
  }

  renderNavs() {
    return navs.map((item) => (
      <Flex.Item
        key={item.id}
        onClick={() => {
          this.props.history.push(item.path);
        }}
      >
        <img src={item.img} alt="" />
        <h2>{item.title}</h2>
      </Flex.Item>
    ));
  }

  render() {
    return (
      <div className="index">
        <div className="swiper-container">
          {this.state.isSwiperLoaded && this.state.showCarousel ? (
            <Carousel autoplay={true} infinite autoplayInterval="2000" className="carousel-fade">
              {this.renderSwipers()}
            </Carousel>
          ) : (
            <div className="loading-wrapper">
              <ActivityIndicator size="large" />
            </div>
          )}
        </div>

        <Flex className="nav">{this.renderNavs()}</Flex>
      </div>
    );
  }
}
css 复制代码
// ... existing code ...

.swiper-container {
  height: 212px;
  position: relative;
}

.loading-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.carousel-fade {
  animation: fadeIn 0.5s ease-in-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

// ... existing code ...
相关推荐
自进化Agent智能体2 分钟前
MCP与Hooks:让AI Agent安全连接一切的治理框架
前端
明天一点4 分钟前
Cloudflare 通知转发钉钉机器人
前端·后端
前端Hardy4 分钟前
前端日历组件,要变天了?Schedule-X v4.6 彻底杀疯了
前端·javascript·后端
微扬嘴角20 分钟前
React快速入门
前端·react.js·前端框架
喵了几个咪22 分钟前
AI重构软件开发范式:框架与脚手架为何仍是生产级开发的刚需?
vue.js·人工智能·react.js·重构·golang·ai编程
ImTryCatchException32 分钟前
React Native 嵌入现有 Android 项目:踩坑记录与解决方案
android·react native·react.js
ZC跨境爬虫37 分钟前
跟着 MDN 学CSS day_40:(Flexbox实战技能测试)
前端·css·ui·html·tensorflow
ZC跨境爬虫42 分钟前
跟着 MDN 学CSS day_36:(float、clear与BFC深度解析)
前端·javascript·css·ui·交互
ConardLi1 小时前
啊?我刚开源的 Skills 已经 7K Star 了?!
前端·人工智能·后端
糯米团子7491 小时前
javascript高频知识点
开发语言·前端·javascript