侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

微信(小程序)  /  管理员 发布于 3年前   309

前言

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

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

效果

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

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

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

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

实现

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

                        {{userInfo.nickName}}              用户:{{YHMC}}    账号:{{YHZH}}    角色:{{YHJS}}    版本:{{version}}                               {{motto}}        

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

/**index.wxss**/.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;}/* 当屏幕向左滑动,出现侧边栏的时候,主页的动画样式 *//* scale:取值范围 0~1 ,表示屏幕大小是原来的百分之几,可以自己修改成 0.8 试下*//* translate(60%,0%) 表示向左滑动的时候,侧边栏占用平时的宽度为 60%  */.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   });  } }

参考资料

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

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

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

您可能感兴趣的文章:

  • 微信小程序在ios下Echarts图表不能滑动的问题解决
  • 微信小程序实现卡片层叠滑动效果
  • 微信小程序导航栏跟随滑动效果的实现代码
  • 微信小程序顶部导航栏滑动tab效果
  • 微信小程序 scroll-view实现锚点滑动的示例
  • 微信小程序 页面滑动事件的实例详解
  • 微信小程序图片横向左右滑动案例
  • 微信小程序 向左滑动删除功能的实现


  • 上一条:
    微信公众平台消息接口校验与消息接口响应实例
    下一条:
    微信小程序实现手势滑动卡片效果
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 小程序开发之跳转微信直播示例api(0个评论)
    • 在uni_app中开发小程序之常用功能示例代码汇总(0个评论)
    • 小程序开发之多端框架:taro(0个评论)
    • 微信小程序前端使用七牛云官方SDK上传七牛云代码示例(0个评论)
    • 百度小程序审核未通过,真机审核存在点击返回键退出小程序...解决方式之一tabBar(0个评论)
    • 近期文章
    • windows系统中安装FFMpeg及在phpstudy环境php7.3 + php-ffmpeg扩展的使用流程步骤(0个评论)
    • 在go语言中对浮点的数组、切片(slice)进行正向排序和反向排序(0个评论)
    • 在go语言中对整数数组、切片(slice)进行排序和反向排序(0个评论)
    • 在go语言中对字符串数组、切片(slice)进行排序和反向排序(0个评论)
    • 最新国内免注册ChatGPT体验站_ChatGPT镜像站访问链接地址2023/3/28持续更新(0个评论)
    • 在Laravel项目中的实现无密码认证之:发送邮箱链接授权(0个评论)
    • 在go语言中使用GoRoutines实现高性能并发批量调用api示例(0个评论)
    • Docker撤回受争议的收费方案,又可以继续使用docker了(0个评论)
    • 在go语言生成唯一ID之SnowFlake算法(0个评论)
    • ChatGPT再出新功能,推出插件功能,能联网、搜索了(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客