微信小程序?qū)崙?zhàn)之輪播圖(3)
輪播圖是大部分應(yīng)用的一個(gè)常用的功能,常用于廣告投放、產(chǎn)品展示、活動(dòng)展示等等。
漂亮的輪播圖效果可以吸引用戶的點(diǎn)擊,達(dá)到推廣產(chǎn)品的作用。
廢話少說,下面開始動(dòng)手。
業(yè)務(wù)需求:
5個(gè)圖片輪番播放,可以左右滑動(dòng),點(diǎn)擊指示點(diǎn)可以切換圖片
重點(diǎn)說明:
由于微信小程序,整個(gè)項(xiàng)目編譯后的大小不能超過1M
查看做輪播圖功能的一張圖片大小都已經(jīng)有100+k了
那么我們可以把圖片放在服務(wù)器上,發(fā)送請(qǐng)求來獲取。
index.wxml:
這里使用小程序提供的<swiper>組件
autoplay:自動(dòng)播放
interval:自動(dòng)切換時(shí)間
duration:滑動(dòng)動(dòng)畫的時(shí)長(zhǎng)
current:當(dāng)前所在的頁(yè)面
bindchange:current 改變時(shí)會(huì)觸發(fā) change 事件
由于<swiper>組件提供的指示點(diǎn)樣式比較單一,另外再自定義指示點(diǎn)的樣式
<view class="recommend" >
<view class="swiper-container">
<swiper autoplay="auto" interval="5000" duration="500" current="{{swiperCurrent}}" bindchange="swiperChange" class="swiper">
<block wx:for="{{slider}}" wx:key="unique">
<swiper-item data-id="{{item.id}}" data-url="{{item.linkUrl}}">
<image src="{{item.picUrl}}" class="img"></image>
</swiper-item>
</block>
</swiper>
<view class="dots">
<block wx:for="{{slider}}" wx:key="unique">
<view class="dot{{index == swiperCurrent ? ' active' : ''}}" bindtap="chuangEvent" id="{{index}}">{{index+1}}</view>
</block>
</view>
</view>
</view>
index.wxss:
.swiper-container{
position: relative;
}
.swiper-container .swiper{
height: 300rpx;
}
.swiper-container .swiper .img{
width: 100%;
height: 100%;
}
.swiper-container .dots{
position: absolute;
right: 40rpx;
bottom: 20rpx;
display: flex;
justify-content: center;
}
.swiper-container .dots .dot{
margin: 0 10rpx;
width: 28rpx;
height: 28rpx;
background: #fff;
border-radius: 50%;
transition: all .6s;
font: 300 18rpx/28rpx "microsoft yahei";
text-align: center;
}
.swiper-container .dots .dot.active{
background: #f80;
color:#fff;
}
index.js:
//導(dǎo)入js
var util = require('../../utils/util.js')
Page({
data: {
slider: [],
swiperCurrent: 0
},
onLoad: function () {
var that = this;
//網(wǎng)絡(luò)訪問,獲取輪播圖的圖片
util.getRecommend(function(data){
that.setData({
slider: data.data.slider
})
});
},
//輪播圖的切換事件
swiperChange: function(e){
//只要把切換后當(dāng)前的index傳給<swiper>組件的current屬性即可
this.setData({
swiperCurrent: e.detail.current
})
},
//點(diǎn)擊指示點(diǎn)切換
chuangEvent: function(e){
this.setData({
swiperCurrent: e.currentTarget.id
})
}
})
utils.js:
//網(wǎng)絡(luò)訪問
function getRecommend(callback) {
wx.request({
url: 'https://c.y.qq.com/musichall/fcgi-bin/fcg_yqqhomepagerecommend.fcg',
data: {
g_tk: 5381,
uin: 0,
format: 'json',
inCharset: 'utf-8',
outCharset: 'utf-8',
notice: 0,
platform: 'h5',
needNewCode: 1,
_: Date.now()
},
method: 'GET',
header: {'content-Type': 'application/json'},
success: function(res){
if(res.statusCode == 200){
callback(res.data);
}
}
})
}
module.exports = {
getRecommend: getRecommend
}
運(yùn)行:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序 swiper組件輪播圖詳解及實(shí)例
- 微信小程序之swiper輪播圖中的圖片自適應(yīng)高度的方法
- 微信小程序視圖容器(swiper)組件創(chuàng)建輪播圖
- 微信小程序使用swiper組件實(shí)現(xiàn)類3D輪播圖
- 微信小程序 輪播圖swiper詳解及實(shí)例(源碼下載)
- 微信小程序使用swiper組件實(shí)現(xiàn)層疊輪播圖
- 微信小程序?qū)崿F(xiàn)的3d輪播圖效果示例【基于swiper組件】
- 微信小程序?qū)崿F(xiàn)輪播圖效果
- 微信小程序?qū)崿F(xiàn)Swiper輪播圖效果
- 微信小程序?qū)崿F(xiàn)輪播圖指示器
相關(guān)文章
微信小程序開發(fā)之自定義tabBar的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序開發(fā)之自定義tabBar的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
echarts同一頁(yè)面中四個(gè)圖表切換的js數(shù)據(jù)交互方法示例
這篇文章主要給大家介紹了關(guān)于echarts同一頁(yè)面中四個(gè)圖表切換的js數(shù)據(jù)交互的相關(guān)資料,文中給出了完整的示例代碼供大家參考學(xué)習(xí),對(duì)大家的學(xué)習(xí)或者工作具有一定的幫助,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
jQuery實(shí)現(xiàn)文字自動(dòng)橫移
本文詳細(xì)介紹了通過jquery尺寸相關(guān)函數(shù)實(shí)現(xiàn)文字自動(dòng)橫移的方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
Javascript+CSS3實(shí)現(xiàn)進(jìn)度條效果
本篇文章主要介紹Javascript+CSS3實(shí)現(xiàn)進(jìn)度條效果,可以實(shí)現(xiàn)給用戶一個(gè)等待的過程,有需要的可以了解一下。2016-10-10
attachEvent的使用方法與傳遞參數(shù)[IE|firefox]

