WeChat Mini Program Development Guide: About Carousel

高洛峰
Release: 2017-03-04 14:44:28
Original
1689 people have browsed it

The most common one in the application is probably the carousel. This article talks about the implementation of the carousel. Part of the content comes from the official documents, and adds some problems and experiences of the author in actual operation.
This article is based on the public beta version of the WeChat applet, IDE: WeChat Developer Tool 0.10.102800

Component swiper

WeChat Mini Program Development Guide: About Carousel

When the vertical attribute is not set, or When vertical="false" is set, the indicator point is at the bottom of the component, and the image rotation is from left to right. The effect is as follows:

WeChat Mini Program Development Guide: About Carousel

When vertical="true" is set, the indicator point On the right side of the component, the image carousel goes from bottom to top, and the effect is as follows:

WeChat Mini Program Development Guide: About Carousel

Note: swiper is a container class view, but only components can be placed in it, such as placing other nodes. , will be automatically deleted.

swiper-item

The code is as follows:

<!--main.wxml-->
<view>
    <swiper class="swiper_box" indicator-dots="{{indicatorDots}}" vertical="{{vertical}}"
            autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" bindchange="swiperchange">
        <block wx:for="{{images}}">
            <swiper-item>
                <image src="{{item.picurl}}" class="slide-image"/>
            </swiper-item>
        </block>
    </swiper>
</view>
Copy after login
//main.js
//获取应用实例
var app = getApp()
Page({
    data: {
        indicatorDots: true,
        vertical: true,
        autoplay: true,
        interval: 3000,
        duration: 1000,
        loadingHidden: false  // loading
    },

    //事件处理函数
    swiperchange: function(e) {
        // 此处写 轮播 改变时会触发的 change 事件
    },

    onLoad: function() {
        console.log(&#39;onLoad&#39;)
        var that = this

        //sliderList
        wx.request({
            url: &#39;http://huanqiuxiaozhen.com/wemall/slider/list&#39;,
            method: &#39;GET&#39;,
            data: {},
            header: {
                &#39;Accept&#39;: &#39;application/json&#39;
            },
            success: function(res) {
                that.setData({
                    images: res.data
                })
            }
        })
    }
})
Copy after login

item click event

Bind the event on swiper-item and bind it through the data custom tag fixed data. Then get it through event in function.

<!--main.wxml-->
<view>
    <swiper class="swiper_box" indicator-dots="{{indicatorDots}}" vertical="{{vertical}}"
            autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" bindchange="swiperchange">
        <block wx:for="{{images}}">
            <swiper-item bindtap="itemclick" data-id="{{item.id}}" data-name="{{item.name}}">
                <image src="{{item.picurl}}" class="slide-image"/>
            </swiper-item>
        </block>
    </swiper>
</view>
Copy after login
// 轮播item点击事件
itemclick: function(e) {
    wx.showToast({
        title: e.currentTarget.dataset.id + "",
        icon: &#39;success&#39;,
        duration: 2000
    })
},
Copy after login

Note that the corresponding data can be obtained through event in the bound function. For example: e.currentTarget.dataset.id corresponds to data-id

in wxml. Of course, there is another way. There is no need to bind events, by wrapping an a tag around each swiper-item to jump to the page as a hyperlink.

More WeChat Mini Program Development Guide: For articles related to carousels, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template