场景:点击操作按钮,让弹出层悬浮到点击的位置

/* 返回值为新的相对位置 */
reposition(target, reference) { var docH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight, tRect = reference.getBoundingClientRect(), mRect = target.getBoundingClientRect(); /** * 根据相对视口的坐标偏移,计算新的相对位置 *(top偏移:rect的top的坐标偏移差,left偏移:rect的left坐标偏移差) **/ var tTop = tRect.top, tLeft = tRect.left, mTop = mRect.top, mLeft = mRect.left, top = target.offsetTop + (tTop - mTop) + tRect.height, left = target.offsetLeft + (tLeft - mLeft); var offsetD = tRect.y + tRect.height; if (docH - offsetD
< mRect.height) {top = top - mRect.height - tRect.height; } return { left: left, top: top }; }

 

相关文章:

  • 2021-08-21
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
猜你喜欢
  • 2022-12-23
  • 2021-10-16
  • 2021-05-23
  • 2021-11-03
  • 2022-12-23
  • 2021-09-20
相关资源
相似解决方案