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 ...
相关推荐
马可家的菠萝15 分钟前
自动保存已经有了,为什么笔记软件还需要“历史版本”?
前端·后端·架构
半个落月30 分钟前
从 "use client" 到 Route Handler:用 Todos 理解 Next.js 水合与全栈请求
前端·react.js·next.js
qq_4523962334 分钟前
第七篇:《大型前端项目的模块化与目录结构设计》
前端
你脑门上的脚印42 分钟前
Vue 项目从零实现语音转文字、文字转语音功能(完整可用 + 踩坑指南)
前端·vue.js
paopaokaka_luck1 小时前
基于springboot3+vue3的车间生产管理系统(Echarts图形化分析、BI报表)
java·前端·spring boot·学习·echarts
程序员鱼皮1 小时前
3 大 DeepSeek Harness 进阶玩法,招多个大肥鱼帮我干活!
前端·后端·ai编程
KoPa1 小时前
HeySmart:大模型开源网关基座-请求生命周期与钩子引擎
前端·后端
七牛开发者2 小时前
Coding Agent 如何跑稳长任务?从上下文管理到运行时状态
前端·javascript·后端
半个落月2 小时前
从 CSR 到 Server Component:吃透 Next.js 16 App Router 路由、布局与 SEO
前端·next.js
七牛开发者2 小时前
拆解 DeepSeek Harness:Profile 与 Bundle 如何装配运行时
前端·javascript·后端