微信小程序 动画之一(侧边菜单展开和闭合)

1.效果如下:

在这里插入图片描述
在这里插入图片描述

2,全部代码

1.js

Page({
  data: {
    choose: false,
    animationData: {},
    stopBtn: true, //动画未执行完之前禁用按钮
  },
  showContent: function (e) {
    var that = this;
    // 创建一个动画实例
    var animation = wx.createAnimation({
      duration: 500,      // 动画持续时间
      timingFunction: 'linear'      // 定义动画效果,当前是匀速
    })
    that.animation = animation
    animation.height("0").opacity(0).step()
    that.setData({
      // 通过export()方法导出数据
      animationData: animation.export(),
      choose: true,
      width: 200,
    })
    // 设置setTimeout来改变高度以及透明度,实现有感觉的展开
    setTimeout(function () {
      animation.height("60rpx").opacity(1).step({
        duration: 500
      })
      that.setData({
        animationData: animation.export(),
      })
    }, 50)
    //在动画时间禁用按钮
    setTimeout(function () {
      that.setData({
        stopBtn: false
      })
    }, 500)
  },

  // 隐藏
  hideContent: function (e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.height(0).opacity(0).step({
      duration: 500
    })
    that.setData({
      animationData: animation.export()
    })
    setTimeout(function () {
      animation.height("60rpx").step();
      that.setData({
        animationData: animation.export(),
        choose: false,
        width: 50,
      })
    }, 500)
    //收回动画开始禁用按钮
    that.setData({
      stopBtn: true,
    })
  },
})

2.wxml

<view class="fixBox" bindtap="{{ stopBtn ? 'showContent' : 'hideContent' }}" style="width:{{width}}px;">
  <view wx:if="{{choose}}" class='companyInfo' animation='{{animationData}}'>
      *[内容自己添加]
  </view>
  <view wx:else>
       *[内容自己添加]
  </view>
</view>

3.wxss

.fixBox{
	width: 50px;
	text-align: center;
	height:40px;
	line-height: 40px;
	border-top: 1px solid #333333;
	border-bottom: 1px solid #333333;
	border-right: 1px solid #333333;
	border-radius: 0px 10px 10px 0px;
	position: fixed;
	top: 25%;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中使用 Vant 组件库的侧边导航栏滚动,需要首先在项目中引入 Vant 组件库,并且按照 Vant 的文档进行组件的配置和使用。 首先,在项目根目录下的 `app.json` 文件中配置 Vant 组件的全局样式,示例如下: ```json { "style": "vant-weapp/dist/common/index" } ``` 然后在需要使用侧边导航栏滚动的页面中引入 Vant 的侧边导航栏组件,并按照需求设置好相关属性和事件,示例如下: ```html <van-sidebar show="{{ true }}" width="200rpx" :style="height: 100vh;"> <van-sidebar-item v-for="(item, index) in menuList" :key="index" :title="item.title" :dot="item.dot" :badge="item.badge" /> </van-sidebar> ``` 其中,`show` 属性用于控制侧边导航栏的显示与隐藏,`width` 属性用于设置导航栏的宽度,`:style` 属性用于设置导航栏的高度为可视区域的高度。 此外,需要根据实际需求在 `data` 数据中定义 `menuList` 数组,并为每个侧边导航栏项设置相应的标题、角标和小红点。 最后,在页面的生命周期函数中,通过监听滚动事件来实现侧边导航栏的滚动效果,示例如下: ```javascript Page({ onScroll: function(event) { // 根据滚动位置处理侧边导航栏的样式 } }) ``` 在监听滚动事件中,可以根据滚动位置来处理侧边导航栏的样式,例如高亮当前正在滚动的导航项。 总结起来,要在微信小程序中使用 Vant 组件库的侧边导航栏滚动,需要首先引入 Vant 组件库并进行配置,然后在页面中按照需求引入侧边导航栏组件并设置相应的属性和事件,最后在监听滚动事件中处理导航栏的样式即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值