微信小程序swiper 前后边距的使用-----previous-margin和next-margin的使用

小程序里经常使用swiper组件来实现轮播滑动的效果。但是有时候我们需要露出前一张和后一张图片一部分,我们可以使用previous-margin和next-margin属性。
  previous-margin:前边距,可用于露出前一项的一小部分
  next-margin:后边距,可用于露出后一项的一小部分
假设设置 一个图片的尺寸为宽为666rpx,高为600rpx,前一张图片露出为20rpx,后一张图片露出为20rpx。这里使用的ip6的屏幕尺寸,宽为750rpx (rpx单位:可以根据屏幕宽度进行自适应)。
1、用屏幕宽度-图片的宽度,得到两边的边距和
750-666=84rpx
2、.将边距拆成两部分,得到一边的边距,用边距减去露出的尺寸,得到两张图片之间的边距
84/2-20=22rpx
3、图片间距 拆成两部分,分别为图片的外边距
22/2=11rpx
4、露出的尺寸 加上 外边距 就等于前后边距previous-margin和next-margin的值
11+20=31rpx
注意:swiper-item的宽度不能改成图片的大小在这里插入图片描述

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
是的,可以使用swiper和scroll-view组件来实现微信小程序的左右分页功能。 首先,在wxml文件中添加swiper和scroll-view组件,并设置相应的属性。 ```html <swiper class="swiper-container" current="{{current}}" duration="{{500}}" style="height:{{pageHeight}}px"> <swiper-item> <scroll-view class="scroll-view" scroll-x="{{true}}" scroll-y="{{false}}" style="width:{{windowWidth}}px;height:{{pageHeight}}px;"> <!--第一页内容--> </scroll-view> </swiper-item> <swiper-item> <scroll-view class="scroll-view" scroll-x="{{true}}" scroll-y="{{false}}" style="width:{{windowWidth}}px;height:{{pageHeight}}px;"> <!--第二页内容--> </scroll-view> </swiper-item> <!--更多swiper-item--> </swiper> ``` 其中,swiper组件设置了current属性,用于控制当显示的页码;scroll-view组件设置了scroll-x属性,用于开启水平方向的滚动,并设置了样式宽度为屏幕宽度,高度为页面高度。 接着,在js文件中,监听swiper组件的change事件,用于动态更新当页码。 ```javascript Page({ data: { current: 0, windowWidth: wx.getSystemInfoSync().windowWidth, pageHeight: wx.getSystemInfoSync().windowHeight }, onLoad: function () { //... }, onPageChange: function (e) { this.setData({ current: e.detail.current }) } }) ``` 最后,在样式文件中,设置swiper-container和scroll-view的样式,用于实现左右分页效果。 ```css .swiper-container { overflow: hidden; position: relative; } .scroll-view { white-space: nowrap; } .swiper-item { display: inline-block; vertical-align: top; width: 100%; } ``` 以上就是使用swiper和scroll-view组件实现微信小程序左右分页功能的方法。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值