util.js 927 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. function getScroll(w) {
  2. let ret = w.scrollX;
  3. const method = 'scrollLeft';
  4. if (typeof ret !== 'number') {
  5. const d = w.document;
  6. // ie6,7,8 standard mode
  7. ret = d.documentElement[method];
  8. if (typeof ret !== 'number') {
  9. // quirks mode
  10. ret = d.body[method];
  11. }
  12. }
  13. return ret;
  14. }
  15. function getClientPosition(elem) {
  16. let x;
  17. let y;
  18. const doc = elem.ownerDocument;
  19. const {
  20. body
  21. } = doc;
  22. const docElem = doc && doc.documentElement;
  23. const box = elem.getBoundingClientRect();
  24. x = box.left;
  25. y = box.top;
  26. x -= docElem.clientLeft || body.clientLeft || 0;
  27. y -= docElem.clientTop || body.clientTop || 0;
  28. return {
  29. left: x,
  30. top: y
  31. };
  32. }
  33. export function getOffsetLeft(el) {
  34. const pos = getClientPosition(el);
  35. const doc = el.ownerDocument;
  36. // Only IE use `parentWindow`
  37. const w = doc.defaultView || doc.parentWindow;
  38. pos.left += getScroll(w);
  39. return pos.left;
  40. }