微信小程序轮播图自适应实现

2025-05-10 05:03:18

1、打开小程序开发工具,在项目pages下新建mypage文件夹,在文件夹内新建page,名为mypage,并在app.json中将mypage设为第一页面

微信小程序轮播图自适应实现

3、在mypage.js添加代码,给图片列表赋值,注意要拷贝三张图到mypage文件夹Page({data: {imgUrls: ['s1.jpg','s2.jpg','s3.jpg'],},})

微信小程序轮播图自适应实现

5、修改mypage.wxml代码如下,动态设置swiper组件的高度:<swiper class = "sw" style = "height:{{imgheights[current]}}px" autoplay="true" interval="5000" duration="1000" circular="true" bindchange='bindchange'><block wx:for="{{imgUrls}}" wx:key="{{index}}"><swiper-item><image src="{{item}}" data-id ="{{index}}"mode="widthFix" bindload="imgload"/></swiper-item></block></swiper>mypage.wxss代码如下:.sw{width: 100%;}image{width: 100%;}

微信小程序轮播图自适应实现

7、编译代码,更改模拟器大尺寸,依然可以自适应了,不过原来这个swiper的设计宽高固定的确实很麻烦

微信小程序轮播图自适应实现
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢