|
以前工作沒直接進(jìn)行過小程序的開發(fā),最近閑了下來就趕緊學(xué)習(xí)一下。因?yàn)閺牧汩_始,所以沒有使用任何框架及UI庫,記錄一下本次開發(fā)中踩過的坑吧~ 展示效果(界面樣式設(shè)計(jì)與交互來自iOS 4.8.0版本知乎App):
動(dòng)態(tài)效果請(qǐng)移步到GitHub查看。 一、開始前的準(zhǔn)備
二、初始化一個(gè)小程序
weChatApp |___client | |___assets // 存儲(chǔ)圖片 | |___pages // 頁面 | | |___index // 首頁 | | |___index.wxml // 頁面結(jié)構(gòu)文件 | | |___index.wxss // 樣式表文件 | | |___index.js // js文件 | |___utils // 全局公共函數(shù) | |___app.js // 系統(tǒng)的方法處理文件 | |___app.json // 系統(tǒng)全局配置文件 | |___app.wxss // 全局的樣式表 | |___config.json // 域名等配置文件 |___project.config.json |___README
{
// 頁面路由
"pages": [
"pages/index/index", // 首頁
"pages/findMore/findMore", // 想法頁(開始起名為發(fā)現(xiàn)頁面,后來沒改/(ㄒoㄒ)/~~)
"pages/userCenter/userCenter", // 更多(同上,原來起名為個(gè)人中心o(╯□╰)o)
"pages/market/market", // 市場(chǎng)
"pages/searchResult/searchResult",// 搜索結(jié)果頁
"pages/message/message", // 消息列表頁
"pages/titleDetail/titleDetail", // 點(diǎn)擊標(biāo)題進(jìn)入的問題詳情頁
"pages/contentDetail/contentDetail"// 點(diǎn)擊內(nèi)容進(jìn)入的回答詳情頁
],
// 窗口
"window": {
"backgroundColor": "#FFF", // 窗口的背景色
"backgroundTextStyle": "dark", // 下拉背景字體、loading 圖的樣式,僅支持 dark/light
"navigationBarBackgroundColor": "#FFF",// 頂部tab背景顏色
"navigationBarTitleText": "知小乎", //頂部顯示標(biāo)題
"navigationBarTextStyle": "black", // 導(dǎo)航欄標(biāo)題顏色,僅支持 black/white
"enablePullDownRefresh": true // 是否開啟下拉刷新
},
// tab導(dǎo)航條
"tabBar": {
"backgroundColor": "#fff", // 背景顏色
"color": "#999", // 默認(rèn)文字顏色
"selectedColor": "#1E8AE8", // 選中時(shí)文字顏色
"borderStyle": "white", // tabbar上邊框的顏色, 僅支持 black/white
/**
* tab列表,最少2個(gè),最多5個(gè)
* selectedIconPath: 選中時(shí)圖片
* iconPath: 默認(rèn)圖片
* pagePath: 對(duì)應(yīng)頁面路由
* text: 對(duì)應(yīng)文案
**/
"list": [{
"selectedIconPath": "assets/home-light.png",
"iconPath": "assets/home.png",
"pagePath": "pages/index/index",
"text": "首頁"
}, {
"selectedIconPath": "assets/find-light.png",
"iconPath": "assets/find.png",
"pagePath": "pages/findMore/findMore",
"text": "想法"
},
{
"selectedIconPath": "assets/market-light.png",
"iconPath": "assets/market.png",
"pagePath": "pages/market/market",
"text": "市場(chǎng)"
},
{
"selectedIconPath": "assets/msg-light.png",
"iconPath": "assets/msg.png",
"pagePath": "pages/message/message",
"text": "消息"
}, {
"selectedIconPath": "assets/more-light.png",
"iconPath": "assets/more.png",
"pagePath": "pages/userCenter/userCenter",
"text": "更多"
}]
}
}
三、開發(fā)中的遇到的問題及解決方案1、小程序渲染HTML片段
看了網(wǎng)頁版知乎,接口返回的回答數(shù)據(jù)是一段HTML的代碼片段,所以答案中可以在任意位置都插入圖片。
經(jīng)過反復(fù)嘗試,發(fā)現(xiàn)原生寫法不支持渲染一段HTML代碼片段,因此放棄了返回HTML的代碼片段的做法,所以我的回答列表中也沒有圖片(?_?)。 但在調(diào)研中發(fā)現(xiàn)了一個(gè)自定義組件:wxParse-微信小程序富文本解析組件,還沒嘗試使用,準(zhǔn)備在下次優(yōu)化項(xiàng)目時(shí)嘗試一下。
2、首頁的頂部tab切換 實(shí)現(xiàn)思路 每個(gè)可點(diǎn)擊的tab分別綁定data-index,在最外層bindtap綁定的方法中獲取所點(diǎn)擊的tab的index值,再根據(jù)index的值分別顯示對(duì)應(yīng)的tab-content
<view class="tab-wrapper" bindtap="setActive">
<view class="tab-item {{isActive == 0 ? 'tab-item-active' : ''}}" data-index="0">關(guān)注</view>
<view class="tab-item {{isActive == 1 ? 'tab-item-active' : ''}}" data-index="1">推薦</view>
<view class="tab-item {{isActive == 2 ? 'tab-item-active' : ''}}" data-index="2">熱榜</view>
<view class="tab-item-line" animation="{{animationData}}"></view>
</view>
<view class="tab-content {{isActive == 0 ? 'show' : 'hide'}}">
// ...
</view>
<view class="tab-content {{isActive == 1 ? 'show' : 'hide'}}">
// ...
</view>
<view class="tab-content {{isActive == 2 ? 'show' : 'hide'}}">
// ...
</view>
3、上拉加載和下拉刷新 實(shí)現(xiàn)思路 上拉加載:emmmmmm......我指的上拉加載是觸底后的加載更多,怕跟大家理解的不一樣o(╯□╰)o。
下拉刷新:
要注意的是,每次對(duì)數(shù)組進(jìn)行操作后,都要使用setData對(duì)原數(shù)組重新賦值,否則數(shù)據(jù)不會(huì)更新的啊( ⊙ o ⊙ )!
4、搜索歷史的存儲(chǔ) 實(shí)現(xiàn)思路 wx.setStorage、wx.getStorage和wx.removeStorage 存儲(chǔ)搜索歷史:
顯示搜索歷史:
刪除搜索歷史:
5、swiper輪播組件 在想法頁的輪播組件中,原知乎App中的xxxx人正在討論是嵌在輪播模塊內(nèi)的垂直方向的文字輪播,但是小程序中的swiper輪播組件不支持互相嵌套,因此沒能實(shí)現(xiàn)該部分,只好換一種樣式去寫/(ㄒoㄒ)/~~。
6、滾動(dòng)吸頂 頁面中的標(biāo)題欄在滾動(dòng)到頂部時(shí),吸頂展示。 實(shí)現(xiàn)效果
實(shí)現(xiàn)方案
<view class="find-hot-header fixed" wx:if="{{scrollTop >= 430}}">
<view class="find-hot-title">最近熱門</view>
</view>
<view class="find-hot-header">
<view class="find-hot-title">最近熱門</view>
</view>
7、展開和收起全文 展示效果
文字部分默認(rèn)添加class,超出兩行文字顯示省略號(hào)。
.text-overflow{
height: 85rpx;
display: -webkit-box;
word-break: break-all;
text-overflow: ellipsis;
overflow: hidden;
-webkit-box-orient: vertical;
-webkit-line-clamp:2;
}
點(diǎn)擊展開全文和收起全文時(shí)對(duì)showIndex[index]的值取反,對(duì)應(yīng)添加或移除該class。
<view class="find-hot-content {{!showIndex[index] ? 'text-overflow' : ''}}">
{{item.content}}
</view>
<view wx:if="{{!showIndex[index]}}" class="find-show-all" data-index="{{index}}" bindtap="toggleShow">展開全文</view>
<view wx:if="{{showIndex[index]}}" class="find-show-all" data-index="{{index}}" bindtap="toggleShow">收起全文</view>
8、更改switch樣式 switch類名如下,一定要加上父類,不然全局的都會(huì)被改掉_(:з」∠)_。
父類 .wx-switch-input{
display: inline-block;
position: absolute;
top: 20rpx;
right: 20rpx;
width: 84rpx;
height: 44rpx;
}
父類 .wx-switch-input::before{
width: 80rpx;
height: 40rpx;
}
父類 .wx-switch-input::after{
width: 40rpx;
height: 40rpx;
}
四、總結(jié)通過這次小程序的開發(fā),學(xué)到了很多東西,雖然遇到了很多問題,但解決問題的過程讓我收獲的更多,動(dòng)手實(shí)踐才是學(xué)習(xí)的最好方式。 另外,此次項(xiàng)目中仍有許多功能不夠完善,一些細(xì)節(jié)還可以繼續(xù)優(yōu)化,長(zhǎng)路漫漫啊(?•??•?) ?。 如果文章中有錯(cuò)誤和不足歡迎批評(píng)指正。 |