微信小程序之側(cè)邊欄滑動實(shí)現(xiàn)過程解析(附完整源碼)
一、效果圖
講什么都不如直接上效果圖好,所以我們先來看下實(shí)現(xiàn)效果如何。
通過滑動屏幕,或者點(diǎn)擊左上角的圖標(biāo)按鈕,都能實(shí)現(xiàn)側(cè)邊欄的劃出效果。
二、原理解析
1.先實(shí)現(xiàn)側(cè)邊欄的內(nèi)容,讓側(cè)邊欄的內(nèi)容居左,然后將側(cè)邊欄的內(nèi)容置于屏幕的最底部。
2.接著實(shí)現(xiàn)主頁的內(nèi)容,并且讓主頁的內(nèi)容默認(rèn)是覆蓋在側(cè)邊欄的內(nèi)容上面。
3.然后,實(shí)現(xiàn)手指滑動屏幕的時(shí)候,主頁的內(nèi)容,向左滑動一定的寬度,讓側(cè)邊欄的內(nèi)容顯示出來,而滑動的效果是通過 css 的 transition 來實(shí)現(xiàn)的。
三、源碼
由于實(shí)現(xiàn)過程的時(shí),我對代碼作了比較詳細(xì)的注釋,所有這里就不廢話,直接上代碼。
slide.wxml
<view class="page"> <!-- 側(cè)邊欄內(nèi)容 --> <view class="page-slidebar"> <view class="page-content"> <view class="wc"> <text>首頁</text> </view> <view class="wc"> <text>導(dǎo)航一</text> </view> <view class="wc"> <text>導(dǎo)航二</text> </view> <view class="wc"> <text>導(dǎo)航三</text> </view> </view> </view> <!-- 主頁內(nèi)容 --> <!-- (open ? 'c-state' : '')三元運(yùn)算符: --> <!-- 1.默認(rèn)情況下 open 為 false --> <!-- 2.當(dāng)點(diǎn)擊左上角圖標(biāo)的時(shí)候或者屏幕向左滑動,open 變化 true --> <!-- 3.當(dāng) open 為 true 時(shí),會加上類名為 c-state 和 cover 的 css 樣式 --> <!-- bindtouchstart、 bindtouchmove、bindtouchend --> <!-- 當(dāng)手指觸摸屏幕并滑動時(shí),所觸發(fā)的三個函數(shù) --> <view bindtouchmove="tap_drag" bindtouchend="tap_end" bindtouchstart="tap_start" class="page-top {{open ? ['c-state','cover'] : ''}} "> <image bindtap="tap_ch" src="../../images/btn.png"></image> <view class='content'> <text>為了幫助開發(fā)者簡單和高效地開發(fā)和調(diào)試微信小程序,我們在原有的公眾號網(wǎng)頁調(diào)試工具的基礎(chǔ)上,推出了全新的 微信開發(fā)者工具,集成了公眾號網(wǎng)頁調(diào)試和小程序調(diào)試兩種開發(fā)模式。 使用公眾號網(wǎng)頁調(diào)試,開發(fā)者可以調(diào)試微信網(wǎng)頁授權(quán)和微信JS-SDK 詳情 使用小程序調(diào)試,開發(fā)者可以完成小程序的 API 和頁面的開發(fā)調(diào)試、代碼查看和編輯、小程序預(yù)覽和發(fā)布等功能。 為了更好的開發(fā)體驗(yàn),我們從視覺、交互、性能等方面對開發(fā)者工具進(jìn)行升級,推出了 1.0.0 版本。</text> <button>按鈕</button> </view> </view> </view>
slide.wcss
/* 全局樣式 */ page, .page { height: 100%; font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, 'Droid Sans Fallback', 'Microsoft Yachei', sans-serif; } /* 側(cè)邊欄樣式 */ .page-slidebar { height: 100%; width: 750rpx; position: fixed; background-color:white; z-index: 0; } /* 控制側(cè)邊欄的內(nèi)容距離頂部的距離 */ .page-content { padding-top: 60rpx; } /* 側(cè)邊欄內(nèi)容的 css 樣式 */ .wc { color:black; padding: 30rpx 0 30rpx 150rpx; border-bottom: 1px solid #eee; } /* 當(dāng)屏幕向左滑動,出現(xiàn)側(cè)邊欄的時(shí)候,主頁的動畫樣式 */ /* scale:取值范圍 0~1 ,表示屏幕大小是原來的百分之幾,可以自己修改成 0.8 試下*/ /* translate(60%,0%) 表示向左滑動的時(shí)候,側(cè)邊欄占用平時(shí)的寬度為 60% */ .c-state { transform: rotate(0deg) scale(1) translate(60%, 0%); -webkit-transform: rotate(0deg) scale(1) translate(60%, 0%); } /* 主頁樣式 */ .page-top { height: 100%; position: fixed; width: 750rpx; background-color:white; z-index: 0; transition: All 0.4s ease; -webkit-transition: All 0.4s ease; } /* 左上角圖標(biāo)的樣式 */ .page-top image { position: absolute; width: 68rpx; height: 68rpx; left: 20rpx; top: 20rpx; } /* 遮蓋層樣式 */ .cover{ width: 100%; height: 100%; background-color:gray; opacity: 0.5; z-index: 9000; } .content{ margin-top: 100rpx; }
slide.js
Page({ data: { open: false, // mark 是指原點(diǎn)x軸坐標(biāo) mark: 0, // newmark 是指移動的最新點(diǎn)的x軸坐標(biāo) newmark: 0, istoright: true }, // 點(diǎn)擊左上角小圖標(biāo)事件 tap_ch: function(e) { if (this.data.open) { this.setData({ open: false }); } else { this.setData({ open: true }); } }, tap_start: function(e) { // touchstart事件 // 把手指觸摸屏幕的那一個點(diǎn)的 x 軸坐標(biāo)賦值給 mark 和 newmark this.data.mark = this.data.newmark = e.touches[0].pageX; }, tap_drag: function(e) { // touchmove事件 this.data.newmark = e.touches[0].pageX; // 手指從左向右移動 if (this.data.mark < this.data.newmark) { this.istoright = true; } // 手指從右向左移動 if (this.data.mark > this.data.newmark) { this.istoright = false; } this.data.mark = this.data.newmark; }, tap_end: function(e) { // touchend事件 this.data.mark = 0; this.data.newmark = 0; // 通過改變 opne 的值,讓主頁加上滑動的樣式 if (this.istoright) { this.setData({ open: true }); } else { this.setData({ open: false }); } } })
四、項(xiàng)目下載
git 在線下載:
https://github.com/yyzheng1729/slide
五、同類文章推薦
微信小程序之下拉列表實(shí)現(xiàn)方法解析(附完整源碼)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js 獲取范圍內(nèi)的隨機(jī)數(shù)實(shí)例代碼
下面小編就為大家?guī)硪黄猨s 獲取范圍內(nèi)的隨機(jī)數(shù)實(shí)例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08JavaScript設(shè)計(jì)模式之中介者模式詳解
當(dāng)對象之間進(jìn)行多對多引用時(shí),進(jìn)行開發(fā),維護(hù),閱讀時(shí)將變得特別痛苦。在這些對象之間添加中間者,使它們都只與中介者,當(dāng)中介者處理完一個對象的請求后,將結(jié)果通知于其他對象2022-08-08JavaScript插入動態(tài)樣式實(shí)現(xiàn)代碼
能夠把CSS樣式包含到HTML頁面中的元素有兩個。其中,<link>元素用于包含來自外部的文件,而<style>元素用于指定嵌入的樣式2012-02-02