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>

效果图

在这里插入图片描述




相关推荐
2501_916007472 天前
iPhone查看App日志和系统崩溃日志的完整实用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
iOS 抓不到包怎么办?全流程排查思路与替代引导
android·ios·小程序·https·uni-app·iphone·webview
HebyH_2 天前
uniapp如何封装uni.request 全局使用
uni-app
随笔记3 天前
uniapp蓝牙连接设备并发送接收信息
javascript·uni-app·app
脑袋大大的3 天前
从“PPT动画”到“丝滑如德芙”——uni-app x 动画性能的“终极奥义”
前端·javascript·nginx·uni-app·uniapp·app开发·混合开发
随笔记3 天前
uniapp开发的app原生操作手机系统文件
前端·javascript·uni-app
于慨3 天前
uniapp上传文件
前端·uni-app
猫头_3 天前
uni-app 转微信小程序 · 避坑与实战全记录
前端·微信小程序·uni-app
iOS阿玮3 天前
凭一己之力干穿一个品牌,互联网+时代口碑比以前更重要了!
uni-app·app·apple
前端amanda3 天前
uniapp中uview组件中u-input格式化后赋值踩坑
前端·javascript·uni-app