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>

效果图

在这里插入图片描述




相关推荐
一条可有可无的咸鱼9 小时前
企业招聘信息,企业资讯进行公示
java·vue.js·spring boot·uni-app
游戏开发爱好者819 小时前
H5 混合应用加密 Web 资源暴露到 IPA 层防护的完整技术方案
android·前端·ios·小程序·uni-app·iphone·webview
2501_9151063220 小时前
最新版本iOS系统设备管理功能全面指南
android·macos·ios·小程序·uni-app·cocoa·iphone
游戏开发爱好者820 小时前
HTTPS DDoS 排查 异常流量到抓包分析
网络协议·ios·小程序·https·uni-app·iphone·ddos
一点晖光20 小时前
小程序中web-view加载uni-app H5如何使用postMessage方法的解决方案
前端·小程序·uni-app
2501_9159184121 小时前
iOS 性能监控 运行时指标与系统行为的多工具协同方案
android·macos·ios·小程序·uni-app·cocoa·iphone
qq_4244091921 小时前
uniapp,通过webview内嵌h5页面,如何修改h5的大小
uni-app
00后程序员张21 小时前
IPA 混淆技术全解,从成品包结构出发的 iOS 应用安全实践与工具组合
android·安全·ios·小程序·uni-app·cocoa·iphone
郑州光合科技余经理1 天前
定制开发实战:海外版外卖系统PHP全栈解决方案
java·服务器·开发语言·javascript·git·uni-app·php
2501_916008891 天前
IOScer 证书到底是什么和怎么使用的完整说明
android·ios·小程序·https·uni-app·iphone·webview