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 ...
相关推荐
踩着两条虫35 分钟前
如何评价VTJ.PRO?
前端·架构·ai编程
Mh2 小时前
鼠标跟随倾斜动效
前端·css·vue.js
小码哥_常2 小时前
Kotlin类型魔法:Any、Unit、Nothing 深度探秘
前端
Web极客码4 小时前
深入了解WordPress网站访客意图
服务器·前端·wordpress
幺风4 小时前
Claude Code 源码分析 — Tool/MCP/Skill 可扩展工具系统
前端·javascript·ai编程
vjmap4 小时前
唯杰地图CAD图层加高性能特效扩展包发布
前端·gis
ZC跨境爬虫4 小时前
3D 地球卫星轨道可视化平台开发 Day7(AI异步加速+卫星系列精简+AI Agent自动评论)
前端·人工智能·3d·html·json
ID_180079054734 小时前
淘宝 API 上货 / 商品搬家 业务场景实现 + JSON 返回示例
前端·javascript·json
M ? A5 小时前
Vue 动态组件在 React 中,VuReact 会如何实现?
前端·javascript·vue.js·经验分享·react.js·面试·vureact
vipbic5 小时前
独立开发复盘:我用 Uni-app + Strapi v5 肝了一个“会上瘾”的打卡小程序
前端·微信小程序