微信小程序基于本地緩存實(shí)現(xiàn)點(diǎn)贊功能的方法
本文實(shí)例講述了微信小程序基于本地緩存實(shí)現(xiàn)點(diǎn)贊功能的方法。分享給大家供大家參考,具體如下:
wxml中的寫法
注意:
1. 使用wx:if="{{condition}}" wx:else
實(shí)現(xiàn)圖標(biāo)的切換效果;
2. 為圖片綁定點(diǎn)擊事件bindtap="toCollect"
,兩個(gè)image標(biāo)簽都要綁定!
<image wx:if="{{collection}}" src="/images/icon/pic1.png" bindtap="toCollect"></image> <image wx:else src="/images/icon/pic2.png" bindtap="toCollect"></image>
js中的寫法:
Page({ data: { }, onLoad: function(option) { // 獲取接收到的id值 var getId = option.id; // 讓接收到的id值傳遞到data:{}里面 this.setData({ currentId: getId }); // 讀取所有的文章列表點(diǎn)贊緩存狀態(tài) var cache = wx.getStorageSync('cache_key'); // 如果緩存狀態(tài)存在 if (cache) { // 拿到所有緩存狀態(tài)中的1個(gè) var currentCache = cache[getId]; // 把拿到的緩存狀態(tài)中的1個(gè)賦值給data中的collection,如果當(dāng)前文章沒有緩存狀態(tài),currentCache 的值就是 false,如果當(dāng)前文章的緩存存在,那么 currentCache 就是有值的,有值的說明 currentCache 的值是 true this.setData({ collection: currentCache }) } else { // 如果所有的緩存狀態(tài)都不存在 就讓不存在的緩存存在 var cache = {}; // 既然所有的緩存都不存在,那么當(dāng)前這個(gè)文章點(diǎn)贊的緩存也不存在,我們可以把當(dāng)前這個(gè)文章點(diǎn)贊的緩存值設(shè)置為 false cache[getId] = false; // 把設(shè)置的當(dāng)前文章點(diǎn)贊放在整體的緩存中 wx.setStorageSync('cache_key',cache); } }, // 點(diǎn)擊圖片的點(diǎn)贊事件 這里使用的是同步的方式 toCollect: function(event) { // 獲取所有的緩存 var cache = wx.getStorageSync('cache_key'); // 獲取當(dāng)前文章是否被點(diǎn)贊的緩存 var currentCache = cache[this.data.currentId]; // 取反,點(diǎn)贊的變成未點(diǎn)贊 未點(diǎn)贊的變成點(diǎn)贊 currentCache = !currentCache; // 更新cache中的對應(yīng)的1個(gè)的緩存值,使其等于當(dāng)前取反的緩存值 cache[this.data.currentId] = currentCache; // 重新設(shè)置緩存 wx.setStorageSync('cache_key',cache); // 更新數(shù)據(jù)綁定,從而切換圖片 this.setData({ // collection 默認(rèn)的是 false collection: currentCache }); // 交互反饋 wx.showToast({ title: currentCache?'點(diǎn)贊':'取消', icon: 'success', duration: 2000 }); } })
js中操作反饋—wx.showModal的寫法:
// 點(diǎn)擊圖片的點(diǎn)贊事件 這里使用的是同步的方式 toCollect: function(event) { // 獲取緩存,得到當(dāng)前文章是否被點(diǎn)贊 var cache = wx.getStorageSync('cache_key'); // 獲取當(dāng)前文章是否被點(diǎn)贊的緩存 var currentCache = cache[this.data.currentId]; // 取反,點(diǎn)贊的變成未點(diǎn)贊 未點(diǎn)贊的變成點(diǎn)贊 currentCache = !currentCache; // 更新cache中的對應(yīng)的1個(gè)的緩存值,使其等于當(dāng)前取反的緩存值 cache[this.data.currentId] = currentCache; // 調(diào)用 showModal方法 this.showModal(cache,currentCache); }, showModal: function(cache,currentCache) { var that = this; wx.showModal({ title: "點(diǎn)贊" content: currentCache?"要點(diǎn)贊嗎?":"要取消贊嗎?", showCancel: "true", cancelText: "取消", cancelColor: "#666", confirmText: "確定", confirmColor: "#222", success: function(res) { if (res.confirm) { // 重新設(shè)置緩存 wx.setStorageSync('cache_key',cache); // 更新數(shù)據(jù)綁定,從而切換圖片 that.setData({ collection: currentCache }) } } }) }
希望本文所述對大家微信小程序開發(fā)有所幫助。
- 微信小程序?qū)崿F(xiàn)點(diǎn)贊業(yè)務(wù)
- 小程序云開發(fā)教程如何使用云函數(shù)實(shí)現(xiàn)點(diǎn)贊功能
- 微信小程序?qū)崿F(xiàn)點(diǎn)贊、取消點(diǎn)贊功能
- 小程序?qū)崿F(xiàn)列表點(diǎn)贊功能
- 微信小程序?qū)崿F(xiàn)列表頁的點(diǎn)贊和取消點(diǎn)贊功能
- 小程序點(diǎn)贊收藏功能的實(shí)現(xiàn)代碼示例
- 微信小程序項(xiàng)目總結(jié)之點(diǎn)贊 刪除列表 分享功能
- 微信小程序小組件 基于Canvas實(shí)現(xiàn)直播點(diǎn)贊氣泡效果
- 小程序animate動(dòng)畫實(shí)現(xiàn)直播間點(diǎn)贊
相關(guān)文章
微信頁面彈出鍵盤后iframe內(nèi)容變空白的解決方案
當(dāng)鍵盤彈出后,頁腳也被頂起來;而當(dāng)搜索完(要刷新整體頁面),鍵盤縮回后,iframe里 鍵盤當(dāng)住的地方變成白色。怎么解決這個(gè)問題呢?下面腳本之家小編給大家分享微信頁面彈出鍵盤后iframe內(nèi)容變空白的解決方案,一起看看吧2017-09-09document.createElement()用法及注意事項(xiàng)(ff下不兼容)
今天處理了一個(gè)日期選擇器的ie和ff的兼容問題,本來這種情況就很難找錯(cuò)誤,找了好久才把錯(cuò)誤定位到j(luò)s中創(chuàng)建元素的方法document.createElement(),這個(gè)方法在ie下支持這樣創(chuàng)建元素2013-03-03JavaScript數(shù)據(jù)類型轉(zhuǎn)換簡單方法舉例
JavaScript是一種無類型語言,但同時(shí)JavaScript提供了一種靈活的自動(dòng)類型轉(zhuǎn)換的處理方式,下面這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)據(jù)類型轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2023-12-12JavaScript修改、刪除數(shù)組中某個(gè)對象的某個(gè)屬性幾種方法
在JavaScript開發(fā)中,經(jīng)常需要修改數(shù)組中對象的屬性,下面這篇文章主要介紹了JavaScript修改、刪除數(shù)組中某個(gè)對象的某個(gè)屬性幾種方法,需要的朋友可以參考下2024-09-09