微信小程序使用Swiper组件实现层叠轮播图

本篇代码照搬自网友,见链接。

搭建静态资源服务器

  1. 全局安装serve:npm install -g serve
  2. 在任何你想的地方新建文件夹:resources。resources下新建文件夹:images,用于存放静态图片资源;resources下新建文件夹:videos,用于存放视频文件;resources下新建文件夹:audios,用于存放音频文件。
  3. 启动服务器:serve resources

小程序项目

代码涉及的主要文件有:

  1. index.wxml
  2. index.wxss
  3. index.js

在这里插入图片描述

index.wxml

<swiper previous-margin='200rpx' next-margin='200rpx' bindchange="swiperChange" style='height:{{swiperH}}'  circular>
  <swiper-item wx:for='{{imgList}}' wx:key='{{index}}'>
    <image class='le-img {{nowIdx==index?"le-active":""}}' bindload='getHeight' src='{{item}}' style='height:{{swiperH}};'></image>
  </swiper-item>
</swiper>

index.wxss

.le-img {
  width: 100%;
  display: block;
  transform: scale(0.8);
  transition: all 0.3s ease;
  border-radius: 6px;
  box-shadow: 0 0 6rpx rgba(50, 50, 50, 0.4);
  -webkit-filter: blur(2rpx);
  -moz-filter: blur(2rpx);
  -ms-filter: blur(2rpx);
  -o-filter: blur(2rpx);
  filter: blur(2rpx);
}
.le-img.le-active {
  transform: scale(1);
  box-shadow: none;
  -webkit-filter: blur(0);
  -moz-filter: blur(0);
  -ms-filter: blur(0);
  -o-filter: blur(0);
  filter: blur(0);
}

index.js

Page({
  data: {
    swiperH:'',
    nowIdx:0,
    imgList:[
      'http://localhost:3000/images/j1.jpg',
      'http://localhost:3000/images/j2.jpg',
      'http://localhost:3000/images/j3.jpg',
      'http://localhost:3000/images/j4.jpg',
      'http://localhost:3000/images/j5.jpg',
      'http://localhost:3000/images/j3.jpg'
    ]
  },
  //获取swiper高度
  getHeight:function(e){
    var winWid = wx.getSystemInfoSync().windowWidth - 2*100;//获取当前屏幕的宽度
    var imgh = e.detail.height;//图片高度
    var imgw = e.detail.width;
    var sH = winWid * imgh / imgw + "px"
    this.setData({
      swiperH: sH
    })
  },
  //swiper滑动事件
  swiperChange:function(e){
    this.setData({
      nowIdx: e.detail.current
    })
  }
})

相关链接

微信小程序使用swiper组件实现层叠轮播图
轮播图的实现
微信小程序之实现层叠轮播图的效果案例

  • 1
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值