当前位置: 首页 > news >正文

uniapp 仿小红书轮播图效果

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

1.单张图片时容器根据图片像素定高
2.多图时轮播图容器高度以首图为锚点
3.比首图长则固高左右留白
4.比首图短则固宽上下留白

代码如下:

<template><view>
<!--轮播--><swiperclass="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 = 400return new Promise((resolve, reject) => {wx.getImageInfo({src: imageUrl,success: (res) => {const { width, height } = resuni.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>

效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

相关文章:

  • 深度解析 TransmittableThreadLocal(TTL):原理、实战与优化指南
  • Node.js 学习入门指南
  • Linux 内核 IPv4 套接字创建机制与协议表管理深度解析
  • 全链路数据仓建设指南:从构建流程到应用场景
  • 银河麒麟系统安装vscode
  • 2023 国考
  • JAVA中包装类型的数值比较问题
  • SPH Engineering - 无人机技术开发专家
  • shell脚本2
  • k8s基于角色的访问控制(RBAC)
  • 使用ffmpeg 将图片合成为视频,填充模糊背景,并添加两段音乐
  • SiamFC算法深度解析
  • 解决微信开发者工具报错 “Component is not found in path wx://not-found“ 代码修改后热更新报错
  • 【无人机】无人机遥控器设置与校准,飞行模式的选择,无线电控制 (RC) 设置
  • 被封号如何申诉?Google Play开发者账号申诉模版分享
  • ERROR: x264 not found using pkg-config
  • CS144 Lab 6 实战记录:构建 IP 路由器
  • Android学习总结之扩展基础篇(一)
  • AI数字人多模态技术如何提升用户体验?
  • c++——内部类
  • 人大法工委:涉核领域还需要有一部统领性的基础法律
  • 公安部知识产权犯罪侦查局:侦破盗录传播春节档院线电影刑案25起
  • 李良生已任应急管理部党委委员、政治部主任
  • 何立峰出席跨境贸易便利化专项行动部署会并讲话
  • 下周起上海浦东将投放5000万元消费券,预计分五周发放
  • 研讨会丨明清区域史研究的比较与对话