小程序-- 工作中一些比較實(shí)用的知識(shí)點(diǎn)
小程序中設(shè)置背景圖片
小程序中不能通過css的background 設(shè)置小程序的背景圖片
.container{
background-image:url(../back.jpg)
}
這樣小程序是識(shí)別不出來踪蹬。
目前是通過image的標(biāo)簽進(jìn)行設(shè)置背景圖,但是src也只支持本地圖片胶台,不支持網(wǎng)絡(luò)圖片,如果要實(shí)用網(wǎng)絡(luò)圖片奥裸,只能通過一個(gè)wx.downloadFile這個(gè)api把圖片下載到本地才可以
<view class="head">
<image class="head-img" src="../images/homePage/head.png"></image>
</view>
.head {
width: 100%;
height: 492rpx;
}
.head-img {
width: 100%;
height: 100%;
}
如果要定位的就父層使用position:relative圖片層使用position:absolute
在本頁(yè)面改變上一頁(yè)面的值
此需求類似于點(diǎn)擊輸入框 ( 使用text寫,而不是input沪袭,會(huì)彈出小鍵盤的 ) 進(jìn)入一個(gè)搜索的頁(yè)面湾宙,搜索完成后點(diǎn)擊選擇項(xiàng)或者點(diǎn)擊小鍵盤的右下角都會(huì)返回上一頁(yè)面,點(diǎn)擊選擇項(xiàng)的值或者搜索的值要顯示在輸入框中冈绊;我做的就是在搜索框完成后把搜索完成的值傳到上一頁(yè)面侠鳄。
wxml
<view class='input-container'>
<text class='input-text' name='school' data-type-name='school' bindtap ='onFocusInput' style="color:{{school?'#000':'#ddd'}}">{{school ?school : '請(qǐng)輸入畢業(yè)學(xué)校'}}</text>
</view>
data-type-name 是識(shí)別哪一個(gè)輸入框的。
使用三元運(yùn)算符來顯示空還是學(xué)校
js
//點(diǎn)擊輸入框后開始跳轉(zhuǎn)
onFocusInput:function(event){
var that = this;
console.log("eeee", event);
// var select = event.detail.value
if (event.currentTarget.dataset.typeName =='school'){
var select = that.data.school;
}else{
var select = that.data.industry;
}
console.log("select",select);
var typeName = event.currentTarget.dataset.typeName
console.log("type", typeName);
wx.navigateTo({
url: '../search/search?select=' + select + "&typeName=" + typeName
})
},
search.wxml
<view class='wrap-container'>
<view class='main-container'>
<view class='search-container'>
<input class='search-input' focus='true' placeholder="{{typeName == 'school' ? '搜索畢業(yè)學(xué)校' : '搜索所學(xué)專業(yè)'}}" bindinput='onSearchInput' focus='true' bindconfirm='onBindConfirm' value="{{typeName == 'school' ? school : industry}}"></input>
</view>
</view>
<view class='content-container'>
<scroll-view scroll-y style="height: {{screenHeight}}px;" class='content' >
<view class='content-li' wx:for='{{datasets}}' data-index="{{item}}" bindtap='selectIndex'>
{{item}}
</view>
</scroll-view>
</view>
</view>
search.js
//點(diǎn)擊完成后的函數(shù)
searchConfirmSuccess(res) {
var that = this;
this.setData({
datasets: res.data.data
})
var pages = getCurrentPages() //獲取當(dāng)前加載的頁(yè)面
pages是一個(gè)數(shù)組棧死宣,記錄著頁(yè)面的狀態(tài)以及data值
console.log("pages", pages)
var upToPage = pages[pages.length - 2] //獲取上一頁(yè)面的對(duì)象
// 設(shè)置上以頁(yè)面的data
if (that.data.typeName == 'school') {
upToPage.setData({
school: that.data.key
})
} else if (that.data.typeName == 'industry') {
upToPage.setData({
industry: that.data.key
})
}
wx.navigateBack({
// 返回的頁(yè)面數(shù)
data: 1
})
}
//點(diǎn)擊搜索之后的選擇項(xiàng)的函數(shù)
//搜索后進(jìn)行點(diǎn)擊選擇
selectIndex(options){
console.log("eeee2222222222222", options)
var pages = getCurrentPages() //獲取加載的頁(yè)面
console.log("pages",pages)
var upToPage = pages[pages.length - 2] //獲取上一頁(yè)面的對(duì)象
var currentPage = pages[pages.length - 1] //獲取當(dāng)前頁(yè)面的對(duì)象
var option = currentPage.options //如果要獲取url中所帶的參數(shù)可以查看options
console.log("555555555555555",option)
//點(diǎn)擊選擇框后選擇后的學(xué)形岸瘢或者專業(yè);根據(jù)option.typeName;
let select = options.currentTarget.dataset.index
console.log("location", select)
// 設(shè)置上以頁(yè)面的data
if (option.typeName == 'school') {
upToPage.setData({
school: select
})
} else if (option.typeName == 'industry') {
upToPage.setData({
industry: select
})
}
wx.navigateBack({
// 返回的頁(yè)面數(shù)
data: 1
})
},
小程序的canvas
小程序中不支持轉(zhuǎn)發(fā)到朋友圈毅该,唯一個(gè)方式就是通過保存一張帶有二維碼的圖片博秫,進(jìn)行發(fā)送到朋友圈,這就用到了canvas畫出得到一張帶有二維碼的圖片眶掌,進(jìn)行保存挡育。
wxml
<canvas bindlongtap="saveImage" style="width:{{screenWidth}}px;height:{{screenHeight}}px;" canvas-id="qr-canvas" bindtouchend='bindEnd' disable-scroll = 'true' />
1. style是定義畫布的寬高
2. canvas-id 是定義canvas的唯一標(biāo)識(shí)
3. bindtouchend 是觸摸結(jié)束之后觸發(fā)的函數(shù),當(dāng)成點(diǎn)擊函數(shù)
4. bindlongtap是長(zhǎng)按canvas的觸發(fā)函數(shù)朴爬,可當(dāng)做為長(zhǎng)按保存
5. disable-scroll 是當(dāng)前頁(yè)面禁止?jié)L動(dòng)即寒,
比如我們就繪制一個(gè)這樣的圖片
其中只有可少奮斗4.1年是繪制上去的,其他的是圖片
//res是后臺(tái)返回的數(shù)據(jù) 小于10的數(shù)字保留一個(gè)小數(shù)召噩,大于10 保留整數(shù) 因?yàn)閏anvas.measureText的計(jì)算的是字符串的長(zhǎng)度母赵,所以需要將數(shù)字進(jìn)行數(shù)字化
// 可少奮斗多少年
if (Number(res.years_saved) < 10) {
var headerTextTwo = Number(res.years_saved).toFixed(1) + '';
} else {
var headerTextTwo = parseInt(res.years_saved) + '';
}
var headerTextOne = '可少奮斗'
var headerTextThree = '年'
//根據(jù)屏幕的寬度調(diào)整系數(shù)
var scale = that.data.screenWidth / that.data.DEF_IMAGE_HEADER_WIDTH
//繪制圖片
const ctx = wx.createCanvasContext('qr-canvas');
//計(jì)算第一個(gè)設(shè)置大小之后的長(zhǎng)度
//大寫的都是事先設(shè)置好的字號(hào) ctx.setFontSize(that.data.DEF_HEADER_TEXT_SIZE * scale)
var headerTextWidthOne = ctx.measureText(headerTextOne).width
//計(jì)算第三個(gè)設(shè)置大小之后的長(zhǎng)度
ctx.setFontSize(that.data.DEF_HEADER_TEXT_SIZE * scale)
var headerTextWidthThree = ctx.measureText(headerTextThree).width
console.log("headerTextWidthThree", headerTextWidthThree)
//計(jì)算第二個(gè)設(shè)置大小之后的長(zhǎng)度
ctx.setFontSize(that.data.DEF_HEADER_HEIGHT * scale)
var headerTextWidthTwo = ctx.measureText(headerTextTwo).width
console.log("headerTextWidthTwo6666666666666", headerTextWidthTwo)
//設(shè)置左外邊距 為了居中顯示
var marginLeftOne = (that.data.screenWidth - headerTextWidthOne - headerTextWidthTwo - headerTextWidthThree) / 2
//setFontSize 是設(shè)置字號(hào)的,setFillStyle 是設(shè)置字體顏色具滴,每次設(shè)置都要設(shè)置一下字號(hào)和顏色凹嘲,除非都一樣的字號(hào)和顏色。 ctx.setFontSize(that.data.DEF_HEADER_TEXT_SIZE * scale)
ctx.fillText(headerTextOne, marginLeftOne, that.data.DEF_HEADER_MARGIN_TOP * scale + 100 * scale)
//設(shè)置第二個(gè)文字的顏色
ctx.setFillStyle(that.data.canvasFontColor);
ctx.setFontSize(that.data.DEF_HEADER_HEIGHT * scale)
ctx.fillText(headerTextTwo, marginLeftOne + headerTextWidthOne, that.data.DEF_HEADER_MARGIN_TOP * scale + 100 * scale + 5 * scale)
//設(shè)置第三個(gè)文字的顏色
ctx.setFontSize(that.data.DEF_HEADER_TEXT_SIZE * scale)
ctx.setFillStyle('black');
ctx.fillText(headerTextThree, marginLeftOne + headerTextWidthOne + headerTextWidthTwo, that.data.DEF_HEADER_MARGIN_TOP * scale + 100 * scale)
//背景圖片的大小
ctx.drawImage(canvasHeader, 0, 0, that.data.DEF_IMAGE_HEADER_WIDTH * scale, that.data.DEF_IMAGE_HEADER_HEIGHT * scale);
//draw 可有可無(wú)里面的布爾值构韵,當(dāng)為false時(shí)周蹭,時(shí)清空畫布的內(nèi)容趋艘,true是保留畫布的內(nèi)容,也有回調(diào)函數(shù)谷醉。表示當(dāng)畫布繪制完成
ctx.draw(true,function(res){
console.log("繪制完成")
})
//保存圖片
saveImage() {
console.log("我要保存圖片了")
var that = this
//小程序彈出的一個(gè)數(shù)組致稀,最上層的index為0
wx.showActionSheet({
itemList: ['保存'],
success: res => {
if (res.tapIndex == 0) {
//小程序的圖片輸出一個(gè)路徑
wx.canvasToTempFilePath({
canvasId: 'qr-canvas',
success: res => {
console.log(res)
//保存圖片到相冊(cè)
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: res => {
console.log('saveImageToPhotosAlbum success', res)
wx.showToast({
title: '保存成功',
icon: 'success',
duration: 3000
})
// that.cancel()
},
fail: res => {
console.log(JSON.stringify(res))
}
})
},
}, that)
}
}
})
},
是在工作當(dāng)中出現(xiàn)的有canvas的一個(gè)bug存在,就是設(shè)定disable-scroll 之后俱尼,canvas的長(zhǎng)按(bindlongtap)失效抖单,所以如果設(shè)定start事件和end事件之間的差值做長(zhǎng)按功能就會(huì)導(dǎo)致只有end結(jié)束之后才會(huì)觸發(fā)事件,有點(diǎn)傻遇八,所以就只能去掉disable-scroll 事件矛绘,使用position定位使得canvas處于不同視線內(nèi)。
<!--pages/analysis-canvas/analysis-canvas.wxml-->
<!-- <text>pages/analysis-canvas/analysis-canvas.wxml</text> -->
<view class='container' disable-scrool>
<canvas disable-scroll= 'true' style="width:{{screenWidth}}px;height:{{canvasCodeHeight}}px;position:absolute;top:-2000px;left:0;" canvas-id="qr-code-canvas" />
<canvas style="width:500px;height:400px;position:absolute;top:0;left:-5000px;" canvas-id="qr-share-canvas" />
<canvas bindlongtap="saveImage" style="width:{{screenWidth}}px;height:{{screenHeight}}px; position:absolute;top:0;left:0;" canvas-id="qr-canvas" bindtouchend='bindEnd' />
</view>