微信小程序?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ā)送請求來獲取。
index.wxml:
這里使用小程序提供的<swiper>組件
autoplay:自動(dòng)播放
interval:自動(dòng)切換時(shí)間
duration:滑動(dòng)動(dòng)畫的時(shí)長
current:當(dāng)前所在的頁面
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)行:
以上就是本文的全部內(nèi)容,希望對大家的學(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)文章
Extjs4實(shí)現(xiàn)兩個(gè)GridPanel之間數(shù)據(jù)拖拽功能具體方法
這篇文章主要介紹了Extjs4實(shí)現(xiàn)兩個(gè)GridPanel之間數(shù)據(jù)拖拽功能具體方法,有需要的朋友可以參考一下2013-11-11在LayUI圖片上傳中,解決由跨域問題引起的請求接口錯(cuò)誤的方法
今天小編就為大家分享一篇在LayUI圖片上傳中,解決由跨域問題引起的請求接口錯(cuò)誤的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09淺析Javascript中bind()方法的使用與實(shí)現(xiàn)
下面小編就為大家?guī)硪黄獪\析Javascript中bind()方法的使用與實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,一起跟隨小編過來看看吧2016-04-04JavaScript處理XML DOM、XPath和XSLT方法詳解
這篇文章介紹了JavaScript處理XML DOM、XPath和XSLT的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05使用threejs實(shí)現(xiàn)第一人稱視角的移動(dòng)的問題(示例代碼)
第一人稱視角的場景巡檢主要需要解決兩個(gè)問題,人物在場景中的移動(dòng)和碰撞檢測。移動(dòng)與碰撞功能是所有三維場景首先需要解決的基本問題,今天我們就通過最基本的threejs來完成第一人稱視角的場景巡檢功能,感興趣的朋友一起看看吧2022-02-02對layui數(shù)據(jù)表格動(dòng)態(tài)cols(字段)動(dòng)態(tài)變化詳解
今天小編就為大家分享一篇對layui數(shù)據(jù)表格動(dòng)態(tài)cols(字段)動(dòng)態(tài)變化詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10