<!-- 滚动图 -->
<view class="swiper" style="position:relative">
<swiper autoplay="{{true}}" circular="{{true}}" onChange="currentHandle">
<block a:for="{{swiperList}}">
<swiper-item class="swiper-box">
<view class="swiper-item" style="width:100%height:300rpx">
<!-- lazy-load根据需要 onTap可以点击图片跳转 data-url绑定到跳转的链接-->
<image lazy-load="{{true}}" mode="scaleToFill" src="{{item.image}}" style="display:flexwidth:100%height:300rpx"
onTap="swiper" data-url="{{item.url}}" data-index='{{index}}' />
</view>
</swiper-item>
</block>
</swiper>
<!-- 圆点 -->
<view class="swiper_dot">
<view class="trans MR10 {{current === index ?'active': ''}}" a:for="{{swiperList}}" a:key="{{index}}"></view>
</view>
</view>
```
```
data(){
swiperList:[
{
image:'',//图片的路径
url:""//要跳转的路径
},
{
image:'',
url:""
}
],
current: 0,//初始化dot
},
//监听current
currentHandle(e) {
console.log(e)
//改变current的值
let { current } = e.detail
this.setData({
current
})
},
```
```
.swiper-box {
padding: 0 30rpx
}
.swiper-item {
border-radius: 10rpx
overflow: hidden
}
.swiper_dot {
display: flex
flex: 1
justify-content: center
position: absolute
bottom: 16rpx
left: 42%//通过绝对定位 在滚动图的正下方 具体看自己
}
.MR10 {
margin-right: 10rpx
}
.trans {
width: 23rpx
height: 8rpx
background-color: #ffffff70
border-radius: 3.5rpx
transition: width 0.5s linear
}
.active {
background-color: #ffffffd7
width: 67rpx
transition: width 0.5s linear
}
```
---转自我的自个的
支付宝小程序Swiper 滚动图 带圆点和跳转方式_多甘范科夫斯基的博客-CSDN博客
一、需求:
swiper组件缩小后需要完全居中。
二、实现方案:
一种方式是flex居中,但我调试后效果不行;还有一种就是绝对定位,使用left 50%,这种我调试也不居中;还有一种就是计算屏幕宽度和swiper宽度,绝对定位后left=(屏幕width-swiper width)/2,本文介绍最后一种实现方式。
三、程序实现
3.1 js文件
data声明变量
onload时计算参数值
3.2 wxss文件
使用绝对定位,left使用变量,格式: var( --变量名--)
3.3 wxml文件
swiper外用一个view组件,并使用变量,格式:style="--left--:{{动态变量}}px"
四、实现效果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)