swiper轮播图息屏一段时间或快速滑动切换时出现疯狂抖动

以前做小程序项目的时候,没专门测试人员,都是开发者自测,可能我的手机性能比较不错(哈哈)或时机不对,总之没发掘到这个bug;近期做项目,测试了不同的手机,发现在一些手机上会出现这个bug,尤其是对于性能差的一些手机,出现的概率尤其的大。由于本次项目是基于uni-app的,直接就用uni-app进行操作说明;

说明:uni-app编写小程序:采用的是微信小程序的API,Vue的语法,所以,他们很相似,只是在写法上有一点稍微的区别,小程序的那套东西也是适用于uni-app的。

在微信小程序的官方文档中,有这样个提示, 如果在 bindchange 的事件回调函数中使用 setData 改变 current 值,则有可能导致 setData 被不停地调用。
最终产生的后果是如果快速滑动或者息屏一段时间打开后,就出现轮播图的疯狂抖动。

一、解决方法是:
在动画播放结束后(@animationfinish)改变current的值,而不是一滑动(@change)就改变current的值。即用@animationfinish代替 @change。

二、@change与@animationfinish对比说明
1、相同点:都会改变current的值,current代表当前所在滑块的index
2、不同点:改变current的时机不同,@change滑动时立即改变,@animationfinish动画结束后改变!

三、说明:

如果想要自定义面板指示点,不建议与swiper共用一个索引值,最好将swiper与指示点的索引区分开,即用不同的变量,然后两者同步变化就可以了!

下面直接来展示一个自定义指示面板,又可以解决抖动问题的案例!

1、逻辑梳理

  1>先解决抖动问题,用@animationfinish改变当前滑块的值;

<template>
<view class="swipers-wrap">
<swiper @animationfinish="swiperChange" :current="currentSwiper" circular="true" class="swipers-view"
:autoplay="autoplay" @change="dotChange">
<block v-for="(item,index) in swiperList" :key="index">
<swiper-item>
<view class="swipers-best-hot-video">
美景展示~
</view>
<view class="swipers-image-view">
<image :src="item.imgUrl" mode="aspectFill" class="swipers-image"></image>
<view class="swipers-title">
{{item.title}}
</view>
</view>
</swiper-item>
</block>
</swiper> <!--重置指示面板(小圆点)的样式 -->
<view class="swipers-dots">
<block v-for="(item,index) in swiperList" :key="index">
<view class="dot" :class="{active: currentDot == index}"></view>
</block>
</view>
</view>
</template>

  2>改变指示面板的索引,用@change;

<template>
<view class="swipers-wrap">
<swiper @animationfinish="swiperChange" :current="currentSwiper" circular="true" class="swipers-view"
:autoplay="autoplay" @change="dotChange">
<block v-for="(item,index) in swiperList" :key="index">
<swiper-item>
<view class="swipers-best-hot-video">
美景展示~
</view>
<view class="swipers-image-view">
<image :src="item.imgUrl" mode="aspectFill" class="swipers-image"></image>
<view class="swipers-title">
{{item.title}}
</view>
</view>
</swiper-item>
</block>
</swiper> <!--重置指示面板(小圆点)的样式 -->
<view class="swipers-dots">
<block v-for="(item,index) in swiperList" :key="index">
<view class="dot" :class="{active: currentDot == index}"></view>
</block>
</view>
</view>
</template>

  3>同步:对两者赋值e.detail.current;

dotChange: function(e) {
  this.currentDot = e.detail.current
},
swiperChange: function(e) {
  this.currentSwiper = e.detail.current
},

2、完整代码展示

<template>
<view class="swipers-wrap">
<swiper @animationfinish="swiperChange" :current="currentSwiper" circular="true" class="swipers-view"
:autoplay="autoplay" @change="dotChange">
<block v-for="(item,index) in swiperList" :key="index">
<swiper-item>
<view class="swipers-best-hot-video">
美景展示~
</view>
<view class="swipers-image-view">
<image :src="item.imgUrl" mode="aspectFill" class="swipers-image"></image>
<view class="swipers-title">
{{item.title}}
</view>
</view>
</swiper-item>
</block>
</swiper> <!--重置指示面板(小圆点)的样式 -->
<view class="swipers-dots">
<block v-for="(item,index) in swiperList" :key="index">
<view class="dot" :class="{active: currentDot == index}"></view>
</block>
</view>
</view>
</template> <script>
export default {
data() {
return {
currentDot: 0, //指示面板对应的索引
currentSwiper: 0, //用来记录当前swiper对应的索引
autoplay: true,
swiperList:[
{
'imgUrl':'../../static/swiper.jpg',
'title':'白云朵朵!'
},
{
'imgUrl':'../../static/list.jpg',
'title':'蓝天绿树!'
},
{
'imgUrl':'../../static/user.jpg',
'title':'boy!'
}
]
}
}, methods: {
dotChange: function(e) {
this.currentDot = e.detail.current
},
swiperChange: function(e) {
this.currentSwiper = e.detail.current
},
}
}
</script> <style>
.swipers-wrap {
height: auto;
position: relative;
background-color: blue;
padding: 0 15upx;
padding-bottom: 54upx; } .swipers-view {
height: 420upx;
} .swipers-best-hot-video {
height: 90upx;
line-height: 90upx;
color: #f8f3bf;
font-size: 36upx;
text-align: center;
} .swipers-image-view {
height: 100%;
position: relative;
z-index: 1;
} .swipers-image {
width: 100%;
height: 100%;
} .swipers-image-view .swipers-title {
position: absolute;
z-index: 99;
left: 25upx;
bottom: 105upx;
font-size: 24upx;
font-weight: bold; color: #fff;
font-size: 36upx;
} /*用来包裹所有的小圆点 */
.swipers-dots {
width: 100%;
height: 50upx;
display: flex;
flex-direction: row;
position: absolute;
left: 0;
bottom: 0upx;
/* border:1upx solid red; */
display: flex;
justify-content: center;
align-items: center;
/* border:1rpx solid green; */
} /*未选中时的小圆点样式 */
.dot {
width: 6px;
height: 2upx;
width: 55upx;
background-color: #999;
margin: 0 10upx;
/* border:1upx solid red; */
} /*选中以后的小圆点样式 */
.dot.active {
height: 2upx;
width: 80upx;
background-color: #fff;
margin: 0 10upx;
}
</style>

swiper轮播图出现疯狂抖动(小程序)的更多相关文章

  1. 微信小程序_(组件)swiper轮播图

    微信小程序swiper轮播图组件官方文档 传送门 Learn: swiper组件 一.swiper组件 indicator-dots:是否显示面板指示点[默认值false] autoplay:是否自动 ...

  2. Swiper轮播图

    今天咱们来说一下.Swiper轮播图. 超级简单的: 翠花,上代码:   <!DOCTYPE html>   <html lang="en">   < ...

  3. 【Vue项目】尚品汇(三)Home模块+Floor模块+Swiper轮播图

    写在前面 今天是7.23,这一篇内容主要完成了Home模块和部分Search模块的开发,主要是使用了swiper轮播图插件获取vuex仓库数据展示组件以及其他信息. 1 Search模块 1.1 Se ...

  4. 微信小程序之swiper轮播图中的图片自适应高度

    小程序中的轮播图很简单,官方都有例子的,但是唯一的缺陷就是swiper是固定死的150px高度,这样如果传入的图片大于这个高度就会被隐藏.辣么,怎样让图片自适应不同分辨率捏. 我的思路是:获取屏幕宽度 ...

  5. 如何自定义微信小程序swiper轮播图面板指示点的样式

    https://www.cnblogs.com/myboogle/p/6278163.html 微信小程序的swiper组件是滑块视图容器,也就是说平常我们看到的轮播图就可以用它来做,不过这个组件有很 ...

  6. 自定义微信小程序swiper轮播图面板指示点的样式

    微信小程序的swiper组件是滑块视图容器,也就是说平常我们看到的轮播图就可以用它来做,不过这个组件有很多样式是固定的,但是,有时候我们的设计稿的面板指示点是需要个性化的,那么如何去修改swiper组 ...

  7. swiper轮播图--兼容IE8

    //引入idangerous.swiper.min.js var mySwiper = new Swiper ('.swiper-container', { loop: true, paginatio ...

  8. swiper 轮播图,拖动之后继续轮播

    在此贴出swiper官网地址:https://www.swiper.com.cn/api/index.html 示例如下(官网示例): <script> var mySwiper = ne ...

  9. swiper轮播图(逆向自动切换类似于无限循环)

    swiper插件轮播图,默认的轮播循序是会从右向左,第一张,第二张,第三张,然后肉眼可见是的从第三张从左到右倒回第一张,这样就会有些视觉体验不高, ,不过还是能够用swiper本身的特性更改成无限循环 ...

  10. swiper轮播图插件

    一.简介 ①Swiper是纯javascript打造的滑动特效插件,面向手机.平板电脑等移动终端.Swiper能实现触屏焦点图.触屏Tab切换.触屏多图切换等常用效果. ②Swiper 是一款免费以及 ...

随机推荐

  1. 创建本地yum仓库

    创建本地yum仓库 1,将镜像挂载到/mnt 如果失败打开虚拟机把设备状态的两个选项打勾 2,切换到客户端的指定目录 3,创建文件夹bak存放网络yum创库配置文件 4,将网络源移动到bak减少干扰 ...

  2. 清理MySQL的binlog日志

    前言 MySQL的binlog是以二进制形式打印的日志,没设置自动删除的话,时间长了就会占用大量存储空间.删除MySQL的binlog日志有两种方法:自动删除和手动删除. 自动删除 永久生效:修改My ...

  3. 从redis未授权访问到获取服务器权限

    从redis未授权访问到获取服务器权限 好久没写博客了,博客园快荒芜了.赶紧再写一篇,算是一个关于自己学习的简要的记录把. 这里是关于redis未授权访问漏洞的一篇漏洞利用: 首先是redis,靶场搭 ...

  4. CSS基础(4)

    目录 1 定位 1.1 为什么需要定位 1.2 定位组成 1.2.1 边偏移(方位名词) 1.2.2 定位模式 (position) 1.3 定位模式介绍 1.3.1 静态定位(static) - 了 ...

  5. go-zero 是如何实现令牌桶限流的?

    原文链接: 上一篇文章介绍了 如何实现计数器限流?主要有两种实现方式,分别是固定窗口和滑动窗口,并且分析了 go-zero 采用固定窗口方式实现的源码. 但是采用固定窗口实现的限流器会有两个问题: 会 ...

  6. 网络请求-Android篇(Okhttp和Retrofit)

    一.OkHttp的介绍和基本用法 OkHttp是一个流行的开源Java和Android应用程序的HTTP客户端.它由Square Inc.开发,提供了一种简单高效的方式来进行应用程序中的HTTP请求. ...

  7. Linux 内核音频数据传递主要流程 (下)

    来而不往非礼也.前面看到了用户空间应用程序和 DMA buffer 之间交换数据,并更新 runtime->control->appl_ptr 指针的过程,这里看一下硬件设备驱动程序在完成 ...

  8. Java爬虫实战系列——常用的Java网络爬虫库

    常用的Java网络爬虫库 Java 开发语言是业界使用最广泛的开发语言之一,在互联网从业者中具有广泛的使用者,Java 网络爬虫可以帮助 Java 开发人员以快速.简单但广泛的方式为各种目的抓取数据. ...

  9. 浅谈API安全的应用

    ​ 理论基础 API它的全称是Application Programming Interface,也叫做应用程序接口,它定义了软件之间的数据交互方式.功能类型.随着互联网的普及和发展,API 从早期的 ...

  10. 一篇关于获得拼多多商品详情 API的使用说明

    拼多多(Pinduoduo)是中国一家快速发展的电商平台,为了帮助开发者更好地接入拼多多,平台提供了丰富的 API 接口供开发者使用,其中包括获取拼多多商品详情的 API.接下来,我们将介绍如何使用拼 ...