selectpopup.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // components/selectpopup/selectpopup.js
  2. //上次按钮点击时间
  3. var lasttime = 0;
  4. //按键防抖时间
  5. var clickTime = 200;
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. itemHeight: {
  12. type: Number,
  13. value:100
  14. },
  15. padding: {
  16. type: Number,
  17. value:15
  18. },
  19. radius: {
  20. type: Number,
  21. value:15
  22. },
  23. showExit: {
  24. type: Boolean,
  25. value:true
  26. },
  27. exitText: {
  28. type: String,
  29. value:"取消"
  30. },
  31. exitColor: {
  32. type: String,
  33. value:"#666666"
  34. },
  35. selectItemText: {
  36. type: Array,
  37. value: ["测试1", "测试2"]
  38. },
  39. selectItemTextColor: {
  40. type: Array,
  41. value: ["#576B95", "#576B95"],
  42. },
  43. outExit: {
  44. type: Boolean,
  45. value:false
  46. },
  47. mark: {
  48. type: String,
  49. value:"selectPopup"
  50. },
  51. },
  52. /**
  53. * 组件的初始数据
  54. */
  55. data: {
  56. //item高度,单位rpx
  57. itemHeight: 100,
  58. //padding
  59. padding: 15,
  60. //圆角
  61. radius: 15,
  62. //是否显示取消按钮
  63. showExit: true,
  64. //取消按钮文字
  65. exitText: "取消",
  66. //取消按钮文字颜色
  67. exitColor: "#666666",
  68. //item内容,数组
  69. selectItemText: ["测试1", "测试2"],
  70. //item文字颜色,数组,不设置默认#576B95
  71. selectItemTextColor: ["#576B95", "#ff0000"],
  72. popupSelectDisplay: "hidden",
  73. //点击空白区域是否取消显示
  74. outExit:false,
  75. //组件标识
  76. mark:"selectPopup",
  77. },
  78. /**
  79. * 组件的方法列表
  80. */
  81. methods: {
  82. click: function (e) {
  83. //获取点击时间
  84. let d = new Date();
  85. let nowtime = d.getTime();
  86. if (nowtime - lasttime > clickTime) {
  87. lasttime = nowtime;
  88. let index = parseInt(e.currentTarget.id.replace("list_", ""));
  89. console.log(index)
  90. this.closePopup('selectPopupItemClick', [ index,this.data.mark ])
  91. }
  92. },
  93. exitClick: function (e) {
  94. //获取点击时间
  95. let d = new Date();
  96. let nowtime = d.getTime();
  97. if (nowtime - lasttime > clickTime) {
  98. lasttime = nowtime;
  99. switch (e.currentTarget.id) {
  100. case "out":
  101. if(this.data.outExit){
  102. this.closePopup('selectPopupExit', [ -1,this.data.mark ])
  103. }
  104. break;
  105. case "cancel":
  106. this.closePopup('selectPopupExit', [ -1,this.data.mark ])
  107. break;
  108. }
  109. }
  110. },
  111. show(selectItemText,selectItemTextColor){
  112. var that = this
  113. that.setData({
  114. selectItemText: selectItemText,
  115. selectItemTextColor:selectItemTextColor,
  116. })
  117. this.openPopup()
  118. },
  119. //打开弹窗
  120. openPopup() {
  121. var query = this.createSelectorQuery()
  122. var that = this
  123. query.select('#popup_select_content').boundingClientRect(function (res) {
  124. var screenH = res.height;
  125. that.setData({
  126. popupSelectDisplay: "visible",
  127. })
  128. that.animation_select = wx.createAnimation({
  129. duration: 300,
  130. timingFunction: 'ease',
  131. delay: 0,
  132. transformOrigin: 'left top 0',
  133. })
  134. that.animation_select.translate(0, -screenH).step()
  135. that.setData({
  136. //输出动画
  137. animation_select: that.animation_select.export()
  138. })
  139. }).exec();
  140. },
  141. //关闭弹窗
  142. closePopup(event,detail) {
  143. //实例化一个动画
  144. var that = this
  145. that.animation_select = wx.createAnimation({
  146. // 动画持续时间,单位ms,默认值 400
  147. duration: 300,
  148. timingFunction: 'ease',
  149. // 延迟多长时间开始
  150. delay: 0,
  151. transformOrigin: 'left top 0',
  152. })
  153. that.animation_select.translate(0, 0).step()
  154. that.setData({
  155. //输出动画
  156. animation_select: that.animation_select.export()
  157. })
  158. setTimeout(function () {
  159. that.setData({
  160. popupSelectDisplay: "hidden",
  161. })
  162. this.triggerEvent(event,detail,{})
  163. }.bind(this), 300)
  164. }
  165. }
  166. })