微信小程序?qū)崿F(xiàn)圖片輪播及文件上傳
微信小程序?qū)崿F(xiàn)圖片輪播及文件上傳
剛剛接觸微信小程序,看著網(wǎng)上的資源寫了個小例子,本地圖片輪播以及圖片上傳。
圖片輪播:
index.js
<span style="font-size:14px;">var app = getApp()
Page({
data:{
mode: 'aspectFit',
// src:'../images/timg1.jpg',
imgUrls:[
'../images/1.jpg',
'../images/2.jpg',
'../images/3.jpg',
'../images/4.jpg'
],
indicatorDots: true, //是否出現(xiàn)焦點
autoplay: true, //是否自動播放
interval: 2000, //自動播放時間間隔
duration: 1000, //滑動動畫時間
userInfo: {}
},
onLoad:function(){
console.log('onLoad Test');
}
})</span>
注:imgUrls中為本地圖片數(shù)組。
index.wxml:
<swiper indicator-dots = "{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
<block wx:for="{{imgUrls}}">
<swiper-item class="swiper_i">
<image src="{{item}}" mode="{{mode}}" class="slide-image" />
</swiper-item>
</block>
</swiper>
index.json:
{
"navigationBarTitleText": "相冊"
}
index.wxss:
.slide-image{
width:100%;
height:100%;
}
.swiper_i{
text-align: center;
width:100%;
}
好了,這是圖片輪播的幾個文件。會在app.json中配置。
接下來是圖片上傳的。因為沒有服務(wù)器,暫時不能上傳,但是可以從本地相冊或拍照獲取照片。
upload.js:
var app = getApp()
Page({
data:{
// text:"這是一個頁面"
source: '',
tt:false
},
/**
* 選擇相冊或者相機 配合上傳圖片接口用
*/
onLoad: function() {
var that = this;
wx.chooseImage({
count: 1,
//original原圖,compressed壓縮圖
sizeType: ['original'],
//album來源相冊 camera相機
sourceType: ['album', 'camera'],
//成功時會回調(diào)
success: function(res) {
//重繪視圖
that.setData({
source: res.tempFilePaths,
tt:true
})
/* var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'https://', //僅為示例,非真實的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData:{
'user': 'test'
},
success: function(res){
var data = res.data
//do something
}
})*/
}
})
},
/*onHide:function(){
this.setData({
source:''
})
}*/
})
upload.json:
{
"navigationBarTitleText": "上傳圖片"
}
upload.wxml:
<view class="Container">
<image src="{{source}}" mode="aspectFit" class="image-i"/>
<block wx:if="{{tt}}">
<button type="primary" bindtap="listenerButtonChooseImage">確認上傳</button>
</block>
</view>
upload.wxss:
/* pages/upload/upload.wxss */
.Container{
text-align:center;
width:100%;
}
.image-i{
width:100%;
height:100%;
}
app.js為空。
app.json:
{
"pages": [
"pages/index/index",
"pages/upload/upload"
],
"window": {
"navigationBarTextStyle": "black",
"navigationBarTitleText": "演示2",
"navigationBarBackgroundColor": "#fbf9fe",
"backgroundColor": "#fbf9fe"
},
"networkTimeout": {
"request": 10000,
"connectSocket": 10000,
"uploadFile": 10000,
"downloadFile": 10000
},
"tabBar": {
"list": [{
"pagePath": "pages/index/index",
"text": "顯示圖片",
"iconPath": "pages/images/icon_API.png",
"selectedIconPath": "pages/images/icon_API_HL.png"
},{
"pagePath": "pages/upload/upload",
"text": "上傳",
"iconPath": "pages/images/icon_API.png",
"selectedIconPath": "pages/images/icon_API_HL.png"
}]
},
"debug": true
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
javascript中的箭頭函數(shù)基礎(chǔ)語法及使用場景示例
這篇文章主要為大家介紹了?javascript中的箭頭函數(shù)基礎(chǔ)語法及使用場景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
微信小程序 后臺https域名綁定和免費的https證書申請詳解
這篇文章主要介紹了微信小程序 后臺https域名綁定和免費的https證書申請詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11
Three.js添加陰影和簡單后期處理實現(xiàn)示例詳解
這篇文章主要為大家介紹了Three.js添加陰影和簡單后期處理實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
JavaScript數(shù)據(jù)類型對函數(shù)式編程的影響示例解析
這篇文章主要為大家介紹了JavaScript數(shù)據(jù)類型對函數(shù)式編程的影響示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02

