share.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. function shareFn(shareInfo,drawList,callback){
  2. //以下为计算菜单的nview绘制布局,为固定算法,使用者无关关心
  3. let screenWidth = plus.screen.resolutionWidth
  4. //以360px宽度屏幕为例,上下左右边距及2排按钮边距留25像素,图标宽度55像素,同行图标间的间距在360宽的屏幕是30px,但需要动态计算,以此原则计算4列图标分别的left位置
  5. //图标下的按钮文字距离图标5像素,文字大小12像素
  6. //底部取消按钮高度固定为44px
  7. //TODO 未处理横屏和pad,这些情况6个图标应该一排即可
  8. let marginTop = 25,//上间距
  9. marginLeft=25,//左间距
  10. iconWidth = 55,//图标宽宽
  11. iconHeight=55,//图标高度
  12. icontextSpace = 10,//图标与文字间距
  13. textHeight = 12//文字大小
  14. let left1 = marginLeft / 360 * screenWidth;
  15. let colNumber=Math.floor(screenWidth/(iconWidth+marginLeft));
  16. let i=(screenWidth-(iconWidth+marginLeft)*colNumber-marginLeft)/(colNumber+1);
  17. let initMargin=marginLeft+i;
  18. let itemWidth=iconWidth+initMargin;
  19. let itemHeight=iconHeight+icontextSpace+textHeight+marginTop;
  20. let textTop=iconHeight+icontextSpace;
  21. let alphaBg = new plus.nativeObj.View("alphaBg", { //先创建遮罩层
  22. top: '0px',
  23. left: '0px',
  24. height: '100%',
  25. width: '100%',
  26. backgroundColor: 'rgba(0,0,0,0.5)'
  27. });
  28. alphaBg.addEventListener("click", function() { //处理遮罩层点击
  29. alphaBg.hide();
  30. shareMenu.hide();
  31. })
  32. let shareMenu = new plus.nativeObj.View("shareMenu", { //创建底部图标菜单
  33. bottom: '0px',
  34. left: '0px',
  35. height: Math.ceil(drawList.length/colNumber)*itemHeight+marginTop+44+1+'px',
  36. width: '100%',
  37. backgroundColor: 'rgb(255,255,255)'
  38. });
  39. //绘制底部图标菜单的内容
  40. shareMenu.draw([
  41. {
  42. tag: 'rect',//菜单顶部的分割灰线
  43. color: '#e7e7e7',
  44. position: {
  45. top: '0px',
  46. height: '1px'
  47. }
  48. },
  49. {
  50. tag: 'font',
  51. id: 'sharecancel',//底部取消按钮的文字
  52. text: '取消分享',
  53. textStyles: {
  54. size: '14px'
  55. },
  56. position: {
  57. bottom: '0px',
  58. height: '44px'
  59. }
  60. },
  61. {
  62. tag: 'rect',//底部取消按钮的顶部边线
  63. color: '#e7e7e7',
  64. position: {
  65. bottom: '45px',
  66. height: '1px'
  67. }
  68. }
  69. ]);
  70. drawList.map((v,k)=>{
  71. let time=new Date().getTime();
  72. let row=Math.floor(k/colNumber);
  73. let col=k%colNumber;
  74. let item=[{
  75. src:v.icon,
  76. id:Math.random()*1000+time,
  77. tag:"img",
  78. position:{
  79. top:row*itemHeight+marginTop,
  80. left:col*itemWidth+initMargin,
  81. width:iconWidth,
  82. height:iconWidth
  83. }
  84. },{
  85. text:v.text,
  86. id:Math.random()*1000+time,
  87. tag:"font",
  88. textStyles:{
  89. size: textHeight
  90. },
  91. position:{
  92. top:row*itemHeight+textTop,
  93. left:col*itemWidth+initMargin,
  94. width:iconWidth,
  95. height:iconWidth
  96. }
  97. }];
  98. shareMenu.draw(item);
  99. });
  100. shareMenu.addEventListener("click", function(e) { //处理底部图标菜单的点击事件,根据点击位置触发不同的逻辑
  101. console.log("dianji")
  102. if (e.screenY > plus.screen.resolutionHeight - 44) { //点击了底部取消按钮
  103. console.log("22222222")
  104. alphaBg.hide();
  105. shareMenu.hide();
  106. } else if (e.clientX < 5 || e.clientX > screenWidth - 5 || e.clientY < 5) {
  107. console.log("33333333333")
  108. //屏幕左右边缘5像素及菜单顶部5像素不处理点击
  109. } else { //点击了图标按钮
  110. console.log("444444444")
  111. let x=e.clientX;
  112. let y=e.clientY;
  113. let colIdx=Math.floor(x/itemWidth);
  114. let rowIdx=Math.floor(y/itemHeight);
  115. let tapIndex=colIdx+rowIdx*colNumber;
  116. callback&&callback(tapIndex);
  117. }
  118. })
  119. return {alphaBg,shareMenu};
  120. };
  121. export default shareFn;