微信小程序实现炫酷的弹出式菜单特效


Posted in Javascript onJanuary 28, 2019

今天给大家带来一个微信小程序的弹出是菜单效果,老规矩先上效果图。(录制的gif动画有点卡,实际真机或是模拟器上很顺畅)

微信小程序实现炫酷的弹出式菜单特效

先简单说下思路:

1、首先在屏幕的某个位置放几个悬浮按钮,放几个看你需要的功能

2、点击最上层(wxml中最后一个就是最上层)的的按钮后增加背景遮罩,这个遮罩在我前面自定义modal弹框时有用到

3、分别对按钮做旋转和移动动画和透明度,造成动画差异就是位移的动画距离不同

4、收起的时候回到原来位置并且让透明度变成0就ok了

思路说完了,下面开始上实现代码,这里同样也是封装成了组件,方便调用。

微信小程序实现炫酷的弹出式菜单特效

首先是wxml实现

<view class="drawer_screen" bindtap="showOrHide" wx:if="{{isShow}}" catchtouchmove="myCatchTouch"></view>
<view >
 <image src="../../img/add.png" class="buttom" animation="{{animDelLots}}" bindtap="deleteLots"></image>
 <image src="../../img/add.png" class="buttom" animation="{{animAdd}}" bindtap="add"></image>
 <image src="../../img/add.png" class="buttom" animation="{{animMain}}" bindtap="showOrHide"></image>
</view>

然后是wxss

//悬浮按钮
.buttom{
 width: 100rpx;
 height: 100rpx;
 display: flex;
 flex-direction: row;
 position: fixed;
 bottom:60rpx;
 right: 60rpx;
 z-index: 1001;
}
.drawer_screen {
 width: 100%;
 height: 100%;
 position: fixed;
 top: 0;
 left: 0;
 right:0;
 bottom:0;
 z-index: 1000;
 background: #000;
 opacity: 0.5;
 overflow: hidden;
}
.drawer_box {
 overflow: hidden;
 position: fixed;
 z-index: 1001;
}

json文件

{
 "component": true,
 "usingComponents": {}
}

最后是js逻辑实现

// components/Menu/menu.js
var systemInfo = wx.getSystemInfoSync();
Component({
 /**
 * 组件的属性列表
 */
 properties: {
 
 },
 
 /**
 * 组件的初始数据
 */
 data: {
 isShow: false,//是否已经弹出
 animMain: {},//旋转动画
 animAdd: {},//item位移,透明度
 animDelLots: {},//item位移,透明度
 },
 
 /**
 * 组件的方法列表
 */
 methods: {
 //点击弹出或者收起
 showOrHide: function () {
  if (this.data.isShow) {
  //缩回动画
  this.takeback();
  this.setData({
   isShow: false
  })
  } else {
  //弹出动画
  this.popp();
  this.setData({
   isShow: true
  })
  }
 },
 add: function () {
  this.triggerEvent("addEvent")
  this.showOrHide()
 },
 deleteLots: function () {
  this.triggerEvent("deleteLotsEvent")
  this.showOrHide()
 },
 
 //弹出动画
 popp: function () {
  //main按钮顺时针旋转
  var animationMain = wx.createAnimation({
  duration: 500,
  timingFunction: 'ease-out'
  })
  var animationDelLots = wx.createAnimation({
  duration: 500,
  timingFunction: 'ease-out'
  })
  var animationAdd = wx.createAnimation({
  duration: 500,
  timingFunction: 'ease-out'
  })
  animationMain.rotateZ(180).step();
  animationDelLots.translate(0, -200 / 750 * systemInfo.windowWidth).rotateZ(180).opacity(1).step();
  animationAdd.translate(0, -320 / 750 * systemInfo.windowWidth).rotateZ(180).opacity(1).step();
  this.setData({
  animMain: animationMain.export(),
  animDelLots: animationDelLots.export(),
  animAdd: animationAdd.export(),
  })
 },
 //收回动画
 takeback: function () {
  //main按钮逆时针旋转
  var animationMain = wx.createAnimation({
  duration: 500,
  timingFunction: 'ease-out'
  })
  var animationDelLots = wx.createAnimation({
  duration: 500,
  timingFunction: 'ease-out'
  })
  var animationAdd = wx.createAnimation({
  duration: 500,
  timingFunction: 'ease-out'
  })
  animationMain.rotateZ(0).step();
  animationDelLots.translate(0, 0).rotateZ(0).opacity(0).step();
  animationAdd.translate(0, 0).rotateZ(0).opacity(0).step();
  this.setData({
  animMain: animationMain.export(),
  animDelLots: animationDelLots.export(),
  animAdd: animationAdd.export(),
  })
 }
 },
 //解决滚动穿透问题
 myCatchTouch: function () {
 return
 }
})

在要调用的页面json文件引用menu组件(我这里引用了两个组件,还有一个是前面封装的dialog组件)

"usingComponents": {
 "dialog": "/components/Dialog/dialog",
 "menu": "/components/Menu/menu"
 },

然后在调用的wxml中使用

<!--_addEvent 和 _deleteLotsEvent分别是弹出菜单里面按钮对应的事件,需要在调用的js中实现 -->
<menu hidden id='menu' bind:addEvent="_addEvent" bind:deleteLotsEvent="_deleteLotsEvent" />

调用menu组件的js中实现菜单中item的点击事件

_addEvent: function(){
 //do something
 },
 _deleteLotsEvent: function(){
 //do something
 }

整体代码实现就这么多,代码比较简单,如果有不清楚的童鞋,请留言,我将为你们解答。

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

Javascript 相关文章推荐
提高代码性能技巧谈—以创建千行表格为例
Jul 01 Javascript
extjs 学习笔记(二) Ext.Element类
Oct 13 Javascript
jQuery的一些特性和用法整理小结
Jan 13 Javascript
Tips 带三角可关闭的文字提示
Oct 06 Javascript
Jquery实现自定义窗口随意的拖拽
Mar 12 Javascript
JavaScript实现关键字高亮功能
Nov 12 Javascript
jquery计算鼠标和指定元素之间距离的方法
Jun 26 Javascript
浅析jQuery Ajax通用js封装
Jun 22 Javascript
利用vue组件自定义v-model实现一个Tab组件方法示例
Dec 06 Javascript
在vscode里使用.vue代码模板的方法
Apr 28 Javascript
浅谈React 服务器端渲染的使用
May 08 Javascript
js数组去重的N种方法(小结)
Jun 07 Javascript
微信小程序实现页面浮动导航
Jan 28 #Javascript
微信小程序顶部导航栏滑动tab效果
Jan 28 #Javascript
详解基于node.js的脚手架工具开发经历
Jan 28 #Javascript
微信小程序实现顶部导航特效
Jan 28 #Javascript
详解几十行代码实现一个vue的状态管理
Jan 28 #Javascript
vue.js仿hover效果的实现方法示例
Jan 28 #Javascript
vue-for循环嵌套操作示例
Jan 28 #Javascript
You might like
浅析PHP绘图技术
2013/07/03 PHP
Opcache导致php-fpm崩溃nginx返回502
2015/03/02 PHP
windows下安装php的memcache模块的方法
2015/04/07 PHP
PHP加密技术的简单实现
2016/09/04 PHP
PHP页面输出时js设置input框的选中值
2016/09/30 PHP
Yii框架多语言站点配置方法分析【中文/英文切换站点】
2020/04/07 PHP
动态为事件添加js代码示例
2009/02/15 Javascript
一个简单的jQuery插件制作 学习过程及实例
2010/04/25 Javascript
ExtJs的Date格式字符代码
2010/12/30 Javascript
基于jQuery的前端数据通用验证库
2011/08/08 Javascript
Jquery命名冲突解决的五种方案分享
2012/03/16 Javascript
jquery ajax 简单范例(界面+后台)
2013/11/19 Javascript
javascript实现表单提交后,提交按钮不可用的方法
2015/04/18 Javascript
jQuery实现网页抖动的菜单抖动效果
2015/08/07 Javascript
使用canvas实现仿新浪微博头像截取上传功能
2015/09/02 Javascript
JavaScipt中栈的实现方法
2016/02/17 Javascript
Markdown+Bootstrap图片自适应属性详解
2016/05/21 Javascript
AngularJS实现的2048小游戏功能【附源码下载】
2018/01/03 Javascript
Vue中遍历数组的新方法实例详解
2019/07/21 Javascript
Vue组件模板及组件互相引用代码实例
2020/03/11 Javascript
python关闭windows进程的方法
2015/04/18 Python
Android模拟器无法启动,报错:Cannot set up guest memory ‘android_arm’ Invalid argument的解决方法
2016/07/01 Python
python使用xpath中遇到:到底是什么?
2018/01/04 Python
python中使用print输出中文的方法
2018/07/16 Python
Python提取转移文件夹内所有.jpg文件并查看每一帧的方法
2019/06/27 Python
Python for循环与getitem的关系详解
2020/01/02 Python
Python中读取文件名中的数字的实例详解
2020/12/25 Python
如果NULL定义成#define NULL((char *)0)难道不就可以向函数传入不加转换的NULL了吗
2012/02/15 面试题
学校卫生检查制度
2014/02/03 职场文书
领导失职检讨书
2014/02/24 职场文书
教师自我鉴定范文
2014/03/20 职场文书
乱丢垃圾袋检讨书
2014/10/08 职场文书
论群众路线学习心得体会
2014/10/31 职场文书
综合办公室岗位职责
2015/04/11 职场文书
Python基础之数据类型知识汇总
2021/05/18 Python
python机器学习Github已达8.9Kstars模型解释器LIME
2021/11/23 Python