微信小程序之Swiper组件

微信小程序实现引导页Swiper详细教程
本文通过实例代码介绍了微信小程序中如何使用Swiper组件创建引导页,包括设置图片轮播、指示点和自动切换等功能。点击立即体验按钮会显示提示信息。

一、概述

滑块视图容器。其中只可放置swiper-item组件,否则会导致未定义的行为。

官方文档:https://developers.weixin.qq.com/miniprogram/dev/component/swiper.html

二、Swiper实现引导页

在移动开发中,我们经常使用ViewPager(Android)和UIScrollView(ios)来实现引导页面,效果如下。

test.wxml

<swiper indicator-active-color='#fff' indicator-dots="true" indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}"
  interval="{{interval}}" duration="{{duration}}">
  <block wx:for="{{imgs}}" wx:for-index="index" wx:key="swiperItem" wx:for-item="item">
    <swiper-item class="swiper-items">
      <image class="swiper-image" src="{{item}}"></image>
      <button class="button-img" bindtap="start" wx:if="{{index == imgs.length - 1}}">立即体验</button>
    </swiper-item>
  </block>
</swiper>
View Code

test.wxss

swiper {
  position: absolute;
  height: 100%;
  width: 100%;
}

.swiper-image {
  height: 100%;
  width: 100%;
  opacity:0.9;
}

.button-img{   
   position: relative;
   bottom: 120px;
   height: 40px;
   width: 120px;
   opacity:0.6;
}
View Code

test.js

Page({
  data: {
    imgs: [
      "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2522069454.jpg",
      "https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2522778567.jpg",
      "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2523516430.jpg",
    ],

    img: "http://img.kaiyanapp.com/7ff70fb62f596267ea863e1acb4fa484.jpeg",
    duration: 2000, // 滑动动画时长
    indicatorDots: true, // 是否显示指示点
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔
  },
  start: function () {
    wx.showToast({
      title: '该功能未上线!',
      icon: 'none',
      duration: 1500
    })
  },
})
View Code

当点击"立即体验",会有一个弹窗提示。

本文参考链接:

https://blog.youkuaiyun.com/xiangzhihong8/article/details/80645094

微信小程序的`Swiper`组件是一个常用的轮播图组件,它可以帮助你快速创建滑动效果展示产品图片或者信息。在购物平台上,你可以按照以下步骤实现一个带今日特价区的轮播: 1. **引入Swiper组件**: 首先,在你的页面的`wxml`文件中导入`swiper`组件,以及`indicator-dots`(圆点指示器)和`navigator`(导航条)如果需要。 ```html <view class="container"> <swiper indicator-dots="{{dots}}" autoplay> <block wx:for="{{productImages}}"> <swiper-item> <image src="{{item.url}}" mode="cover" /> </swiper-item> </block> <!-- 特价区 --> <swiper-item class="promotion-swiper"> <view>今日特价:{{todaySpecial}}</view> </swiper-item> </swiper> </view> ``` 2. **配置Swiper属性**: 在对应的`js`文件中,初始化Swiper并设置相关属性,如自动播放、循环模式等。同时,定义`productImages`和`todaySpecial`数组或变量存储商品图片和今日特价信息。 ```javascript Page({ data: { productImages: ['image1', 'image2', ...], todaySpecial: '今日特价商品', dots: true, }, swiperOption: { autoplay: true, loop: true, // 设置循环滚动 interval: 3000, // 每次切换的时间间隔(毫秒) } }) ``` 3. **样式设计**: 使用`wxss`添加一些样式,比如轮播容器的宽度、间距、指示器的颜色等。 ```css .container { position: relative; } .swiper { width: 100%; height: 400rpx; /* 根据需求调整高度 */ } .promotion-swiper { background-color: #f00; /* 特价区背景颜色 */ padding: 20rpx; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值