小程序開發(fā)教程欄目介紹微信小程序開發(fā)的一些問題
推薦(免費(fèi)):小程序開發(fā)教程
微信小程序開發(fā)問題匯總
- 樣式如何使用變量
- video遮罩問題
- 彈幕自動(dòng)上推信息流
- 軟鍵盤問題
- websocket使用
- weapp.socket.io
- 小程序當(dāng)中的使用
小程序開發(fā)告一段落,總結(jié)一下這段時(shí)間小程序開發(fā)遇到的問題及解決方案,react沖沖沖?。?!
樣式如何使用變量
在wxss中,定義變量:width:var(–width–);
在js中,定義變量:viewWidth,并給這個(gè)變量賦予想要的值
在wxml中,使用wxss中的變量和js中的變量:style="–width–: {{ viewWidth }}px"
video遮罩問題
在實(shí)現(xiàn)直播的功能時(shí),我們需要彈出紅包等遮蓋video的處理,此時(shí)會(huì)發(fā)現(xiàn),使用z-index屬性在小程序中是無(wú)效的,微信開發(fā)者文檔提供了cover-view,cover-imge等控件實(shí)現(xiàn)遮罩的功能。
這里值得注意的是cover-view中的background-image屬性是無(wú)效的,所以我們要安放背景圖時(shí)需要使用到cover-image,并且將它的position設(shè)置為absolute, top為0, left也為0即可。
彈幕自動(dòng)上推信息流
首先是將這個(gè)scroll的高度定死,讓scroll自動(dòng)滑動(dòng)到某個(gè)item的位置:
????<scroll-view class="danmu-list" scroll-y="true" scroll-into-view="{{'item_' + danmulist.length}}" style="height: {{windowHeight - 890}}rpx"> ????????<view id="{{'item_' + (index + 1)}}" wx:for="{{danmulist}}" class="{{item.nickName == username ? 'danmu-item owner' : 'danmu-item'}}" wx:key="this"> ??????????<view class="nickname">{{item.nickName}}:</view> ??????????<view class="content {{ item.system ? 'system' : '' }}" style="color: {{ item.system ? '#71f474' : (item.color || '#fff')}}">{{item.content}}</view> ????????</view> ????</scroll-view>
為scroll添加樣式:
.danmu-list?{ ??width:?750rpx; ??height:?290rpx; ??position:?relative; ??padding-top:?27rpx;}.danmu-item?{ ??padding:?0?27rpx;}.danmu-item?.nickname?{ ??color:?#cdd5ff; ??font-size:?26rpx; ??display:?inline-block;}.danmu-item.owner?.nickname?{ ??color:?#ffab00;}.danmu-item?.content?{ ??color:?#ffffff; ??font-size:?26rpx; ??display:?inline-block;}
可以看到在小程序上的實(shí)現(xiàn),明顯比在網(wǎng)頁(yè)上要簡(jiǎn)單,我們只需要一個(gè)scroll-into-view
的屬性,并且為每個(gè)item添加一個(gè)id即可。
那么有沒有純css的實(shí)現(xiàn)呢?當(dāng)然。
我們將item都放在一個(gè)盒子中,讓盒子的相對(duì)于list底部對(duì)齊,overflow則進(jìn)行scroll,這樣同樣能實(shí)現(xiàn)現(xiàn)在的效果。
軟鍵盤問題
此次開發(fā),需要實(shí)現(xiàn)一個(gè)輸入框點(diǎn)擊,軟鍵盤彈起,選擇顏色功能。我們看下效果圖:
那么考慮幾個(gè)問題:
1、選擇顏色時(shí)鍵盤失去焦點(diǎn)會(huì)縮起
微信小程序提供了一個(gè)hold-keyboard
屬性
我在input 中設(shè)定了hold-keyboard="true"
2、軟鍵盤彈出時(shí)會(huì)自動(dòng)把頁(yè)面上推,但是我們僅僅想要軟鍵盤把input框上推而不是整個(gè)頁(yè)面。
分析一下這個(gè)問題,首先是考慮純css解決方案,把頁(yè)面設(shè)為fixed,然而不起作用;接下來(lái)考慮頁(yè)面彈起時(shí)減去軟鍵盤的高度讓它恢復(fù)到原位,這會(huì)帶來(lái)兩個(gè)問題:1)軟鍵盤彈起后才能獲取軟鍵盤高度,這樣一來(lái)頁(yè)面的下落會(huì)產(chǎn)生嚴(yán)重的滯后;2)同樣的不起作用
這個(gè)問題最終的解決方案是這樣的:
首先查看官方文檔,微信小程序提供了一個(gè)adjust-position
屬性
設(shè)定adjust-position=“false"
,此時(shí)的確不會(huì)進(jìn)行頁(yè)面的上推了,但是我們需要的input框上推如何實(shí)現(xiàn)?
我們可以在input的方法參數(shù)e.detail.height中拿到軟鍵盤的高度,設(shè)定input的高度為e.detail.height的高度即可。
最終代碼:
?<cover-view wx:if="{{inputParam.colorShow}}" class="color-check" style="bottom: {{inputParam.bottom + (windowWidth / 750 * 90)}}px"> ???<cover-image class="color-background" src="{{assets}}/images/live-index-danmu-send-color-check.png"></cover-image> ???<cover-view class="color-list"> ?????<cover-view wx:for="{{colorStatusList}}" wx:key="this" catchtap="checkColor" data-index="{{index}}" class="{{item.checked == 0 ? 'color-icon' : 'color-icon with-border'}}" style="background-color: {{item.color}}"></cover-view> ???</cover-view> ?</cover-view> ? ?<view class="enterDanmu" style="bottom: {{inputParam.bottom}}px"> ??<input hold-keyboard="true" catchtap catchfocus="enterMessage" bindconfirm="loseColor" bindinput="getInputValue" placeholder="發(fā)個(gè)彈幕唄~" placeholder-style="font-size: 26rpx; color: #09091b" style="color:{{fontcolor}};" value="{{inputParam.inpuentertValue}}" focus="{{inputParam.focus}}" adjust-position="{{false}}"></input> ?????<image catchtap="sendMessageOperation" class="danmu-btn" src="{{assets}}images/live-index-danmu-send.png"></image> ?</view>
checkColor(e)?{ ????let?colorStatusList?=?this.data.colorStatusList; ????let?index?=?e.currentTarget.dataset.index; ????let?foncolor?=?colorStatusList[index].color; ????let?inputParam?=?this.data.inputParam ????inputParam.focus?=?true ????if?(colorStatusList[index].checked?==?true)?{ ??????colorStatusList[index].checked?=?false ??????foncolor?=?'#09091b' ????}?else?{ ??????for?(let?colorIndex?in?colorStatusList)?{ ????????colorStatusList[colorIndex].checked?=?false ??????} ??????colorStatusList[index].checked?=?true ????} ????this.setData({ ??????colorStatusList:?colorStatusList, ??????fontcolor:?foncolor, ??????inputParam:?inputParam????}) ??}, ??getInputValue(e)?{ ????let?inputParam?=?this.data.inputParam; ????inputParam.inputValue?=?e.detail.value; ????this.setData({ ??????inputParam:?inputParam????}) ??}, ??enterMessage(e)?{ ????let?inputParam?=?this.data.inputParam; ????inputParam.colorShow?=?true, ????inputParam.focus?=?true, ????inputParam.bottom?=?e.detail.height????this.setData({ ??????inputParam:?inputParam, ????}) ??}, ??loseColor()?{ ????let?inputParam?=?this.data.inputParam; ????inputParam.colorShow?=?false; ????inputParam.focus?=?false; ????inputParam.bottom?=?0; ????this.setData({ ??????inputParam:?inputParam, ????}) ??}, ??sendMessageOperation(e)?{ ????let?inputParam?=?this.data.inputParam; ????if?(inputParam.inputValue?!=?'')?{ ??????this.socket.emit('message',?inputParam.inputValue,?this.data.fontcolor); ??????app.api.send_message(this.data.liveId,?this.data.fontcolor,?inputParam.inputValue); ??????inputParam.inputValue?=?''; ??????inputParam.colorShow?=?false ??????inputParam.focus?=?false ??????inputParam.bottom?=?0 ??????this.setData({ ????????inputParam:?inputParam, ??????}) ??????console.log("sendMessageOperation") ????}?else?{ ??????inputParam.inputValue?=?''; ??????inputParam.colorShow?=?false ??????inputParam.focus?=?false ??????this.setData({ ????????inputParam:?inputParam, ??????}) ????} ??}
至于說上面的catchtap則很好理解了,當(dāng)我們要點(diǎn)擊任意處導(dǎo)致失去焦點(diǎn)時(shí),必定要在外層綁定bindtap事件,那么此處就需要使用catchtap阻止事件的冒泡。
值得一提的是,微信小程序也提供了一個(gè)wx.onKeyboardHeightChange(function callback)
方法來(lái)監(jiān)聽鍵盤的高度變化,但是親測(cè)這個(gè)方法并沒有很好用,嘗試了一下就棄之不用了。
websocket使用
我們都知道 HTTP 協(xié)議有一個(gè)缺陷:通信只能由客戶端發(fā)起。那么在這種情況下,如果服務(wù)器有連續(xù)的狀態(tài)變化,客戶端要獲知就非常麻煩。我們只能使用"輪詢",最典型的應(yīng)用場(chǎng)景就是聊天室了。
輪詢的效率低,非常浪費(fèi)資源。因此,工程師們一直在思考,有沒有更好的方法。WebSocket 就是這樣發(fā)明的。
那么如何在微信小程序中使用websocket呢?先來(lái)看看本次的需求:
在觀看直播的過程當(dāng)中,用戶會(huì)進(jìn)行聊天,服務(wù)器要將用戶的彈幕信息推送到每個(gè)用戶的手機(jī)端。
weapp.socket.io
weapp.socket.io是基于socket.io的微信程序環(huán)境中的客戶端,以及socket.io-client瀏覽器版本的完整功能。
安裝方式:
npm?i?weapp.socket.io
簡(jiǎn)單使用的代碼:
<template> ????<view class="content"> ???????<button type="primary" @click="send">發(fā)送消息</button> ????</view></template>
//?引入?weapp.socket.io.js?import?io?from?'@/util/weapp.socket.io.js';export?default?{ ????data()?{ ????????return?{}; ????}, ????onLoad()?{ ????????//?建立一個(gè)socket連接 ????????const?socket?=(this.socket?=?io('https://socket-io-chat.now.sh/')); ????????/** ?????????*?客戶端socket.on()監(jiān)聽的事件: ?????????*/ ????????//?連接成功 ????????socket.on('connect',?()?=>?{ ????????????console.log('連接成功'); ????????}); ????????//?正在連接 ????????socket.on('connecting',?d?=>?{ ????????????console.log('正在連接',?d); ????????}); ????????//?連接錯(cuò)誤 ????????socket.on('connect_error',?d?=>?{ ????????????console.log('連接失敗',?d); ????????}); ????????//?連接超時(shí) ????????socket.on('connect_timeout',?d?=>?{ ????????????console.log('連接超時(shí)',?d); ????????}); ????????//?斷開連接 ????????socket.on('disconnect',?reason?=>?{ ????????????console.log('斷開連接',?reason); ????????}); ????????//?重新連接 ????????socket.on('reconnect',?attemptNumber?=>?{ ????????????console.log('成功重連',?attemptNumber); ????????}); ????????//?連接失敗 ????????socket.on('reconnect_failed',?()?=>?{ ????????????console.log('重連失敗'); ????????}); ????????//?嘗試重新連接 ????????socket.on('reconnect_attempt',?()?=>?{ ????????????console.log('嘗試重新重連'); ????????}); ????????//?錯(cuò)誤發(fā)生,并且無(wú)法被其他事件類型所處理 ????????socket.on('error',?err?=>?{ ????????????console.log('錯(cuò)誤發(fā)生,并且無(wú)法被其他事件類型所處理',?err); ????????}); ????????//?加入聊天室 ????????socket.on('login',?d?=>?{ ????????????console.log(`您已加入聊天室,當(dāng)前共有?${d.numUsers}?人`); ????????}); ????????//?接受到新消息 ????????socket.on('new?message',?d?=>?{ ????????????console.log('new?message',d); ????????}); ????????//?有人加入聊天室 ????????socket.on('user?joined',?d?=>?{ ????????????console.log(`${d.username}?來(lái)了,當(dāng)前共有?${d.numUsers}?人`); ????????}); ????????//?有人離開聊天室 ????????socket.on('user?left',?d?=>?{ ????????????console.log(`${d.username}?離開了,當(dāng)前共有?${d.numUsers}?人`); ????????}); ????}, ????methods:?{ ????????send(){ ????????????//?發(fā)送消息 ????????????this.socket.emit('new?message',?'發(fā)送消息')? ????????} ????}};
小程序當(dāng)中的使用
?initWebSocket(live)?{ ????if(this.socket)?{ ??????this.socket.disconnect(); ??????this.socket?=?null; ????} ????if(live.step?!=?'直播中')?{ ??????return?this.setData({?liveTipTime:?live.start_time?}); ????} ????const?username?=?this.data.username; ????const?timestamp?=?Math.floor(Date.now()/1000/60/10); ????const?token?=?`gz.${timestamp}.${username}`; ????const?socket?=?io(?`${socketHost}/chat?id=${this.data.liveId}&token=${token}`); ????socket.on('connect',?()?=>?{ ??????this.setData({?socketError:?''?}); ??????console.log('connection?created.') ????}); ????socket.on('join',?user?=>?{ ??????let?{?danmulist?}?=?this.data; ??????danmulist.push({?nickName:?user,?content:?'加入了房間',?system:?true?}); ??????this.setData({?danmulist,?onlineUserCount:?this.data.onlineUserCount?+?1?}); ????}); ????socket.on('message',?msg?=>?{ ??????let?{?danmulist?}?=?this.data; ??????danmulist.push({?nickName:?msg.user,?content:?msg.content,?color:?msg.color?||?'#fff'?}); ??????this.videoContext.sendDanmu({?text:?msg.content,?color:?msg.color?||?'#fff'?}) ??????this.setData({?danmulist?}); ??????console.log(msg) ????}); ????socket.on('alluser',?users?=>?{ ??????//console.log('alluser',?users); ??????this.setData({?onlineUserCount:?users.length?}); ????}); ????socket.on('logout',?users?=>?{ ??????console.log('alluser',?users) ??????this.setData({?onlineUserCount:?this.data.onlineUserCount?-?1?}); ????}); ????socket.on('getAlluser',?({?type,?users?})?=>?{ ??????console.log('getAlluser',?type,?users); ??????if(this.data.isAdmin)?{ ????????app.api.lottery_start(type,?users).then(x=>{ ??????????if(!x.length)?{ ????????????return?wx.showModal({?content:?'當(dāng)前已無(wú)符合條件的中獎(jiǎng)候選名單,請(qǐng)稍后再試'?}); ??????????} ??????????wx.showToast({?title:?'抽獎(jiǎng)成功'?}); ??????????this.setData({?activeTab:?0?}); ??????????this.socket.emit('lotteryStart',?type); ??????????this.lottery_result_summary(); ????????}).catch(e=>{ ??????????wx.showModal({?title:?'抽獎(jiǎng)失敗:?'+e,?showCancel:?false?}); ????????}); ??????} ????}); ????socket.on('setScore',?score?=>?{ ??????const?liveIndex?=?this.data.swiperList.findIndex(x=>x.id?==?this.data.liveId); ??????if(this.data.swiperList[liveIndex])?{ ????????this.setData({?[`swiperList[${liveIndex}].score`]:?score?}); ??????} ??????console.log('setScore',?score) ????}); ????socket.on('lotteryStart',?type?=>?{ ??????console.log('lotteryStart',?type) ??????if(this.data.lotteryStatus?==?1)?{ ????????app.api.lottery_result(type).then(lotteryResult=>{ ??????????this.setData({?lotteryStatus:?2,?lotteryResult,?time2:?10?}); ??????????this.countdown(); ????????}); ??????} ????}); ????socket.on('setliveStep',?step?=>?{ ??????console.log('setliveStep',?step) ????}); ????socket.on('error',?e?=>?{ ??????console.error('socket?error',?e); ??????wx.showToast({?title:?'連接彈幕服務(wù)失敗',?icon:?'none'?}); ??????this.setData({?socketError:?e?+?''?}); ????}) ????this.socket?=?socket; ????this.setData({?liveTipTime:?''?}); ??},
想了解更多編程學(xué)習(xí),敬請(qǐng)關(guān)注php培訓(xùn)欄目!
以上是匯總微信小程序開發(fā)問題的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁(yè)開發(fā)工具

SublimeText3 Mac版
神級(jí)代碼編輯軟件(SublimeText3)

閑魚官方微信小程序悄然上線,在小程序中可以發(fā)布閑置與買家/賣家私信交流、查看個(gè)人資料及訂單、搜索物品等,有用好奇閑魚微信小程序叫什么,現(xiàn)在快來(lái)看一下。閑魚微信小程序叫什么答案:閑魚,閑置交易二手買賣估價(jià)回收。1、在小程序中可以發(fā)布閑置、與買家/賣家私信交流、查看個(gè)人資料及訂單、搜索指定物品等功能;2、在小程序的頁(yè)面中有首頁(yè)、附近、發(fā)閑置、消息、我的5項(xiàng)功能;3、想要使用的話必要要開通微信支付才可以購(gòu)買;

實(shí)現(xiàn)微信小程序中的圖片濾鏡效果隨著社交媒體應(yīng)用的流行,人們?cè)絹?lái)越喜歡在照片中應(yīng)用濾鏡效果,以增強(qiáng)照片的藝術(shù)效果和吸引力。在微信小程序中也可以實(shí)現(xiàn)圖片濾鏡效果,為用戶提供更多有趣和創(chuàng)造性的照片編輯功能。本文將介紹如何在微信小程序中實(shí)現(xiàn)圖片濾鏡效果,并提供具體的代碼示例。首先,我們需要在微信小程序中使用canvas組件來(lái)加載和編輯圖片。canvas組件可以在頁(yè)面

實(shí)現(xiàn)微信小程序中的下拉菜單效果,需要具體代碼示例隨著移動(dòng)互聯(lián)網(wǎng)的普及,微信小程序成為了互聯(lián)網(wǎng)開發(fā)的重要一環(huán),越來(lái)越多的人開始關(guān)注和使用微信小程序。微信小程序的開發(fā)相比傳統(tǒng)的APP開發(fā)更加簡(jiǎn)便快捷,但也需要掌握一定的開發(fā)技巧。在微信小程序的開發(fā)中,下拉菜單是一個(gè)常見的UI組件,實(shí)現(xiàn)了更好的用戶操作體驗(yàn)。本文將詳細(xì)介紹如何在微信小程序中實(shí)現(xiàn)下拉菜單效果,并提供具

閑魚官方微信小程序已經(jīng)悄然上線,它為用戶提供了一個(gè)便捷的平臺(tái),讓你可以輕松地發(fā)布和交易閑置物品。在小程序中,你可以與買家或賣家進(jìn)行私信交流,查看個(gè)人資料和訂單,以及搜索你想要的物品。那么閑魚在微信小程序中究竟叫什么呢,這篇教程攻略將為您詳細(xì)介紹,想要了解的用戶們快來(lái)跟著本文繼續(xù)閱讀吧!閑魚微信小程序叫什么答案:閑魚,閑置交易二手買賣估價(jià)回收。1、在小程序中可以發(fā)布閑置、與買家/賣家私信交流、查看個(gè)人資料及訂單、搜索指定物品等功能;2、在小程序的頁(yè)面中有首頁(yè)、附近、發(fā)閑置、消息、我的5項(xiàng)功能;3、

微信小程序?qū)崿F(xiàn)圖片上傳功能隨著移動(dòng)互聯(lián)網(wǎng)的發(fā)展,微信小程序已經(jīng)成為了人們生活中不可或缺的一部分。微信小程序不僅提供了豐富的應(yīng)用場(chǎng)景,還支持開發(fā)者自定義功能,其中包括圖片上傳功能。本文將介紹如何在微信小程序中實(shí)現(xiàn)圖片上傳功能,并提供具體的代碼示例。一、前期準(zhǔn)備工作在開始編寫代碼之前,我們需要先下載并安裝微信開發(fā)者工具,并注冊(cè)成為微信開發(fā)者。同時(shí),還需要了解微信

實(shí)現(xiàn)微信小程序中的圖片旋轉(zhuǎn)效果,需要具體代碼示例微信小程序是一種輕量級(jí)的應(yīng)用程序,為用戶提供了豐富的功能和良好的用戶體驗(yàn)。在小程序中,開發(fā)者可以利用各種組件和API來(lái)實(shí)現(xiàn)各種效果。其中,圖片旋轉(zhuǎn)效果是一種常見的動(dòng)畫效果,可以為小程序增添趣味性和視覺效果。在微信小程序中實(shí)現(xiàn)圖片旋轉(zhuǎn)效果,需要使用小程序提供的動(dòng)畫API。下面是一個(gè)具體的代碼示例,展示了如何在小程

使用微信小程序?qū)崿F(xiàn)輪播圖切換效果微信小程序是一種輕量級(jí)的應(yīng)用程序,具有簡(jiǎn)單、高效的開發(fā)和使用特點(diǎn)。在微信小程序中,實(shí)現(xiàn)輪播圖切換效果是常見的需求。本文將介紹如何使用微信小程序?qū)崿F(xiàn)輪播圖切換效果,并給出具體的代碼示例。首先,在微信小程序的頁(yè)面文件中,添加一個(gè)輪播圖組件。例如,可以使用<swiper>標(biāo)簽來(lái)實(shí)現(xiàn)輪播圖的切換效果。在該組件中,可以通過b

實(shí)現(xiàn)微信小程序中的滑動(dòng)刪除功能,需要具體代碼示例隨著微信小程序的流行,開發(fā)者們?cè)陂_發(fā)過程中經(jīng)常會(huì)遇到一些常見功能的實(shí)現(xiàn)問題。其中,滑動(dòng)刪除功能是一個(gè)常見、常用的功能需求。本文將為大家詳細(xì)介紹如何在微信小程序中實(shí)現(xiàn)滑動(dòng)刪除功能,并給出具體的代碼示例。一、需求分析在微信小程序中,滑動(dòng)刪除功能的實(shí)現(xiàn)涉及到以下要點(diǎn):列表展示:要顯示可滑動(dòng)刪除的列表,每個(gè)列表項(xiàng)需要包
