轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

微信小程序之自定义抽屉菜单(从下拉出)实例

发布者: tcssh | 发布时间: 2018-7-12 10:04| 查看数: 6399| 评论数: 1|帖子模式

作者:michael_ouyang,来自授权地址 
微信提供了动画api,就是下面这个 

相关链接:

通过使用这个创建动画的api,可以做出很多特效出来 
下面介绍一个抽屉菜单的案例

实现代码: 
wxml:

  1. <!--button-->
  2. <view class="btn" bindtap="powerDrawer" data-statu="open">button</view>
  3. <!--mask-->
  4. <view class="drawer_screen" bindtap="powerDrawer" data-statu="close" wx:if="{{showModalStatus}}"></view>
  5. <!--content-->
  6. <!--使用animation属性指定需要执行的动画-->
  7. <view animation="{{animationData}}" class="drawer_attr_box" wx:if="{{showModalStatus}}">
  8. <!--drawer content-->
  9. <view class="drawer_content">
  10. <view class="drawer_title line">菜单1</view>
  11. <view class="drawer_title line">菜单2</view>
  12. <view class="drawer_title line">菜单3</view>
  13. <view class="drawer_title line">菜单4</view>
  14. <view class="drawer_title">菜单5</view>
  15. </view>
  16. </view>

wxss:

  1. /*button*/
  2. .btn {
  3. width: 80%;
  4. padding: 20rpx 0;
  5. border-radius: 10rpx;
  6. text-align: center;
  7. margin: 40rpx 10%;
  8. background: #0C1939;
  9. color: #fff;
  10. }
  11. /*mask*/
  12. .drawer_screen {
  13. width: 100%;
  14. height: 100%;
  15. position: fixed;
  16. top: 0;
  17. left: 0;
  18. z-index: 1000;
  19. background: #000;
  20. opacity: 0.2;
  21. overflow: hidden;
  22. }
  23. /*content*/
  24. .drawer_attr_box {
  25. width: 100%;
  26. overflow: hidden;
  27. position: fixed;
  28. bottom: 0;
  29. left: 0;
  30. z-index: 1001;
  31. background: #fff;
  32. }
  33. .drawer_content {
  34. padding: 20rpx 40rpx;
  35. height: 470rpx;
  36. overflow-y: scroll;
  37. }
  38. .drawer_title{
  39. padding:20rpx;
  40. font:42rpx "microsoft yahei";
  41. text-align: center;
  42. }
  43. .line{
  44. border-bottom: 1px solid #f8f8f8;
  45. }

js:

  1. Page({
  2. data: {
  3. showModalStatus: false
  4. },
  5. powerDrawer: function (e) {
  6. var currentStatu = e.currentTarget.dataset.statu;
  7. this.util(currentStatu)
  8. },
  9. util: function(currentStatu){
  10. /* 动画部分 */
  11. // 第1步:创建动画实例
  12. var animation = wx.createAnimation({
  13. duration: 200, //动画时长
  14. timingFunction: "linear", //线性
  15. delay: 0 //0则不延迟
  16. });
  17. // 第2步:这个动画实例赋给当前的动画实例
  18. this.animation = animation;
  19. // 第3步:执行第一组动画:Y轴偏移240px后(盒子高度是240px),停
  20. animation.translateY(240).step();
  21. // 第4步:导出动画对象赋给数据对象储存
  22. this.setData({
  23. animationData: animation.export()
  24. })
  25. // 第5步:设置定时器到指定时候后,执行第二组动画
  26. setTimeout(function () {
  27. // 执行第二组动画:Y轴不偏移,停
  28. animation.translateY(0).step()
  29. // 给数据对象储存的第一组动画,更替为执行完第二组动画的动画对象
  30. this.setData({
  31. animationData: animation
  32. })
  33. //关闭抽屉
  34. if (currentStatu == "close") {
  35. this.setData(
  36. {
  37. showModalStatus: false
  38. }
  39. );
  40. }
  41. }.bind(this), 200)
  42. // 显示抽屉
  43. if (currentStatu == "open") {
  44. this.setData(
  45. {
  46. showModalStatus: true
  47. }
  48. );
  49. }
  50. }
  51. })

效果:

最新评论

我想我是海 发表于 2022-7-5 23:15
免费源代码

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表