当前位置: 首页 > news >正文

封装轮播图 (因为基于微博小程序,语法可能有些出入,如需使用需改标签)

这是在组件中使用,基于微博语法

<template><wbx-view class="" style="width: 100vw;height: 70vh;"><WBXswiper @change="gaibian" :vertical="false" :current="current" indicatorActiveColor="#fff" indicatorColor="#c0c0c0" :items="items"   style="width: 375px;height: 200px;border-radius: 20px;"><template slot="swiperItem" slot-scope="scope"><wbx-image :src="scope.item.src" mode="aspectFill" style="width:375px; height: 200px;" /></template></WBXswiper><!-- //测试点击切换轮播 --><wbx-view  style="margin-bottom: 30px;"><web-view style="width: 30px;height: 30px;border: 1px solid red;" @click="add(0)"><view-text>0</view-text></web-view><web-view style="width: 30px;height: 30px;border: 1px solid red;" @click="add(1)"><view-text>1</view-text></web-view><web-view style="width: 30px;height: 30px;border: 1px solid red;" @click="add(2)"><view-text>2</view-text></web-view></wbx-view></wbx-view>
</template><script>
/*** @type WBXAppOption*/
import WBXswiper from "../../commpents/WBXswiper/index.vue";
const pageOptions = {data() {return {items: [{ src: 'res/1.jpg',txt:222222},{ src: 'res/1.jpg',txt:222222},{ src: 'res/1.jpg',txt:222222},],current:0}},computed:{},methods: {gaibian(e){console.log(e,'change')},add(index){console.log(this.current)this.current=index}},components: {WBXswiper,},wbox: {onLoad() { },onShow() {// 页面显示/切入前台时触发},onHide() {// 页面隐藏时触发},onUnload() {// 页面退出时触发},},mounted() { },
};
export default pageOptions;
</script><style></style>

自己封装的swiper组件内部

<template><wbx-viewref="objStyle":style="wrapperStyle"@touchstart="onTouchStart"@touchmove="onTouchMove"@touchend="onTouchEnd"><wbx-viewclass="carousel-wrapper":style="carouselStyle"@transitionend="onTransitionEnd"ref="carouselWrapper"><wbx-view :style="itemStyle"><slot name="swiperItem" :item="items[items.length - 1]"></slot></wbx-view><wbx-view v-for="(item, index) in items" :key="index" :style="itemStyle"><slot name="swiperItem" :item="item"></slot></wbx-view><wbx-view :style="itemStyle"><slot name="swiperItem" :item="items[0]"></slot></wbx-view></wbx-view><wbx-view v-if="indicatorDots" :style="{ width: containerWidth + 'px' }" style="position: absolute; bottom: 10px; display: flex; flex-direction: row; justify-content: center;"><wbx-viewv-for="(item, index) in items":key="index":style="{ backgroundColor: index === realIndex ? indicatorActiveColor : indicatorColor }"style="width: 10px; height: 10px; margin: 0 5px; cursor: pointer; border-radius: 10px;"@click~stop="setCurrentIndex(index)"></wbx-view></wbx-view></wbx-view></template><script>export default {/*items                 数据autoPlay              是否自动播放interval              自动播放间隔时间indicatorDots         是否显示指示点indicatorColor        指示点颜色indicatorActiveColor  当前选中的指示点颜色current               当前所在滑块的indexvertical              滑动方向是否为纵向@change               轮播图改变时会触发 change 事件,返回当前索引值*/props: {items: {type: Array,required: true},autoPlay: {type: Boolean,default: false},interval: {type: Number,default: 3000},indicatorDots: {type: Boolean,default: true},indicatorColor: {type: String,default: '#c0c0c0'},indicatorActiveColor: {type: String,default: '#fff'},current: {type: String,default: ''},vertical: {type: Boolean,default: false}},data() {return {currentIndex: 1,timer: null,startX: 0,startY: 0,offset: 0,isTransitioning: false,containerWidth: 0,containerHeight: 0};},watch: {current(newVal) {this.setCurrentIndex(newVal);}},computed: {wrapperStyle() {return {backgroundColor: "rebeccapurple",position: "relative",width: `${this.wrapperWidth}px`,height: `${this.wrapperHeight}px`,};},carouselStyle() {const baseTranslateValue = -this.currentIndex * (this.vertical ? this.containerHeight : this.containerWidth);const translateValue = baseTranslateValue + this.offset;console.log(this.offset,baseTranslateValue,translateValue,"999999")return {display: 'flex',flexDirection: this.vertical ? 'column' : 'row',transform: this.vertical ? `translateY(${translateValue}px)` : `translateX(${translateValue}px)`,transition: this.isTransitioning ? 'transform 0.3s ease-out' : 'none',width: !this.vertical ? `${this.wrapperWidth}px` : `${this.containerWidth}px`,height: this.vertical ? `${this.wrapperHeight}px` : `${this.containerWidth}px`};},wrapperWidth() {return this.containerWidth * (this.items.length + 2);},wrapperHeight() {return this.containerHeight * (this.items.length + 2);},itemStyle() {return {width: !this.vertical ? `${this.containerWidth}px` : `${this.containerWidth}px`,height: this.vertical ? `${this.containerHeight}px` : `${this.containerWidth}px`,flexShrink: 0};},realIndex() {return (this.currentIndex - 1 + this.items.length) % this.items.length;}},mounted() {this.updateDimensions();this.$nextTick(() => {if (this.autoPlay) {this.startAutoPlay();}});},beforeDestroy() {this.stopAutoPlay();},methods: {updateDimensions() {if (this.$refs.objStyle) {const objStyle =  this.$refs.objStyle.styleObjectthis.containerWidth = parseFloat(objStyle.width);this.containerHeight = parseFloat(objStyle.height);}},startAutoPlay() {this.timer = setInterval(() => {this.next();}, this.interval);},stopAutoPlay() {if (this.timer) {clearInterval(this.timer);this.timer = null;}},next() {this.offset = 0;this.isTransitioning = true;this.currentIndex += 1;this.$emit('change', { current: this.currentIndex });},prev() {this.offset = 0;this.isTransitioning = true;this.currentIndex -= 1;this.$emit('change', { current: this.currentIndex });},setCurrentIndex(index) {this.stopAutoPlay();this.isTransitioning = true;this.currentIndex = index + 1;if (this.autoPlay) {this.startAutoPlay();}},onTouchStart(e) {this.startX = e.touches[0].clientX;this.startY = e.touches[0].clientY;this.offset = 0;this.stopAutoPlay();},onTouchMove(e) {const moveX = e.touches[0].clientX;const moveY = e.touches[0].clientY;this.offset = this.vertical ? moveY - this.startY : moveX - this.startX;},onTouchEnd() {this.isTransitioning = true;if (Math.abs(this.offset) > (this.vertical ? this.containerHeight : this.containerWidth) /6) {if (this.offset > 0) {this.prev();} else {this.next();}} else {this.offset = 0;}if (this.autoPlay) {this.startAutoPlay();}},onTransitionEnd() {this.isTransitioning = false;this.offset = 0;if (this.currentIndex === this.items.length + 1) {this.currentIndex = 1;}if (this.currentIndex === 0) {this.currentIndex = this.items.length;}}}};</script><style></style>

相关文章:

  • 软件自动化测试基础:python运算符精讲
  • Python中的八个TXT文件自动化处理脚本:提升工作效率的必备工具
  • 62.【C语言】浮点数的存储
  • unity_Occlusion_Culling遮挡剔除学习
  • 从两个 Excel 表格中提取相关信息,并根据学生的 学号 和 姓名 将第一个表格中的成绩数据填充到第二个表格中(附Python代码)
  • 什么是 JWT?它是如何工作的?
  • vue数组根据某些条件进行二次切割
  • 1.2.1 HuggingFists安装说明-Linux安装
  • 如何用Prometheus监控禁用了Actuator的SpringBoot?
  • 渗透测试--文件上传常用绕过方式
  • Python编码系列—Python责任链模式:打造灵活的请求处理流程
  • 鸿蒙HarmonyOS之封装Http请求工具类
  • Llama微调以及Ollama部署
  • MRC接收机
  • 【数字图像处理】小白也能懂,最浅显方式手撕直方图均衡化(附python实现)
  • cookie和session
  • CSS实用技巧
  •  D - 粉碎叛乱F - 其他起义
  • ES6之路之模块详解
  • iOS编译提示和导航提示
  • Java的Interrupt与线程中断
  • Java多线程(4):使用线程池执行定时任务
  • Js基础——数据类型之Null和Undefined
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • underscore源码剖析之整体架构
  • Vim 折腾记
  • 给新手的新浪微博 SDK 集成教程【一】
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 那些年我们用过的显示性能指标
  • 盘点那些不知名却常用的 Git 操作
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 我从编程教室毕业
  • ​​快速排序(四)——挖坑法,前后指针法与非递归
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • # 数据结构
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • #if 1...#endif
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (4)STL算法之比较
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (含答案)C++笔试题你可以答对多少?
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)编辑寄语:因为爱心,所以美丽
  • (转)拼包函数及网络封包的异常处理(含代码)
  • .gitignore文件---让git自动忽略指定文件
  • .Net 6.0 Windows平台如何判断当前电脑是否联网
  • .NET MAUI Sqlite数据库操作(二)异步初始化方法
  • .NET Standard 支持的 .NET Framework 和 .NET Core
  • .NET 使用配置文件
  • .NET/C# 的字符串暂存池
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • .NET简谈设计模式之(单件模式)
  • .sys文件乱码_python vscode输出乱码
  • ?php echo ?,?php echo Hello world!;?
  • [ CTF ]【天格】战队WriteUp- 2022年第三届“网鼎杯”网络安全大赛(青龙组)