> 文章列表 > 左右icon切换中间图片逻辑

左右icon切换中间图片逻辑

左右icon切换中间图片逻辑

在这里插入图片描述

大致逻辑:

  1. 初始化时判断length,当图片只有一张时,隐藏左右箭头
  2. 当图片为最后一张,隐藏右箭头;
  3. *当图片为倒数第二张,点击右箭头时,索引加1并隐藏右箭头;
  4. 当图片为第一张,隐藏左箭头;
  5. *当图片为第二张,点击左箭头时,索引减1并隐藏左箭头;

中间图片url列表从接口数据中获取,点击左右icon触发逻辑如下:

// 点击左箭头
this.onclickLeft = e => {let list = this.$model('imageList') //获取图片地址列表 let nowUrl = this.$component('#image').$param('imageSrc')let index = list.findIndex((e) => { // 计算当前图片索引return e == nowUrl})// 当前图片为最后一个,隐藏右箭头if(index === list.length-1){this.$component('#image').$param('imageSrc', list[index-1])this.$component('#leftArrow').$visible(true)this.$component('#rightArrow').$visible(false)} else if(0 < index < length-1){ // 正常情况:索引减1,切换图片this.$component('#image').$param('imageSrc', list[index-1])}// 当图片为第二张,特殊处理,隐藏左箭头(不重复做索引加减)if(index === 1){this.$component('#leftArrow').$visible(false)this.$component('#rightArrow').$visible(true)}
}
// 点击右箭头
this.onclickRight = e => {let list = this.$model('imageList')let nowUrl = this.$component('#image').$param('imageSrc')let index = list.findIndex((e) => {// 计算当前图片索引return e == nowUrl})if(index === 0){this.$component('#image').$param('imageSrc', list[index+1])this.$component('#leftArrow').$visible(false)this.$component('#rightArrow').$visible(true)}else if(0 < index < length-1){this.$component('#image').$param('imageSrc', list[index+1])}// 当图片为倒数第二张,特殊处理,隐藏右箭头(不重复做索引加减)if(index === list.length-2){this.$component('#leftArrow').$visible(true)this.$component('#rightArrow').$visible(false)}
}