嘿哟喂!寻寻觅觅何处是,冷冷清清是独行。
效果图:
1、wxml
<view class="page">
<!-- 侧边栏 -->
<view class="menu-slidebar">
<view class="menu-content">
<view class="menu-item">
<text>首页</text>
</view>
<view class="menu-item">
<text>导航一</text>
</view>
<view class="menu-item">
<text>导航二</text>
</view>
<view class="menu-item">
<text>导航三</text>
</view>
</view>
</view>
<!-- 主页内容 -->
<!-- 1.默认情况下 isMenu 为 false -->
<!-- 2.当点击菜单图标或者屏幕向左滑动,isMenu 变化 true -->
<!-- 3.当 isMenu 为 true 时,会加上类名为 menu-cover 的 css 样式 -->
<!-- 当手指触摸屏幕并滑动时,所触发的三个函数:bindtouchstart、bindtouchmove、bindtouchend -->
<view class="page-main {{isMenu ? 'menu-cover' : ''}}" bindtouchstart="tap_start" bindtouchmove="tap_drag" bindtouchend="tap_end" >
<image bindtap="tap_menu" src="../../static/image/menu.png"></image>
<view class='content'>
<text>
主页内容
</text>
</view>
</view>
</view>
2、wcss
page,.page {height: 100%;font-family: 'PingFang SC','Helvetica Neue', Helvetica,'Droid Sans Fallback','Microsoft Yachei',sans-serif;}
/* 侧边栏样式 */
.menu-slidebar {width:75%;height:100%;position: fixed;background-color:white;z-index: 0;}
.menu-content {padding-top: 60rpx;}
.menu-item {color:black;padding: 30rpx 0 30rpx 60rpx;border-bottom: 1px solid #eee; }
/* 主页样式 */
.page-main { height: 100%; position: fixed;width: 750rpx;background-color:white;z-index: 0;transition: all .5s ease; -webkit-transition: all .5s ease;}
.page-main image { position: absolute; width: 68rpx;height: 68rpx;left: 20rpx; top: 20rpx;}
/* 遮盖层样式 当屏幕向左滑动,出现侧边栏的时候,主页的动画样式 translate(75%,0%) 表示向左滑动时,侧边栏占用平宽度为75% */
.menu-cover{width:100%; height:100%;background:rgba(0,0,0,.3); z-index: 9;transform:translate(75%, 0%);-webkit-transform:translate(75%, 0%);}
.content{padding:15px;margin-top: 100rpx; }
3、js
Page({
data: {
isMenu: false, // 默认关闭
mark: 0, // 原点x轴坐标
newmark: 0, // 移动的最新点的x轴坐标
istoright: false,// 默认关闭 只能左向右划
},
// 菜单按钮事件
tap_menu: function (e) {
let that = this;
that.setData({
isMenu: !that.data.isMenu, // 取反
});
},
// 1、touchstart事件
tap_start: function (e) {
this.istoright = this.data.istoright;
// 把手指触摸屏幕的那一个点的 x 轴坐标赋值给 mark 和 newmark
this.data.mark = this.data.newmark = e.touches[0].pageX;
},
///2、touchmove事件
tap_drag: function (e) {
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;
},
// 3、touchend事件
tap_end: function (e) {
// 复位
//this.data.mark = 0;
//this.data.newmark = 0;
if (this.istoright) {
this.setData({
isMenu: true
});
} else {
this.setData({
isMenu: false
});
}
}
})
人生所见皆如是,生活处处是风景,壮哉!
参考的来源有一个小小的 bug,我修复了,当然你可以测试一下参考来源的代码,看能不能找到,看你细不细心发现了
参考:https://blog.csdn.net/weidong_y/article/details/82289375
参考的github:https://github.com/yyzheng1729/slide