微信小程序:轮播图

这篇文章介绍了如何使用Me.js库在移动端实现一个竖向滚动的swiper组件,展示了如何配置autoplay、vertical属性,并通过<swiper-item>展示自定义通知,包括图片和文字内容。

最外层用<swiper> </swiper>

        swiper组件里的一些常用的属性

                autoplay:自动播放

                vertical:竖着滚动

                circular:循环跳转,就是播到最后一个之后,会自动播放第一个。

                indicator-dots:是否显示小圆点

里层用不同的<swiper-item></swiper-item>

        swiper-item里面也可以嵌套一些view,image之类的。

竖着轮播的效果

me.js

// pages/me/me.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arrs:[
      {"content":"你好,我是通知1","pic":"../images/棒球.png"},
      {"content":"你好,我是通知2","pic":"../images/保龄球.png"},
      {"content":"你好,我是通知3","pic":"../images/橄榄球.png"},
      {"content":"你好,我是通知4","pic":"../images/击剑.png"},
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

me.json

{
  "usingComponents": {}
}

me.wxml

<view class="middle">
    <swiper duration="3000" interval="3000" autoplay="true" class="info" vertical="true">
      <block wx:for="{{arrs}}">
        <swiper-item class="box">
          <view class="img"><image src="{{item.pic}}"></image></view>
          <view class="tip">{{item.content}}</view>
        </swiper-item>
      </block>
    </swiper>
  </view>

me.wxss

/*消息提示框*/
.middle{
  width: 100%;
  height: 10vh;
}
.info{
  width: 100%;
  height: 100%;
}
.box{
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  /*background-color: steelblue;*/
}
.img{
  width: 20%;
  height: 90%;
  /*border: 1px solid rebeccapurple;*/
  margin-left: 10px;
}
.img>image{
  width: 100%;
  height: 100%;
}
.tip{
  margin-left: 10px;
  width: 80%;
  height: 70%;
  font-size: 20px;
  font-weight: bold;
  /*border: 1px solid snow;*/
  display: flex;
  align-items: center;
}

 

要实现微信小程序轮播图中间大、两边小的效果,可以借助 `swiper` 组件的自定义能力,并通过 CSS 样式和 JavaScript 控制器配合实现视觉上的放大与过渡效果。以下是详细的实现方法: ### 1. WXML 结构 在 WXML 文件中,使用 `swiper` 和 `swiper-item` 来构建轮播图的基本结构。为了实现中间项放大的效果,需要为当前激活项添加特定的类名。 ```xml <view class="container"> <swiper class="swiper-wrapper" indicator-dots="{{false}}" autoplay="{{false}}" bindchange="onSwiperChange" > <block wx:for="{{imageList}}" wx:key="index"> <swiper-item class="swiper-item {{currentIndex === index ? 'swiper-item-active' : ''}}" > <image src="{{item}}" mode="aspectFill" class="swiper-image" /> </swiper-item> </block> </swiper> </view> ``` ### 2. WXSS 样式 在 WXSS 文件中,通过设置基础样式和激活状态下的样式来实现视觉差。关键在于为非激活项设置较小的尺寸,并利用 `transition` 实现平滑动画效果。 ```css .container { display: flex; justify-content: center; } .swiper-wrapper { width: 750rpx; height: 900rpx; overflow: visible; } .swiper-item { display: flex; align-items: center; justify-content: center; transition: all 0.6s ease-in-out; } .swiper-item image { width: 448rpx; height: 720rpx; border-radius: 20rpx; transition: all 0.6s ease-in-out; } .swiper-item-active image { width: 560rpx; height: 900rpx; z-index: 2; } ``` ### 3. JavaScript 控制器 在 JS 文件中,需要监听 `bindchange` 事件以获取当前轮播图的索引值,并更新数据驱动视图变化。 ```javascript Page({ data: { imageList: [ 'https://example.com/image1.jpg', 'https://example.com/image2.jpg', 'https://example.com/image3.jpg' ], currentIndex: 0 }, onSwiperChange(e) { const { current } = e.detail; this.setData({ currentIndex: current }); } }); ``` ### 4. 进阶优化 如果希望进一步提升用户体验,可以通过以下方式增强交互效果: - **缩略图预览**:在底部或侧边展示缩略图,用户点击后可跳转到对应图片。 - **阴影效果**:为非激活项添加轻微阴影,使其层次感更强。 - **动态计算宽度**:根据屏幕大小动态调整图片宽度和高度,适配不同设备[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值