uni-image-menu.js 5.2 KB

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