微信小程序?qū)崿F(xiàn)列表左右滑動
做微信小程序開發(fā),和app一樣經(jīng)常會用到列表左右滑動操作(刪除等),目前微信小程序官方?jīng)]有提供相應(yīng)的控件。
只能我們自己來做,方法很多,我這里給個思路,僅供參考,歡迎討論。
1、我們可以把列表的元素放在scroll-view控件中,并且讓scroll-view實現(xiàn)橫向滑動
2、把列表內(nèi)容項的寬度占滿手機寬度,利用rpx特性(自適應(yīng)屏幕),默認(rèn)iphon6就是750rpx,只要設(shè)置大于等于750rpx就可以。
3、監(jiān)聽滑動后列表操作事件,即可
細節(jié)點:
(1)scroll-view實現(xiàn)橫向滑動,這個微信文檔寫的不是很詳細
第一步,wxml中在scroll-view控件中,寫上 scroll-x
<scroll-view scroll-x class='scroll-container'> </scroll-view>
第二步,要在wxss樣式文件中增加上white-space: nowrap;
.scroll-container{ height: 200rpx; background-color: floralwhite; margin-top: 20rpx; display: flex; flex-direction: row; align-items: center; white-space: nowrap; width: 100%;}
第三步:scroll-view 默認(rèn)滑動的時候,是有個線條的。體驗不好,去掉。大家可以對比下效果
::-webkit-scrollbar{ width: 0; height: 0; color: transparent;}
(2)比如滑動刪除第一條數(shù)據(jù)后,第二條數(shù)據(jù)的默認(rèn)是處于滑動后狀態(tài),把操作項都顯示出來了,建議每次操作完,把scroll-view的位置復(fù)原。修改scroll-left的值就好,我每次操作完,就把該值設(shè)置為0。
<scroll-view scroll-x class='scroll-container' scroll-left='{{scroll_left}}'
(3)列表內(nèi)容可能會超出屏幕默認(rèn)值,我這個例子是單行顯示的,所有只要文字大于一定數(shù)量就截取。單獨寫了個函數(shù)把數(shù)據(jù)處理了下,這個可以實際情況實際處理。
代碼如下:
wxml:注意class done1是為了做完成后效果,這里只是演示就簡單實現(xiàn)了下,實際應(yīng)該針對對應(yīng)的列表進行處理,可以考慮把原數(shù)據(jù)做成數(shù)組字典。 /data-content 是為了刪除的時候,知道是刪除了哪一行/
<view> <block wx:for='{{items}}' wx:key=''> <scroll-view scroll-x class='scroll-container' scroll-left='{{scroll_left}}'> <text class='content {{done1}}'>{{item}}</text> <text class='del' data-content='{{index}}' bindtap='del'>刪除</text> <text class='done' data-content='{{index}}' bindtap='done'>完成</text> </scroll-view> </block> </view>
wxss:
::-webkit-scrollbar{ width: 0; height: 0; color: transparent; } .scroll-container{ height: 200rpx; background-color: floralwhite; margin-top: 20rpx; display: flex; flex-direction: row; align-items: center; white-space: nowrap; width: 100%; } .content{ padding-left: 20rpx; display: inline-block; line-height: 200rpx; font-size: 40rpx; width: 750rpx; } .del{ display: inline-flex; width: 200rpx; height: 200rpx; line-height: 200rpx; align-items: center; justify-content: center; background-color: red; color: white; } .done{ display: inline-flex; width: 200rpx; height: 200rpx; line-height: 200rpx; align-items: center; justify-content: center; background-color: green; color: white; } .done1{ text-decoration: line-through; color: gainsboro; }
js:
// pages/test06/test06.js //hhString是為了處理下列表內(nèi)容的,超出了就是用...顯示,可以注釋掉不用 import {hhString} from '../../utils/util.js'; Page({ /** * 頁面的初始數(shù)據(jù) */ data: { items: ['我是第一條測試數(shù)據(jù)', '我是第二條測試數(shù)據(jù)', '我是第三條測試數(shù)據(jù) ', '我是第四條測試數(shù)據(jù)', '我是第五條測試數(shù)據(jù) ', '我是第六條測試數(shù)據(jù),數(shù)據(jù)很多很多很多很多很多', '我是第七條測試數(shù)據(jù)', '我是第八條測試數(shù)據(jù)', '我是第九條測試數(shù)據(jù)'], scroll_left:'0rpx', }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { //hhString是為了處理下列表內(nèi)容的,超出了就是用...顯示,可以注釋掉不用 this.setData({ items:hhString(this.data.items) }) }, del:function(event){ //event.currentTarget.dataset.content獲取到列表的所在的行號 //splice 刪除數(shù)組數(shù)據(jù)函數(shù),第一個參數(shù)是位置,第二個是個數(shù) this.data.items.splice(event.currentTarget.dataset.content,1); this.setData({ items: this.data.items, scroll_left:'0rpx' }); }, done: function (event) { //這里簡易實現(xiàn)了下效果,沒有針對對應(yīng)的行號,實際業(yè)務(wù)再修改 this.setData({ scroll_left: '0rpx', done1: 'done1' }); }, }) util->util.js const hhString = (data)=>{ let hstring = []; for(let val of data){ if(val.length>10){ val = val.substring(0,10); val = val+'...' } hstring.push(val); } return hstring; } module.exports = { hhString:hhString }
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js中的escape及unescape函數(shù)的php實現(xiàn)代碼
js中的escape及unescape函數(shù)的php實現(xiàn)代碼...2007-09-09el-date-picker與el-time-picker的時間格式設(shè)置代碼
這篇文章主要介紹了el-date-picker與el-time-picker的時間格式設(shè)置代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11JavaScript解決浮點數(shù)計算不準(zhǔn)確問題的方法分析
這篇文章主要介紹了JavaScript解決浮點數(shù)計算不準(zhǔn)確問題的方法,結(jié)合實例形式分析了javascript浮點數(shù)運算精度誤差的原因以及相關(guān)的解決方法與具體操作技巧,需要的朋友可以參考下2018-07-07JS?new操作原理及手寫函數(shù)模擬實現(xiàn)示例
這篇文章主要為大家介紹了JS?new操作原理及手寫函數(shù)模擬實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07JS實現(xiàn)金額轉(zhuǎn)換(將輸入的阿拉伯?dāng)?shù)字)轉(zhuǎn)換成中文的實現(xiàn)代碼
這篇文章介紹了JS實現(xiàn)金額轉(zhuǎn)換(將輸入的阿拉伯?dāng)?shù)字)轉(zhuǎn)換成中文的實現(xiàn)代碼,有需要的朋友可以參考一下,希望對大家有用2013-09-09