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