IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> 2022-10-27 工作记录--Swiper/React-解决swiper处理动态数据出现样式紊乱、抖动问题 -> 正文阅读

[JavaScript知识库]2022-10-27 工作记录--Swiper/React-解决swiper处理动态数据出现样式紊乱、抖动问题

Swiper/React-解决swiper处理动态数据出现样式紊乱、抖动问题

First of all,检查自己在push数据前是否先清空了数组:

  • 已清空,请直接往下看👁👁哦;
  • 未清空,请先清空数组(可查看我的另外一篇文章),再继续往下看👁👁哦~

一、开门见山 ??

图1:【正确样式】?

在这里插入图片描述

图2:【紊乱样式】?

在这里插入图片描述

  • 🎀 需求 —— 弹幕式轮播(如上 图1):
    • 1、分为上下两部分,且交错展示;
    • 2、轮播同时开始、速度匀速,且无限循环,即 跑马灯效果🧃;
    • 3、后端每次返的数据是随机的;
  • 🎀 实现方法 —— swiper
    • 1、上下两部分 -> 采用两个swiper,交错展示 -> 给下swiperswiper-container加上padding-left值:
    • 2、轮播同时开始、速度匀速,且无限循环 -> 跑马灯效果🧃,可以查看我的另外一篇文章喔:🌹
      • swiper-实现跑马灯效果swiper6的实现方式与其相同,只是写法不同,具体用法,请往下看哦??,我会以swiper6写个案例);
    • 3、由于是动态数据,出现bug描述如下:💥
      • 当进入弹窗/其他页面后,再次返回页面时,这两个swiper会先抖动一下,然后出现样式紊乱问题(如上 图2):
        • 问题整体描述如下动图:👇🏻👇🏻👇🏻

请添加图片描述

二、亡羊补牢 ??

Taking into account what we have discussed above, we may safely arrive at a conclusion that
依据我们在上文中讨论的内容,我们可以得出一个结论:

  • 解决问题的关键就是解决数据更新问题。

So what do we do?

在组件卸载及销毁之前 清空掉两个swiper里的数据。

三、柳暗花明又一村 ??

哒哒哒~🌈 最终没有bug的效果如下动图啦:

请添加图片描述

四、小试牛刀 ??

react-swiper6

1、安装swiper6

npm i swiper@6

或者

yarn add swiper@6

2、部分代码

请添加图片描述

home.jsx

import React from 'react';
import store from '@src/store'; // 引入接口相关数据
import { Swiper, SwiperSlide } from "swiper/react"; // 引入swiper,实现轮播
import 'style-loader!css-loader!swiper/swiper-bundle.css'; // 引入swiper的css

import './home.less';

class Home extends React.Component {

  async componentDidMount() {
    await store.getHomeData(); // 调用接口-首页
  }
  
  // 在组件卸载及销毁之前 清空掉两个`swiper`里的数据。
  componentWillUnmount() {
    store.setCarouselInfo1([]);
    store.setCarouselInfo2([]);
  }

  render() {
    // carouselInfo1: 上swiper数据, carouselInfo2: 下swiper数据
    const { carouselInfo1, carouselInfo2 } = store;

    return (
      <div className="home">
        {/* 祝福弹幕【swiper-no-swiping: 禁止手动滑动】*/}
        <div className="blessing swiper-no-swiping">
        
          {/* 上swiper */}
          {
            carouselInfo1?.length > 0 &&
            <Swiper
              className="blessingItems blessingTop"
              slidesPerView={1.25}
              speed={3000}
              freeMode={true}
              loop={true}
              autoplay={{
                delay: 0,
              }}
            >
              {
                carouselInfo1?.map((item, index) => {
                  return (
                    <SwiperSlide className="item" key={index}>
                      {/* 用户祝福背景 */}
                      <span className="item_bg i-blessing_bg"></span>
                      {/* 用户祝福文案 */}
                      <div className="item_texts">
                        {/* 用户名称 */}
                        <span className="user_name text-hidden-ellipsis">工行粉丝</span>
                        {/* 祝福语 */}
                        <div className="content text-hidden-ellipsis">送出祝福:{item.prizeName}</div>
                      </div>
                    </SwiperSlide>
                  )
                })
              }
            </Swiper>
          }
          
          {/* 下swiper */}
          {
            carouselInfo2?.length > 0 &&
            <Swiper
              className="blessingItems blessingBottom"
              slidesPerView={0.85}
              speed={3000}
              freeMode={true}
              loop={true}
              autoplay={{
                delay: 0,
              }}
            >
              {
                carouselInfo2?.map((item, index) => {
                  return (
                    <SwiperSlide className="item" key={index}>
                      {/* 用户祝福背景 */}
                      <span className="item_bg i-blessing_bg"></span>
                      {/* 用户祝福文案 */}
                      <div className="item_texts">
                        {/* 用户名称 */}
                        <span className="user_name text-hidden-ellipsis">工行粉丝</span>
                        {/* 祝福语 */}
                        <div className="content text-hidden-ellipsis">送出祝福:{item.prizeName}</div>
                      </div>
                    </SwiperSlide>
                  )
                })
              }
            </Swiper>
          }
          
        </div>
      </div>
    );
  }
}

export default Home;

home.less

/** 实现匀速轮播 */
.swiper-container>.swiper-wrapper {
    -webkit-transition-timing-function: linear;    /*之前是ease-out*/
      -moz-transition-timing-function: linear;
      -ms-transition-timing-function: linear;
      -o-transition-timing-function: linear;
      transition-timing-function: linear;
      margin: 0 auto;
}

store/index.js

import { makeAutoObservable } from 'mobx';
import API from '../api/index';

const store = makeAutoObservable({
  /** 首页 */
  // 上假轮播配置
  carouselInfo1: [],
  setCarouselInfo1(data) {
    this.carouselInfo1 = data;
  },
  // 下假轮播配置
  carouselInfo2: [],
  setCarouselInfo2(data) {
    this.carouselInfo2 = data;
  },
  
  async getHomeData() {
    const { data, success } = await API.getHomeData(); // 调用接口-首页
    if (data && success) {
      	// 处理假轮播数据,分为上下两部分
        this.setCarouselInfo1([]); //【重要】先清空
        this.setCarouselInfo2([]); //【重要】先清空
      	const { carouselInfo } = data; // 假轮播配置
        carouselInfo?.length > 0 &&
          carouselInfo?.forEach((item, index) => {
            if (index % 2 == 0) {
              // 上假轮播展示偶数项
              this.carouselInfo1.push(item);
            } else {
               // 下假轮播展示奇数项
               this.carouselInfo2.push(item);
            }
          })
      }
    }
  },

})
export default store 

请添加图片描述

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2022-10-31 11:46:33  更:2022-10-31 11:51:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 15:34:23-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码