uni-image-menu.js 5.0 KB

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