utils.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * 深度克隆
  3. * @param {*} data 需要克隆的数据
  4. */
  5. export function cloneDeep(data){
  6. let t = Object.prototype.toString.call(data)
  7. let o;
  8. if (t === '[object Array]') {
  9. o = [];
  10. } else if ( t === '[object Object]') {
  11. o = {};
  12. } else {
  13. return data;
  14. }
  15. if (t === '[object Array]') {
  16. for (let i = 0; i < data.length; i++) {
  17. o.push(cloneDeep(data[i]));
  18. }
  19. } else if ( t === '[object Object]') {
  20. for (let i in data) {
  21. o[i] = cloneDeep(data[i]);
  22. }
  23. }
  24. return o;
  25. }
  26. export function calcTipPosition (pDom, tDom, leftW, tipPlace ,border) {
  27. const { scrollHeight, scrollWidth } = document.body
  28. const {pageXOffset} = window
  29. let finalLeft, finalTop, finalBottom, tranigleLeft, tranigleTop, tranigleBottom
  30. let checkLeftDis = (tDom.width/2-leftW) > pDom.left //左边空间不够
  31. let checkRightDis = (tDom.width/2 -(pDom.width - leftW)) > (scrollWidth - pDom.right) // 右边空间不够
  32. let checkTop = pDom.top < (tDom.height + border) // 顶部空间不够
  33. let checkBottom = scrollHeight - pDom.bottom < tDom.height + border // 底部空间不够
  34. let bottomDis = scrollHeight - pDom.bottom + pDom.height + border // 设置底对齐
  35. let topDis = pDom.top + pDom.height + border // 设置顶对齐
  36. let traniglePos = -border*Math.cos(45)
  37. function checkTopBottom () {
  38. if (tipPlace === 'top') {
  39. if (checkTop) {
  40. finalTop = topDis
  41. tranigleTop = traniglePos
  42. } else {
  43. finalBottom = bottomDis
  44. tranigleBottom = traniglePos
  45. }
  46. } else {
  47. if (checkBottom) {
  48. finalBottom = bottomDis
  49. tranigleBottom = traniglePos
  50. } else {
  51. finalTop = topDis
  52. tranigleTop = traniglePos
  53. }
  54. }
  55. }
  56. // 特殊情况处理
  57. if (tDom.width > scrollWidth) { // 弹出框宽度大于窗口
  58. finalLeft = 0
  59. tranigleLeft = pDom.left+leftW + border
  60. checkTopBottom()
  61. } else if ((pDom.right > scrollWidth) && ((scrollWidth-pDom.left) < tDom.width)) { // 右边被遮住并且可见部分不足够展示
  62. finalLeft = scrollWidth - tDom.width
  63. tranigleLeft = (pDom.left - scrollWidth + tDom.width) + leftW - border
  64. checkTopBottom()
  65. } else {
  66. if (checkLeftDis) {
  67. finalLeft = pDom.left + pageXOffset
  68. tranigleLeft = leftW - border
  69. } else if (checkRightDis) {
  70. finalLeft = pDom.left - tDom.width + pDom.width + pageXOffset
  71. tranigleLeft = tDom.width - pDom.width + leftW - border
  72. } else{
  73. finalLeft = pDom.left - (tDom.width/2 - leftW) + pageXOffset
  74. tranigleLeft = tDom.width/2 - border
  75. }
  76. checkTopBottom()
  77. }
  78. return {finalLeft, finalTop, tranigleLeft, tranigleTop ,finalBottom, tranigleBottom}
  79. }