栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

微信小程序实现左侧滑栏过程解析

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信小程序实现左侧滑栏过程解析

前言

一直想给项目中的小程序设置侧滑栏,将退出按钮放到侧滑中,但是小程序没有提供相应的控件和API,因此只能自己手动实现,网上很多大神造的轮子很不错,本文就在是站在巨人的肩膀上实现。

hexo图片不显示问题,可前往简书

效果

先看看效果,我的侧滑栏需要列出如下信息:

初始状态下,左下角设置悬空按钮

点击悬空按钮,侧边栏拉出,悬空按钮旋转180度

主页内容向右滑动一定比例,并设置阴影遮罩

实现

首先将xml文件分为三部分,一部分是主页内容,一部分是侧滑栏内容,一部分是悬浮按钮。




 
  
   
    
    
     
     {{userInfo.nickName}}
    
   
   
   
    
     
    
   
  
 

 
  
   {{motto}}
  
 
 
 
  
 

wxss文件,样式文件中,主要是.c-state,.c-button-open,.cover三个样式。


.userinfo {
 display: flex;
 flex-direction: column;
 align-items: center;
}

.userinfo-avatar {
 width: 128rpx;
 height: 128rpx;
 margin: 20rpx;
 border-radius: 50%;
}

.userinfo-nickname {
 color: #aaa;
}

.account-info {
  margin-top: 50rpx;
}
.account-info-item {
 display: flex;
 align-items: center;
 height: 54px;
 margin-left: 10rpx;
 border-bottom: 1px solid #eee;
}



.page-slidebar {
  height: 100%;
  width: 65%;
  position: fixed;
  background-color:white;
  z-index: 0;
}
 
.page-top {
  height: 100%;
  position: fixed;
  width: 100%;
  background-color:white;
  z-index: 0;
  transition: All 0.4s ease;
  -webkit-transition: All 0.4s ease;
}


.page-content {
  padding-top: 60rpx;
}




.c-state {
  transform: rotate(0deg) scale(1) translate(65%, 0%);
  -webkit-transform: rotate(0deg) scale(1) translate(65%, 0%);
}

.floatbutton {
 position: fixed;
 width: 100rpx;
 height: 100rpx;
 bottom: 140rpx;
 left: 40rpx;
 z-index: 9999;
}

.c-button-open {
 transform: rotate(180deg) scale(1) translate(65%, 0%);
 -webkit-transform: rotate(180deg) scale(1) translate(65%, 0%);
 transition-duration:0.5s;
 -webkit-transition-duration: 0.5s;
 left: 60%;
}


.cover{
  width: 100%;
  height: 100%;
  background-color:gray;
  opacity: 0.5;
  z-index: 9000;
}

.quit-view {
 height: 5%;
 width: 65%;
}

.quit-btn {
 position: fixed;
 bottom: 5rpx;
 z-index: 999;
 color: #fff;
 width: 65%;
 border-radius: 5rpx;
 background-color: #064acb;
}

js文件,这里不涉及我demo中申请用户信息内容。

// 点击左下角小图标事件
 openSlider: function (e) {
  if (this.data.open) {
   this.setData({
    open: false
   });
  } else {
   this.setData({
    open: true
   });
  }
 },
 tap_start: function (e) {
  // touchstart事件
  // 把手指触摸屏幕的那一个点的 x 轴坐标赋值给 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
   });
  }
 }

参考资料

微信小程序之侧边栏滑动实现(附完整源码)

微信小程序侧边栏滑动特效(左右滑动)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/74114.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号