uniapp 仿小红书轮播图效果

通过对小红书的轮播图分析,可得出以下总结:

1.单张图片时容器根据图片像素定高

2.多图时轮播图容器高度以首图为锚点

3.比首图长则固高左右留白

4.比首图短则固宽上下留白

代码如下:

go 复制代码
<template>
  <view>
<!--轮播-->
   <swiper
      class="swiper" :style="{ 'height': swiperHeight+ 'px' }"
      circular
      :indicator-dots="indicatorDots"
      :autoplay="autoplay"
      :interval="interval"
      :duration="duration"
      indicator-active-color="#FFFFFF">
      <swiper-item v-for="(item, index) in imgList" :key="index" >
        <image class="swiper-image" :src="item" mode="aspectFit" @click="showImage(index)" :style="{ 'height': swiperHeight+ 'px' }"/>
      </swiper-item>
    </swiper>
  </view>
</template>


export default {
  data() {
    return {
     indicatorDots: true,
      autoplay: true,
      interval: 5000,
      duration: 300,
      imgList: ["https://mainoss.duoxiaiche.com/UNCATEGORIZED/1745480113402.jpg", "https://mainoss.duoxiaiche.com/UNCATEGORIZED/1745480117425.jpg", "https://mainoss.duoxiaiche.com/UNCATEGORIZED/1745480122350.jpg"],
      swiperHeight:400,//默认高度
    }
   },
     onLoad() {
     const firstImg = this.imgList[0] || ''
	  this.getImgHeight(firstImg).then((heightRes) => {
	    this.swiperHeight = heightRes
	  })
     },
 methods: {
	getImgHeight(imageUrl){
		  let containerHeight = 400
		  return new Promise((resolve, reject) => {
		    wx.getImageInfo({
		      src: imageUrl,
		      success: (res) => {
		        const { width, height } = res
		        uni.getSystemInfo({
		          success: (res) => {
		            const screenWidth = res.windowWidth;
		            const aspectRatio = width / height;
		            const imgHeight = screenWidth / aspectRatio;
					console.log('imgHeight---',imgHeight)
					containerHeight = imgHeight && imgHeight < 400 ? imgHeight : 400;
		            resolve(containerHeight)
		          },
		        })
		      },
		      fail: (err) => {
		        console.error('Failed to get image info', err)
		        reject(containerHeight)
		      },
		    })
		  })
		},
	// 点击显示图片
    showImage(index) {
      uni.previewImage({
        urls: this.imgList,
        current: index,
        indicator: 'number'
      });
    },
	}
 }
</script>

<style lang="scss" scoped>
.swiper {
  .swiper-image {
    width: 100%;
  }
}
</style>

效果图

在这里插入图片描述




相关推荐
米粒宝的爸爸19 小时前
uniapp在app端,在导航栏设置自定义按钮
uni-app
dssxyz20 小时前
uniapp打包微信小程序主包过大问题_uniapp 微信小程序时主包太大和vendor.js过大
javascript·微信小程序·uni-app
xw520 小时前
我犯了错,我于是为我的uni-app项目引入环境标志
前端·uni-app
!win !20 小时前
被老板怼后,我为uni-app项目引入环境标志
前端·小程序·uni-app
颜渊呐1 天前
uniapp中APPwebview与网页的双向通信
前端·uni-app
白杨木影子被拉长1 天前
多状态映射不同样式(scss语法)
vue.js·uni-app
一念杂记1 天前
免费开源!微信小程序商城源码,快速搭建你的线上商城系统!
微信小程序·uni-app
aklry1 天前
uniapp三步完成生成一维码图片
uni-app
雪芽蓝域zzs2 天前
uniapp 国密sm2加密
uni-app
打不着的大喇叭2 天前
uniapp的光标跟随和打字机效果
前端·javascript·uni-app