当前位置:   article > 正文

小程序 使用动画将picker-view模拟类似picker的效果 picker也可能造成appLaunch with an already exist webviewId报错信息_applaunch with an already exist webviewid 98

applaunch with an already exist webviewid 98

在小程序开发中,picker在许多地方会用到,但是可能不会满足产品的要求,此时可以使用picker-view进行处理

js文件中使用createAnimation创建动画来做过度效果,当然也可以在css中加入动画,不过我就要用js  -。-

  1. // pages/myTimeSelect/myTimeSelect.js
  2. const date = new Date()
  3. const years = []
  4. const months = []
  5. const days = []
  6. var self
  7. for (let i = 1990; i <= date.getFullYear(); i++) {
  8. years.push(i)
  9. }
  10. for (let i = 1; i <= 12; i++) {
  11. months.push(i)
  12. }
  13. for (let i = 1; i <= 31; i++) {
  14. days.push(i)
  15. }
  16. Page({
  17. /**
  18. * 页面的初始数据
  19. */
  20. data: {
  21. years: years,
  22. year: date.getFullYear(),
  23. months: months,
  24. month: 2,
  25. days: days,
  26. day: 2,
  27. value: [9999, 1, 1],
  28. myTimeChoose: false,
  29. myTimeChooseViewAnimationData: {},
  30. myTimePickerViewMaskViewAnimationData: {},
  31. pickerViewHeight: 300,
  32. pickerViewTime: 300,
  33. },
  34. bindChange: function (e) {
  35. const val = e.detail.value
  36. this.setData({
  37. year: this.data.years[val[0]],
  38. month: this.data.months[val[1]],
  39. day: this.data.days[val[2]]
  40. })
  41. },
  42. chooseSezi: function (e) {
  43. // 创建一个动画实例
  44. var myTimeChooseViewAnimation = wx.createAnimation({
  45. // 动画持续时间
  46. duration: self.data.pickerViewTime,
  47. // 定义动画效果,当前是匀速
  48. timingFunction: 'ease'
  49. })
  50. // 先在y轴偏移,然后用step()完成一个动画,先向下动画pickerView个高度
  51. myTimeChooseViewAnimation.translateY(self.data.pickerViewHeight).step()
  52. var myTimePickerViewMaskViewAnimation = wx.createAnimation({
  53. duration: self.data.pickerViewTime,
  54. timingFunction: 'ease'
  55. })
  56. myTimePickerViewMaskViewAnimation.opacity(0).step()
  57. // 用setData改变当前动画
  58. self.setData({
  59. // 通过export()方法导出数据
  60. myTimeChooseViewAnimationData: myTimeChooseViewAnimation.export(),
  61. myTimePickerViewMaskViewAnimationData: myTimePickerViewMaskViewAnimation.export(),
  62. // 改变view里面的Wx:if
  63. myTimeChoose: true
  64. })
  65. // 再还原到之前位置
  66. myTimeChooseViewAnimation.translateY(0.2).step()
  67. myTimePickerViewMaskViewAnimation.opacity(1).step()
  68. self.setData({
  69. myTimeChooseViewAnimationData: myTimeChooseViewAnimation.export(),
  70. myTimePickerViewMaskViewAnimationData: myTimePickerViewMaskViewAnimation.export()
  71. })
  72. },
  73. myTimePickerViewMaskViewTap: function (e) {
  74. var myTimePickerViewMaskViewAnimation = wx.createAnimation({
  75. duration: self.data.pickerViewTime,
  76. timingFunction: 'ease'
  77. })
  78. myTimePickerViewMaskViewAnimation.opacity(-0.8).step()
  79. var myTimeChooseViewAnimation = wx.createAnimation({
  80. duration: self.data.pickerViewTime,
  81. timingFunction: 'ease'
  82. })
  83. myTimeChooseViewAnimation.translateY(0).step()
  84. self.setData({
  85. myTimeChooseViewAnimationData: myTimeChooseViewAnimation.export(),
  86. myTimePickerViewMaskViewAnimationData: myTimePickerViewMaskViewAnimation.export(),
  87. myTimeChoose: false
  88. })
  89. },
  90. /**
  91. * 生命周期函数--监听页面加载
  92. */
  93. onLoad: function (options) {
  94. self = this;
  95. },
  96. })

wxss文件

  1. <!--pages/myTimeSelect/myTimeSelect.wxml-->
  2. <view class='text' bindtap='chooseSezi'>请选择时间</view>
  3. <view class='myTimePickerViewMaskView' wx:if="{{myTimeChoose}}" bindtap='myTimePickerViewMaskViewTap' animation='{{myTimePickerViewMaskViewAnimationData}}'></view>
  4. <view class='myTimeChooseView' wx:if="{{myTimeChoose}}" animation='{{myTimeChooseViewAnimationData}}' style='height: {{pickerViewHeight}};'>
  5. <view>{{year}}年{{month}}月{{day}}日</view>
  6. <picker-view indicator-style="height: 50px;" style="width: 100%; height: 300px;" value="{{value}}" bindchange="bindChange">
  7. <picker-view-column>
  8. <view wx:for="{{years}}" wx:key='year' style="line-height: 50px" class='intro'>{{item}}年</view>
  9. </picker-view-column>
  10. <picker-view-column>
  11. <view wx:for="{{months}}" wx:key='month' style="line-height: 50px" class='intro'>{{item}}月</view>
  12. </picker-view-column>
  13. <picker-view-column>
  14. <view wx:for="{{days}}" wx:key='day' style="line-height: 50px" class='intro'>{{item}}日</view>
  15. </picker-view-column>
  16. </picker-view>
  17. </view>

css

  1. .intro {
  2. text-align: center;
  3. }
  4. .myTimePickerViewMaskView {
  5. position: absolute;
  6. top: 0;
  7. width: 100%;
  8. height: 100%;
  9. background-color: rgba(0, 0, 0, 0.5);
  10. }
  11. .myTimeChooseView {
  12. position: absolute;
  13. width: 100%;
  14. bottom: 0;
  15. background-color: #fff;
  16. }

本文为小程序的一个简单页面,可以简单了解js动画的实现 QAQ

另外小程序在启动时报错 appLaunch with an already exist webviewId 加上个数字,可能是因为在首页中有picker未设置默认值导致的,有此问题的小伙伴可以尝试添加value值试试

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/80365
推荐阅读
  

闽ICP备14008679号