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>

效果图

在这里插入图片描述




相关推荐
游戏开发爱好者88 小时前
HTTPS 内容抓取实战 能抓到什么、怎么抓、不可解密时如何定位(面向开发与 iOS 真机排查)
android·网络协议·ios·小程序·https·uni-app·iphone
shykevin16 小时前
uni-app x导航区域跳转
windows·uni-app
2501_9151063217 小时前
iOS 混淆与 IPA 加固全流程,多工具组合实现无源码混淆、源码防护与可审计流水线(iOS 混淆|IPA 加固|无源码加固|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者817 小时前
用多工具组合把 iOS 混淆做成可复用的工程能力(iOS混淆 IPA加固 无源码混淆 Ipa Guard)
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214319 小时前
掌握 iOS 26 App 性能监控,从监测到优化的多工具组合流程
android·macos·ios·小程序·uni-app·cocoa·iphone
勉灬之19 小时前
通过npm run XXX命令生成uniapp的pages.json文件
npm·uni-app·json
知识分享小能手19 小时前
uni-app 入门学习教程,从入门到精通, uni-app常用API的详细语法知识点(上)(5)
前端·javascript·vue.js·学习·微信小程序·小程序·uni-app
2501_9160088920 小时前
手机 iOS 系统全解析,生态优势、开发机制与跨平台应用上架实践指南
android·ios·智能手机·小程序·uni-app·iphone·webview
00后程序员张1 天前
Fiddler使用教程,全面掌握Fiddler抓包工具的配置方法、代理设置与调试技巧(HTTPHTTPS全解析)
前端·测试工具·ios·小程序·fiddler·uni-app·webview
2501_916008891 天前
HTTPS 下的 DDoS 防护与抓包分析实战,从检测到快速缓解的工程化打法
网络协议·ios·小程序·https·uni-app·iphone·ddos