bind-tip.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. var nvMask,nvImageMenu;
  2. export default {
  3. show({list,cancelText},callback){
  4. if(!list){
  5. list = [{
  6. "img":"https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-dc-site/9a952c80-6080-11eb-a16f-5b3e54966275.png",
  7. "text":"图标文字"
  8. }]
  9. }
  10. if(list.length>8){
  11. return uni.showToast({
  12. title: '目前版本最多支持8个图标',
  13. icon: 'none'
  14. });
  15. }
  16. //以下为计算菜单的nview绘制布局,为固定算法,使用者无关关心
  17. var screenWidth = plus.screen.resolutionWidth;
  18. var screenHeight = plus.screen.resolutionHeight;
  19. //以360px宽度屏幕为例,上下左右边距及2排按钮边距留25像素,图标宽度55像素,同行图标间的间距在360宽的屏幕是30px,但需要动态计算,以此原则计算4列图标分别的left位置
  20. //图标下的按钮文字距离图标5像素,文字大小12像素
  21. //底部取消按钮高度固定为44px
  22. //TODO 未处理横屏和pad,这些情况6个图标应该一排即可
  23. var margin = 20,
  24. iconWidth = 60,
  25. icontextSpace = 5,
  26. textHeight = 12
  27. var left1 = margin / 360 * screenWidth
  28. var iconSpace = (screenWidth - (left1 * 2) - (iconWidth * 4)) / 3 //屏幕宽度减去左右留白间距,再减去4个图标的宽度,就是3个同行图标的间距
  29. if (iconSpace <= 5) { //屏幕过窄时,缩小边距和图标大小,再算一次
  30. margin = 15
  31. iconWidth = 40
  32. left1 = margin / 360 * screenWidth
  33. iconSpace = (screenWidth - (left1 * 2) - (iconWidth * 4)) / 3 //屏幕宽度减去左右留白间距,再减去4个图标的宽度,就是3个同行图标的间距
  34. }
  35. var left2 = left1 + iconWidth + iconSpace
  36. var left3 = left1 + (iconWidth + iconSpace) * 2
  37. var left4 = left1 + (iconWidth + iconSpace) * 3
  38. var top1 = left1
  39. var top2 = top1 + iconWidth + icontextSpace + textHeight + left1
  40. nvMask = new plus.nativeObj.View("nvMask", { //先创建遮罩层
  41. top: '0px',
  42. left: '0px',
  43. height: '100%',
  44. width: '100%',
  45. backgroundColor: 'rgba(0,0,0,0.2)'
  46. });
  47. nvMask.addEventListener("click", function() { //处理遮罩层点击
  48. nvMask.hide();
  49. nvImageMenu.hide();
  50. })
  51. var menuHeight = screenHeight/2 - (iconWidth + textHeight + 2 * margin)*Math.ceil(list.length/4) +44;
  52. nvImageMenu = new plus.nativeObj.View("nvImageMenu", { //创建底部图标菜单
  53. top: menuHeight + 'px',
  54. left: '0px',
  55. height: (iconWidth + textHeight + 2 * margin)*Math.ceil(list.length/4) +44+'px',//'264px',
  56. width: '100%',
  57. backgroundColor: 'rgb(255,255,255)'
  58. });
  59. let myList = []
  60. list.forEach((item,i)=>{
  61. myList.push({
  62. tag: 'img',
  63. src: item.img,
  64. position: {
  65. top: eval('top'+( parseInt(i/4) +1)),
  66. left: eval('left'+(1+i%4)),
  67. width: iconWidth,
  68. height: iconWidth
  69. }
  70. })
  71. myList.push({
  72. tag: 'font',
  73. text: item.text,
  74. textStyles: {
  75. size: textHeight
  76. },
  77. position: {
  78. top: eval('top'+(parseInt(i/4)+1)) + iconWidth + icontextSpace,
  79. left: eval('left'+(1+i%4)),
  80. width: iconWidth,
  81. height: textHeight
  82. }
  83. })
  84. })
  85. //绘制底部图标菜单的内容
  86. nvImageMenu.draw([
  87. {
  88. tag: 'rect',//菜单顶部的分割灰线
  89. color: '#e7e7e7',
  90. position: {
  91. top: '0px',
  92. height: '1px'
  93. }
  94. },
  95. {
  96. tag: 'font',
  97. text: cancelText,//底部取消按钮的文字
  98. textStyles: {
  99. size: '14px'
  100. },
  101. position: {
  102. bottom: '0px',
  103. height: '44px'
  104. }
  105. },
  106. {
  107. tag: 'rect',//底部取消按钮的顶部边线
  108. color: '#e7e7e7',
  109. position: {
  110. bottom: '45px',
  111. height: '1px'
  112. }
  113. },
  114. ...myList
  115. ])
  116. nvMask.show()
  117. nvImageMenu.show() //5+应支持从底部向上弹出的动画
  118. nvImageMenu.addEventListener("click",e=>{ //处理底部图标菜单的点击事件,根据点击位置触发不同的逻辑
  119. // console.log("click menu"+JSON.stringify(e));
  120. if (e.screenY > plus.screen.resolutionHeight - 44) { //点击了底部取消按钮
  121. nvMask.hide();
  122. nvImageMenu.hide();
  123. } else if (e.clientX < 5 || e.clientX > screenWidth - 5 || e.clientY < 5) {
  124. //屏幕左右边缘5像素及菜单顶部5像素不处理点击
  125. } else { //点击了图标按钮
  126. var iClickIndex = -1 //点击的图标按钮序号,第一个图标按钮的index为0
  127. var iRow = e.clientY < (top2 - (left1 / 2)) ? 0 : 1
  128. var iCol = -1
  129. if (e.clientX < (left2 - (iconSpace / 2))) {
  130. iCol = 0
  131. } else if (e.clientX < (left3 - (iconSpace / 2))) {
  132. iCol = 1
  133. } else if (e.clientX < (left4 - (iconSpace / 2))) {
  134. iCol = 2
  135. } else {
  136. iCol = 3
  137. }
  138. if (iRow == 0) {
  139. iClickIndex = iCol
  140. } else {
  141. iClickIndex = iCol + 4
  142. }
  143. // console.log("点击按钮的序号: " + iClickIndex);
  144. // if (iClickIndex >= 0 && iClickIndex <= 5) { //处理具体的点击逻辑,此处也可以自行定义逻辑。如果增减了按钮,此处也需要跟着修改
  145. // }
  146. callback(iClickIndex)
  147. this.hide()
  148. }
  149. })
  150. /* nvImageMenu.addEventListener("touchstart", function(e) {
  151. if (e.screenY > (plus.screen.resolutionHeight - 44)) {
  152. //TODO 这里可以处理按下背景变灰的效果
  153. }
  154. })
  155. nvImageMenu.addEventListener("touchmove", function(e) {
  156. //TODO 这里可以处理按下背景变灰的效果
  157. if (e.screenY > plus.screen.resolutionHeight - 44) {}
  158. })
  159. nvImageMenu.addEventListener("touchend", function(e) {
  160. //TODO 这里可以处理释放背景恢复的效果
  161. })
  162. */
  163. },
  164. hide(){
  165. nvMask.hide()
  166. nvImageMenu.hide()
  167. }
  168. }