Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. function _typeof(obj) {
  2. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  3. _typeof = function (obj) {
  4. return typeof obj;
  5. };
  6. } else {
  7. _typeof = function (obj) {
  8. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  9. };
  10. }
  11. return _typeof(obj);
  12. }
  13. function _defineProperty(obj, key, value) {
  14. if (key in obj) {
  15. Object.defineProperty(obj, key, {
  16. value: value,
  17. enumerable: true,
  18. configurable: true,
  19. writable: true
  20. });
  21. } else {
  22. obj[key] = value;
  23. }
  24. return obj;
  25. }
  26. function ownKeys(object, enumerableOnly) {
  27. var keys = Object.keys(object);
  28. if (Object.getOwnPropertySymbols) {
  29. var symbols = Object.getOwnPropertySymbols(object);
  30. if (enumerableOnly) symbols = symbols.filter(function (sym) {
  31. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  32. });
  33. keys.push.apply(keys, symbols);
  34. }
  35. return keys;
  36. }
  37. function _objectSpread2(target) {
  38. for (var i = 1; i < arguments.length; i++) {
  39. var source = arguments[i] != null ? arguments[i] : {};
  40. if (i % 2) {
  41. ownKeys(source, true).forEach(function (key) {
  42. _defineProperty(target, key, source[key]);
  43. });
  44. } else if (Object.getOwnPropertyDescriptors) {
  45. Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
  46. } else {
  47. ownKeys(source).forEach(function (key) {
  48. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  49. });
  50. }
  51. }
  52. return target;
  53. }
  54. var vendorPrefix;
  55. var jsCssMap = {
  56. Webkit: '-webkit-',
  57. Moz: '-moz-',
  58. // IE did it wrong again ...
  59. ms: '-ms-',
  60. O: '-o-'
  61. };
  62. function getVendorPrefix() {
  63. if (vendorPrefix !== undefined) {
  64. return vendorPrefix;
  65. }
  66. vendorPrefix = '';
  67. var style = document.createElement('p').style;
  68. var testProp = 'Transform';
  69. for (var key in jsCssMap) {
  70. if (key + testProp in style) {
  71. vendorPrefix = key;
  72. }
  73. }
  74. return vendorPrefix;
  75. }
  76. function getTransitionName() {
  77. return getVendorPrefix() ? "".concat(getVendorPrefix(), "TransitionProperty") : 'transitionProperty';
  78. }
  79. function getTransformName() {
  80. return getVendorPrefix() ? "".concat(getVendorPrefix(), "Transform") : 'transform';
  81. }
  82. function setTransitionProperty(node, value) {
  83. var name = getTransitionName();
  84. if (name) {
  85. node.style[name] = value;
  86. if (name !== 'transitionProperty') {
  87. node.style.transitionProperty = value;
  88. }
  89. }
  90. }
  91. function setTransform(node, value) {
  92. var name = getTransformName();
  93. if (name) {
  94. node.style[name] = value;
  95. if (name !== 'transform') {
  96. node.style.transform = value;
  97. }
  98. }
  99. }
  100. function getTransitionProperty(node) {
  101. return node.style.transitionProperty || node.style[getTransitionName()];
  102. }
  103. function getTransformXY(node) {
  104. var style = window.getComputedStyle(node, null);
  105. var transform = style.getPropertyValue('transform') || style.getPropertyValue(getTransformName());
  106. if (transform && transform !== 'none') {
  107. var matrix = transform.replace(/[^0-9\-.,]/g, '').split(',');
  108. return {
  109. x: parseFloat(matrix[12] || matrix[4], 0),
  110. y: parseFloat(matrix[13] || matrix[5], 0)
  111. };
  112. }
  113. return {
  114. x: 0,
  115. y: 0
  116. };
  117. }
  118. var matrix2d = /matrix\((.*)\)/;
  119. var matrix3d = /matrix3d\((.*)\)/;
  120. function setTransformXY(node, xy) {
  121. var style = window.getComputedStyle(node, null);
  122. var transform = style.getPropertyValue('transform') || style.getPropertyValue(getTransformName());
  123. if (transform && transform !== 'none') {
  124. var arr;
  125. var match2d = transform.match(matrix2d);
  126. if (match2d) {
  127. match2d = match2d[1];
  128. arr = match2d.split(',').map(function (item) {
  129. return parseFloat(item, 10);
  130. });
  131. arr[4] = xy.x;
  132. arr[5] = xy.y;
  133. setTransform(node, "matrix(".concat(arr.join(','), ")"));
  134. } else {
  135. var match3d = transform.match(matrix3d)[1];
  136. arr = match3d.split(',').map(function (item) {
  137. return parseFloat(item, 10);
  138. });
  139. arr[12] = xy.x;
  140. arr[13] = xy.y;
  141. setTransform(node, "matrix3d(".concat(arr.join(','), ")"));
  142. }
  143. } else {
  144. setTransform(node, "translateX(".concat(xy.x, "px) translateY(").concat(xy.y, "px) translateZ(0)"));
  145. }
  146. }
  147. var RE_NUM = /[\-+]?(?:\d*\.|)\d+(?:[eE][\-+]?\d+|)/.source;
  148. var getComputedStyleX; // https://stackoverflow.com/a/3485654/3040605
  149. function forceRelayout(elem) {
  150. var originalStyle = elem.style.display;
  151. elem.style.display = 'none';
  152. elem.offsetHeight; // eslint-disable-line
  153. elem.style.display = originalStyle;
  154. }
  155. function css(el, name, v) {
  156. var value = v;
  157. if (_typeof(name) === 'object') {
  158. for (var i in name) {
  159. if (name.hasOwnProperty(i)) {
  160. css(el, i, name[i]);
  161. }
  162. }
  163. return undefined;
  164. }
  165. if (typeof value !== 'undefined') {
  166. if (typeof value === 'number') {
  167. value = "".concat(value, "px");
  168. }
  169. el.style[name] = value;
  170. return undefined;
  171. }
  172. return getComputedStyleX(el, name);
  173. }
  174. function getClientPosition(elem) {
  175. var box;
  176. var x;
  177. var y;
  178. var doc = elem.ownerDocument;
  179. var body = doc.body;
  180. var docElem = doc && doc.documentElement; // 根据 GBS 最新数据,A-Grade Browsers 都已支持 getBoundingClientRect 方法,不用再考虑传统的实现方式
  181. box = elem.getBoundingClientRect(); // 注:jQuery 还考虑减去 docElem.clientLeft/clientTop
  182. // 但测试发现,这样反而会导致当 html 和 body 有边距/边框样式时,获取的值不正确
  183. // 此外,ie6 会忽略 html 的 margin 值,幸运地是没有谁会去设置 html 的 margin
  184. x = box.left;
  185. y = box.top; // In IE, most of the time, 2 extra pixels are added to the top and left
  186. // due to the implicit 2-pixel inset border. In IE6/7 quirks mode and
  187. // IE6 standards mode, this border can be overridden by setting the
  188. // document element's border to zero -- thus, we cannot rely on the
  189. // offset always being 2 pixels.
  190. // In quirks mode, the offset can be determined by querying the body's
  191. // clientLeft/clientTop, but in standards mode, it is found by querying
  192. // the document element's clientLeft/clientTop. Since we already called
  193. // getClientBoundingRect we have already forced a reflow, so it is not
  194. // too expensive just to query them all.
  195. // ie 下应该减去窗口的边框吧,毕竟默认 absolute 都是相对窗口定位的
  196. // 窗口边框标准是设 documentElement ,quirks 时设置 body
  197. // 最好禁止在 body 和 html 上边框 ,但 ie < 9 html 默认有 2px ,减去
  198. // 但是非 ie 不可能设置窗口边框,body html 也不是窗口 ,ie 可以通过 html,body 设置
  199. // 标准 ie 下 docElem.clientTop 就是 border-top
  200. // ie7 html 即窗口边框改变不了。永远为 2
  201. // 但标准 firefox/chrome/ie9 下 docElem.clientTop 是窗口边框,即使设了 border-top 也为 0
  202. x -= docElem.clientLeft || body.clientLeft || 0;
  203. y -= docElem.clientTop || body.clientTop || 0;
  204. return {
  205. left: x,
  206. top: y
  207. };
  208. }
  209. function getScroll(w, top) {
  210. var ret = w["page".concat(top ? 'Y' : 'X', "Offset")];
  211. var method = "scroll".concat(top ? 'Top' : 'Left');
  212. if (typeof ret !== 'number') {
  213. var d = w.document; // ie6,7,8 standard mode
  214. ret = d.documentElement[method];
  215. if (typeof ret !== 'number') {
  216. // quirks mode
  217. ret = d.body[method];
  218. }
  219. }
  220. return ret;
  221. }
  222. function getScrollLeft(w) {
  223. return getScroll(w);
  224. }
  225. function getScrollTop(w) {
  226. return getScroll(w, true);
  227. }
  228. function getOffset(el) {
  229. var pos = getClientPosition(el);
  230. var doc = el.ownerDocument;
  231. var w = doc.defaultView || doc.parentWindow;
  232. pos.left += getScrollLeft(w);
  233. pos.top += getScrollTop(w);
  234. return pos;
  235. }
  236. /**
  237. * A crude way of determining if an object is a window
  238. * @member util
  239. */
  240. function isWindow(obj) {
  241. // must use == for ie8
  242. /* eslint eqeqeq:0 */
  243. return obj !== null && obj !== undefined && obj == obj.window;
  244. }
  245. function getDocument(node) {
  246. if (isWindow(node)) {
  247. return node.document;
  248. }
  249. if (node.nodeType === 9) {
  250. return node;
  251. }
  252. return node.ownerDocument;
  253. }
  254. function _getComputedStyle(elem, name, cs) {
  255. var computedStyle = cs;
  256. var val = '';
  257. var d = getDocument(elem);
  258. computedStyle = computedStyle || d.defaultView.getComputedStyle(elem, null); // https://github.com/kissyteam/kissy/issues/61
  259. if (computedStyle) {
  260. val = computedStyle.getPropertyValue(name) || computedStyle[name];
  261. }
  262. return val;
  263. }
  264. var _RE_NUM_NO_PX = new RegExp("^(".concat(RE_NUM, ")(?!px)[a-z%]+$"), 'i');
  265. var RE_POS = /^(top|right|bottom|left)$/;
  266. var CURRENT_STYLE = 'currentStyle';
  267. var RUNTIME_STYLE = 'runtimeStyle';
  268. var LEFT = 'left';
  269. var PX = 'px';
  270. function _getComputedStyleIE(elem, name) {
  271. // currentStyle maybe null
  272. // http://msdn.microsoft.com/en-us/library/ms535231.aspx
  273. var ret = elem[CURRENT_STYLE] && elem[CURRENT_STYLE][name]; // 当 width/height 设置为百分比时,通过 pixelLeft 方式转换的 width/height 值
  274. // 一开始就处理了! CUSTOM_STYLE.height,CUSTOM_STYLE.width ,cssHook 解决@2011-08-19
  275. // 在 ie 下不对,需要直接用 offset 方式
  276. // borderWidth 等值也有问题,但考虑到 borderWidth 设为百分比的概率很小,这里就不考虑了
  277. // From the awesome hack by Dean Edwards
  278. // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
  279. // If we're not dealing with a regular pixel number
  280. // but a number that has a weird ending, we need to convert it to pixels
  281. // exclude left right for relativity
  282. if (_RE_NUM_NO_PX.test(ret) && !RE_POS.test(name)) {
  283. // Remember the original values
  284. var style = elem.style;
  285. var left = style[LEFT];
  286. var rsLeft = elem[RUNTIME_STYLE][LEFT]; // prevent flashing of content
  287. elem[RUNTIME_STYLE][LEFT] = elem[CURRENT_STYLE][LEFT]; // Put in the new values to get a computed value out
  288. style[LEFT] = name === 'fontSize' ? '1em' : ret || 0;
  289. ret = style.pixelLeft + PX; // Revert the changed values
  290. style[LEFT] = left;
  291. elem[RUNTIME_STYLE][LEFT] = rsLeft;
  292. }
  293. return ret === '' ? 'auto' : ret;
  294. }
  295. if (typeof window !== 'undefined') {
  296. getComputedStyleX = window.getComputedStyle ? _getComputedStyle : _getComputedStyleIE;
  297. }
  298. function getOffsetDirection(dir, option) {
  299. if (dir === 'left') {
  300. return option.useCssRight ? 'right' : dir;
  301. }
  302. return option.useCssBottom ? 'bottom' : dir;
  303. }
  304. function oppositeOffsetDirection(dir) {
  305. if (dir === 'left') {
  306. return 'right';
  307. } else if (dir === 'right') {
  308. return 'left';
  309. } else if (dir === 'top') {
  310. return 'bottom';
  311. } else if (dir === 'bottom') {
  312. return 'top';
  313. }
  314. } // 设置 elem 相对 elem.ownerDocument 的坐标
  315. function setLeftTop(elem, offset, option) {
  316. // set position first, in-case top/left are set even on static elem
  317. if (css(elem, 'position') === 'static') {
  318. elem.style.position = 'relative';
  319. }
  320. var presetH = -999;
  321. var presetV = -999;
  322. var horizontalProperty = getOffsetDirection('left', option);
  323. var verticalProperty = getOffsetDirection('top', option);
  324. var oppositeHorizontalProperty = oppositeOffsetDirection(horizontalProperty);
  325. var oppositeVerticalProperty = oppositeOffsetDirection(verticalProperty);
  326. if (horizontalProperty !== 'left') {
  327. presetH = 999;
  328. }
  329. if (verticalProperty !== 'top') {
  330. presetV = 999;
  331. }
  332. var originalTransition = '';
  333. var originalOffset = getOffset(elem);
  334. if ('left' in offset || 'top' in offset) {
  335. originalTransition = getTransitionProperty(elem) || '';
  336. setTransitionProperty(elem, 'none');
  337. }
  338. if ('left' in offset) {
  339. elem.style[oppositeHorizontalProperty] = '';
  340. elem.style[horizontalProperty] = "".concat(presetH, "px");
  341. }
  342. if ('top' in offset) {
  343. elem.style[oppositeVerticalProperty] = '';
  344. elem.style[verticalProperty] = "".concat(presetV, "px");
  345. } // force relayout
  346. forceRelayout(elem);
  347. var old = getOffset(elem);
  348. var originalStyle = {};
  349. for (var key in offset) {
  350. if (offset.hasOwnProperty(key)) {
  351. var dir = getOffsetDirection(key, option);
  352. var preset = key === 'left' ? presetH : presetV;
  353. var off = originalOffset[key] - old[key];
  354. if (dir === key) {
  355. originalStyle[dir] = preset + off;
  356. } else {
  357. originalStyle[dir] = preset - off;
  358. }
  359. }
  360. }
  361. css(elem, originalStyle); // force relayout
  362. forceRelayout(elem);
  363. if ('left' in offset || 'top' in offset) {
  364. setTransitionProperty(elem, originalTransition);
  365. }
  366. var ret = {};
  367. for (var _key in offset) {
  368. if (offset.hasOwnProperty(_key)) {
  369. var _dir = getOffsetDirection(_key, option);
  370. var _off = offset[_key] - originalOffset[_key];
  371. if (_key === _dir) {
  372. ret[_dir] = originalStyle[_dir] + _off;
  373. } else {
  374. ret[_dir] = originalStyle[_dir] - _off;
  375. }
  376. }
  377. }
  378. css(elem, ret);
  379. }
  380. function setTransform$1(elem, offset) {
  381. var originalOffset = getOffset(elem);
  382. var originalXY = getTransformXY(elem);
  383. var resultXY = {
  384. x: originalXY.x,
  385. y: originalXY.y
  386. };
  387. if ('left' in offset) {
  388. resultXY.x = originalXY.x + offset.left - originalOffset.left;
  389. }
  390. if ('top' in offset) {
  391. resultXY.y = originalXY.y + offset.top - originalOffset.top;
  392. }
  393. setTransformXY(elem, resultXY);
  394. }
  395. function setOffset(elem, offset, option) {
  396. if (option.ignoreShake) {
  397. var oriOffset = getOffset(elem);
  398. var oLeft = oriOffset.left.toFixed(0);
  399. var oTop = oriOffset.top.toFixed(0);
  400. var tLeft = offset.left.toFixed(0);
  401. var tTop = offset.top.toFixed(0);
  402. if (oLeft === tLeft && oTop === tTop) {
  403. return;
  404. }
  405. }
  406. if (option.useCssRight || option.useCssBottom) {
  407. setLeftTop(elem, offset, option);
  408. } else if (option.useCssTransform && getTransformName() in document.body.style) {
  409. setTransform$1(elem, offset);
  410. } else {
  411. setLeftTop(elem, offset, option);
  412. }
  413. }
  414. function each(arr, fn) {
  415. for (var i = 0; i < arr.length; i++) {
  416. fn(arr[i]);
  417. }
  418. }
  419. function isBorderBoxFn(elem) {
  420. return getComputedStyleX(elem, 'boxSizing') === 'border-box';
  421. }
  422. var BOX_MODELS = ['margin', 'border', 'padding'];
  423. var CONTENT_INDEX = -1;
  424. var PADDING_INDEX = 2;
  425. var BORDER_INDEX = 1;
  426. var MARGIN_INDEX = 0;
  427. function swap(elem, options, callback) {
  428. var old = {};
  429. var style = elem.style;
  430. var name; // Remember the old values, and insert the new ones
  431. for (name in options) {
  432. if (options.hasOwnProperty(name)) {
  433. old[name] = style[name];
  434. style[name] = options[name];
  435. }
  436. }
  437. callback.call(elem); // Revert the old values
  438. for (name in options) {
  439. if (options.hasOwnProperty(name)) {
  440. style[name] = old[name];
  441. }
  442. }
  443. }
  444. function getPBMWidth(elem, props, which) {
  445. var value = 0;
  446. var prop;
  447. var j;
  448. var i;
  449. for (j = 0; j < props.length; j++) {
  450. prop = props[j];
  451. if (prop) {
  452. for (i = 0; i < which.length; i++) {
  453. var cssProp = void 0;
  454. if (prop === 'border') {
  455. cssProp = "".concat(prop).concat(which[i], "Width");
  456. } else {
  457. cssProp = prop + which[i];
  458. }
  459. value += parseFloat(getComputedStyleX(elem, cssProp)) || 0;
  460. }
  461. }
  462. }
  463. return value;
  464. }
  465. var domUtils = {
  466. getParent: function getParent(element) {
  467. var parent = element;
  468. do {
  469. if (parent.nodeType === 11 && parent.host) {
  470. parent = parent.host;
  471. } else {
  472. parent = parent.parentNode;
  473. }
  474. } while (parent && parent.nodeType !== 1 && parent.nodeType !== 9);
  475. return parent;
  476. }
  477. };
  478. each(['Width', 'Height'], function (name) {
  479. domUtils["doc".concat(name)] = function (refWin) {
  480. var d = refWin.document;
  481. return Math.max( // firefox chrome documentElement.scrollHeight< body.scrollHeight
  482. // ie standard mode : documentElement.scrollHeight> body.scrollHeight
  483. d.documentElement["scroll".concat(name)], // quirks : documentElement.scrollHeight 最大等于可视窗口多一点?
  484. d.body["scroll".concat(name)], domUtils["viewport".concat(name)](d));
  485. };
  486. domUtils["viewport".concat(name)] = function (win) {
  487. // pc browser includes scrollbar in window.innerWidth
  488. var prop = "client".concat(name);
  489. var doc = win.document;
  490. var body = doc.body;
  491. var documentElement = doc.documentElement;
  492. var documentElementProp = documentElement[prop]; // 标准模式取 documentElement
  493. // backcompat 取 body
  494. return doc.compatMode === 'CSS1Compat' && documentElementProp || body && body[prop] || documentElementProp;
  495. };
  496. });
  497. /*
  498. 得到元素的大小信息
  499. @param elem
  500. @param name
  501. @param {String} [extra] 'padding' : (css width) + padding
  502. 'border' : (css width) + padding + border
  503. 'margin' : (css width) + padding + border + margin
  504. */
  505. function getWH(elem, name, ex) {
  506. var extra = ex;
  507. if (isWindow(elem)) {
  508. return name === 'width' ? domUtils.viewportWidth(elem) : domUtils.viewportHeight(elem);
  509. } else if (elem.nodeType === 9) {
  510. return name === 'width' ? domUtils.docWidth(elem) : domUtils.docHeight(elem);
  511. }
  512. var which = name === 'width' ? ['Left', 'Right'] : ['Top', 'Bottom'];
  513. var borderBoxValue = name === 'width' ? elem.getBoundingClientRect().width : elem.getBoundingClientRect().height;
  514. var computedStyle = getComputedStyleX(elem);
  515. var isBorderBox = isBorderBoxFn(elem);
  516. var cssBoxValue = 0;
  517. if (borderBoxValue === null || borderBoxValue === undefined || borderBoxValue <= 0) {
  518. borderBoxValue = undefined; // Fall back to computed then un computed css if necessary
  519. cssBoxValue = getComputedStyleX(elem, name);
  520. if (cssBoxValue === null || cssBoxValue === undefined || Number(cssBoxValue) < 0) {
  521. cssBoxValue = elem.style[name] || 0;
  522. } // Normalize '', auto, and prepare for extra
  523. cssBoxValue = parseFloat(cssBoxValue) || 0;
  524. }
  525. if (extra === undefined) {
  526. extra = isBorderBox ? BORDER_INDEX : CONTENT_INDEX;
  527. }
  528. var borderBoxValueOrIsBorderBox = borderBoxValue !== undefined || isBorderBox;
  529. var val = borderBoxValue || cssBoxValue;
  530. if (extra === CONTENT_INDEX) {
  531. if (borderBoxValueOrIsBorderBox) {
  532. return val - getPBMWidth(elem, ['border', 'padding'], which);
  533. }
  534. return cssBoxValue;
  535. } else if (borderBoxValueOrIsBorderBox) {
  536. if (extra === BORDER_INDEX) {
  537. return val;
  538. }
  539. return val + (extra === PADDING_INDEX ? -getPBMWidth(elem, ['border'], which) : getPBMWidth(elem, ['margin'], which));
  540. }
  541. return cssBoxValue + getPBMWidth(elem, BOX_MODELS.slice(extra), which);
  542. }
  543. var cssShow = {
  544. position: 'absolute',
  545. visibility: 'hidden',
  546. display: 'block'
  547. }; // fix #119 : https://github.com/kissyteam/kissy/issues/119
  548. function getWHIgnoreDisplay() {
  549. for (var _len = arguments.length, args = new Array(_len), _key2 = 0; _key2 < _len; _key2++) {
  550. args[_key2] = arguments[_key2];
  551. }
  552. var val;
  553. var elem = args[0]; // in case elem is window
  554. // elem.offsetWidth === undefined
  555. if (elem.offsetWidth !== 0) {
  556. val = getWH.apply(undefined, args);
  557. } else {
  558. swap(elem, cssShow, function () {
  559. val = getWH.apply(undefined, args);
  560. });
  561. }
  562. return val;
  563. }
  564. each(['width', 'height'], function (name) {
  565. var first = name.charAt(0).toUpperCase() + name.slice(1);
  566. domUtils["outer".concat(first)] = function (el, includeMargin) {
  567. return el && getWHIgnoreDisplay(el, name, includeMargin ? MARGIN_INDEX : BORDER_INDEX);
  568. };
  569. var which = name === 'width' ? ['Left', 'Right'] : ['Top', 'Bottom'];
  570. domUtils[name] = function (elem, v) {
  571. var val = v;
  572. if (val !== undefined) {
  573. if (elem) {
  574. var computedStyle = getComputedStyleX(elem);
  575. var isBorderBox = isBorderBoxFn(elem);
  576. if (isBorderBox) {
  577. val += getPBMWidth(elem, ['padding', 'border'], which);
  578. }
  579. return css(elem, name, val);
  580. }
  581. return undefined;
  582. }
  583. return elem && getWHIgnoreDisplay(elem, name, CONTENT_INDEX);
  584. };
  585. });
  586. function mix(to, from) {
  587. for (var i in from) {
  588. if (from.hasOwnProperty(i)) {
  589. to[i] = from[i];
  590. }
  591. }
  592. return to;
  593. }
  594. var utils = {
  595. getWindow: function getWindow(node) {
  596. if (node && node.document && node.setTimeout) {
  597. return node;
  598. }
  599. var doc = node.ownerDocument || node;
  600. return doc.defaultView || doc.parentWindow;
  601. },
  602. getDocument: getDocument,
  603. offset: function offset(el, value, option) {
  604. if (typeof value !== 'undefined') {
  605. setOffset(el, value, option || {});
  606. } else {
  607. return getOffset(el);
  608. }
  609. },
  610. isWindow: isWindow,
  611. each: each,
  612. css: css,
  613. clone: function clone(obj) {
  614. var i;
  615. var ret = {};
  616. for (i in obj) {
  617. if (obj.hasOwnProperty(i)) {
  618. ret[i] = obj[i];
  619. }
  620. }
  621. var overflow = obj.overflow;
  622. if (overflow) {
  623. for (i in obj) {
  624. if (obj.hasOwnProperty(i)) {
  625. ret.overflow[i] = obj.overflow[i];
  626. }
  627. }
  628. }
  629. return ret;
  630. },
  631. mix: mix,
  632. getWindowScrollLeft: function getWindowScrollLeft(w) {
  633. return getScrollLeft(w);
  634. },
  635. getWindowScrollTop: function getWindowScrollTop(w) {
  636. return getScrollTop(w);
  637. },
  638. merge: function merge() {
  639. var ret = {};
  640. for (var i = 0; i < arguments.length; i++) {
  641. utils.mix(ret, i < 0 || arguments.length <= i ? undefined : arguments[i]);
  642. }
  643. return ret;
  644. },
  645. viewportWidth: 0,
  646. viewportHeight: 0
  647. };
  648. mix(utils, domUtils);
  649. /**
  650. * 得到会导致元素显示不全的祖先元素
  651. */
  652. var getParent = utils.getParent;
  653. function getOffsetParent(element) {
  654. if (utils.isWindow(element) || element.nodeType === 9) {
  655. return null;
  656. } // ie 这个也不是完全可行
  657. /*
  658. <div style="width: 50px;height: 100px;overflow: hidden">
  659. <div style="width: 50px;height: 100px;position: relative;" id="d6">
  660. 元素 6 高 100px 宽 50px<br/>
  661. </div>
  662. </div>
  663. */
  664. // element.offsetParent does the right thing in ie7 and below. Return parent with layout!
  665. // In other browsers it only includes elements with position absolute, relative or
  666. // fixed, not elements with overflow set to auto or scroll.
  667. // if (UA.ie && ieMode < 8) {
  668. // return element.offsetParent;
  669. // }
  670. // 统一的 offsetParent 方法
  671. var doc = utils.getDocument(element);
  672. var body = doc.body;
  673. var parent;
  674. var positionStyle = utils.css(element, 'position');
  675. var skipStatic = positionStyle === 'fixed' || positionStyle === 'absolute';
  676. if (!skipStatic) {
  677. return element.nodeName.toLowerCase() === 'html' ? null : getParent(element);
  678. }
  679. for (parent = getParent(element); parent && parent !== body && parent.nodeType !== 9; parent = getParent(parent)) {
  680. positionStyle = utils.css(parent, 'position');
  681. if (positionStyle !== 'static') {
  682. return parent;
  683. }
  684. }
  685. return null;
  686. }
  687. var getParent$1 = utils.getParent;
  688. function isAncestorFixed(element) {
  689. if (utils.isWindow(element) || element.nodeType === 9) {
  690. return false;
  691. }
  692. var doc = utils.getDocument(element);
  693. var body = doc.body;
  694. var parent = null;
  695. for (parent = getParent$1(element); parent && parent !== body; parent = getParent$1(parent)) {
  696. var positionStyle = utils.css(parent, 'position');
  697. if (positionStyle === 'fixed') {
  698. return true;
  699. }
  700. }
  701. return false;
  702. }
  703. /**
  704. * 获得元素的显示部分的区域
  705. */
  706. function getVisibleRectForElement(element, alwaysByViewport) {
  707. var visibleRect = {
  708. left: 0,
  709. right: Infinity,
  710. top: 0,
  711. bottom: Infinity
  712. };
  713. var el = getOffsetParent(element);
  714. var doc = utils.getDocument(element);
  715. var win = doc.defaultView || doc.parentWindow;
  716. var body = doc.body;
  717. var documentElement = doc.documentElement; // Determine the size of the visible rect by climbing the dom accounting for
  718. // all scrollable containers.
  719. while (el) {
  720. // clientWidth is zero for inline block elements in ie.
  721. if ((navigator.userAgent.indexOf('MSIE') === -1 || el.clientWidth !== 0) && // body may have overflow set on it, yet we still get the entire
  722. // viewport. In some browsers, el.offsetParent may be
  723. // document.documentElement, so check for that too.
  724. el !== body && el !== documentElement && utils.css(el, 'overflow') !== 'visible') {
  725. var pos = utils.offset(el); // add border
  726. pos.left += el.clientLeft;
  727. pos.top += el.clientTop;
  728. visibleRect.top = Math.max(visibleRect.top, pos.top);
  729. visibleRect.right = Math.min(visibleRect.right, // consider area without scrollBar
  730. pos.left + el.clientWidth);
  731. visibleRect.bottom = Math.min(visibleRect.bottom, pos.top + el.clientHeight);
  732. visibleRect.left = Math.max(visibleRect.left, pos.left);
  733. } else if (el === body || el === documentElement) {
  734. break;
  735. }
  736. el = getOffsetParent(el);
  737. } // Set element position to fixed
  738. // make sure absolute element itself don't affect it's visible area
  739. // https://github.com/ant-design/ant-design/issues/7601
  740. var originalPosition = null;
  741. if (!utils.isWindow(element) && element.nodeType !== 9) {
  742. originalPosition = element.style.position;
  743. var position = utils.css(element, 'position');
  744. if (position === 'absolute') {
  745. element.style.position = 'fixed';
  746. }
  747. }
  748. var scrollX = utils.getWindowScrollLeft(win);
  749. var scrollY = utils.getWindowScrollTop(win);
  750. var viewportWidth = utils.viewportWidth(win);
  751. var viewportHeight = utils.viewportHeight(win);
  752. var documentWidth = documentElement.scrollWidth;
  753. var documentHeight = documentElement.scrollHeight; // scrollXXX on html is sync with body which means overflow: hidden on body gets wrong scrollXXX.
  754. // We should cut this ourself.
  755. var bodyStyle = window.getComputedStyle(body);
  756. if (bodyStyle.overflowX === 'hidden') {
  757. documentWidth = win.innerWidth;
  758. }
  759. if (bodyStyle.overflowY === 'hidden') {
  760. documentHeight = win.innerHeight;
  761. } // Reset element position after calculate the visible area
  762. if (element.style) {
  763. element.style.position = originalPosition;
  764. }
  765. if (alwaysByViewport || isAncestorFixed(element)) {
  766. // Clip by viewport's size.
  767. visibleRect.left = Math.max(visibleRect.left, scrollX);
  768. visibleRect.top = Math.max(visibleRect.top, scrollY);
  769. visibleRect.right = Math.min(visibleRect.right, scrollX + viewportWidth);
  770. visibleRect.bottom = Math.min(visibleRect.bottom, scrollY + viewportHeight);
  771. } else {
  772. // Clip by document's size.
  773. var maxVisibleWidth = Math.max(documentWidth, scrollX + viewportWidth);
  774. visibleRect.right = Math.min(visibleRect.right, maxVisibleWidth);
  775. var maxVisibleHeight = Math.max(documentHeight, scrollY + viewportHeight);
  776. visibleRect.bottom = Math.min(visibleRect.bottom, maxVisibleHeight);
  777. }
  778. return visibleRect.top >= 0 && visibleRect.left >= 0 && visibleRect.bottom > visibleRect.top && visibleRect.right > visibleRect.left ? visibleRect : null;
  779. }
  780. function adjustForViewport(elFuturePos, elRegion, visibleRect, overflow) {
  781. var pos = utils.clone(elFuturePos);
  782. var size = {
  783. width: elRegion.width,
  784. height: elRegion.height
  785. };
  786. if (overflow.adjustX && pos.left < visibleRect.left) {
  787. pos.left = visibleRect.left;
  788. } // Left edge inside and right edge outside viewport, try to resize it.
  789. if (overflow.resizeWidth && pos.left >= visibleRect.left && pos.left + size.width > visibleRect.right) {
  790. size.width -= pos.left + size.width - visibleRect.right;
  791. } // Right edge outside viewport, try to move it.
  792. if (overflow.adjustX && pos.left + size.width > visibleRect.right) {
  793. // 保证左边界和可视区域左边界对齐
  794. pos.left = Math.max(visibleRect.right - size.width, visibleRect.left);
  795. } // Top edge outside viewport, try to move it.
  796. if (overflow.adjustY && pos.top < visibleRect.top) {
  797. pos.top = visibleRect.top;
  798. } // Top edge inside and bottom edge outside viewport, try to resize it.
  799. if (overflow.resizeHeight && pos.top >= visibleRect.top && pos.top + size.height > visibleRect.bottom) {
  800. size.height -= pos.top + size.height - visibleRect.bottom;
  801. } // Bottom edge outside viewport, try to move it.
  802. if (overflow.adjustY && pos.top + size.height > visibleRect.bottom) {
  803. // 保证上边界和可视区域上边界对齐
  804. pos.top = Math.max(visibleRect.bottom - size.height, visibleRect.top);
  805. }
  806. return utils.mix(pos, size);
  807. }
  808. function getRegion(node) {
  809. var offset;
  810. var w;
  811. var h;
  812. if (!utils.isWindow(node) && node.nodeType !== 9) {
  813. offset = utils.offset(node);
  814. w = utils.outerWidth(node);
  815. h = utils.outerHeight(node);
  816. } else {
  817. var win = utils.getWindow(node);
  818. offset = {
  819. left: utils.getWindowScrollLeft(win),
  820. top: utils.getWindowScrollTop(win)
  821. };
  822. w = utils.viewportWidth(win);
  823. h = utils.viewportHeight(win);
  824. }
  825. offset.width = w;
  826. offset.height = h;
  827. return offset;
  828. }
  829. /**
  830. * 获取 node 上的 align 对齐点 相对于页面的坐标
  831. */
  832. function getAlignOffset(region, align) {
  833. var V = align.charAt(0);
  834. var H = align.charAt(1);
  835. var w = region.width;
  836. var h = region.height;
  837. var x = region.left;
  838. var y = region.top;
  839. if (V === 'c') {
  840. y += h / 2;
  841. } else if (V === 'b') {
  842. y += h;
  843. }
  844. if (H === 'c') {
  845. x += w / 2;
  846. } else if (H === 'r') {
  847. x += w;
  848. }
  849. return {
  850. left: x,
  851. top: y
  852. };
  853. }
  854. function getElFuturePos(elRegion, refNodeRegion, points, offset, targetOffset) {
  855. var p1 = getAlignOffset(refNodeRegion, points[1]);
  856. var p2 = getAlignOffset(elRegion, points[0]);
  857. var diff = [p2.left - p1.left, p2.top - p1.top];
  858. return {
  859. left: Math.round(elRegion.left - diff[0] + offset[0] - targetOffset[0]),
  860. top: Math.round(elRegion.top - diff[1] + offset[1] - targetOffset[1])
  861. };
  862. }
  863. /**
  864. * align dom node flexibly
  865. * @author yiminghe@gmail.com
  866. */
  867. function isFailX(elFuturePos, elRegion, visibleRect) {
  868. return elFuturePos.left < visibleRect.left || elFuturePos.left + elRegion.width > visibleRect.right;
  869. }
  870. function isFailY(elFuturePos, elRegion, visibleRect) {
  871. return elFuturePos.top < visibleRect.top || elFuturePos.top + elRegion.height > visibleRect.bottom;
  872. }
  873. function isCompleteFailX(elFuturePos, elRegion, visibleRect) {
  874. return elFuturePos.left > visibleRect.right || elFuturePos.left + elRegion.width < visibleRect.left;
  875. }
  876. function isCompleteFailY(elFuturePos, elRegion, visibleRect) {
  877. return elFuturePos.top > visibleRect.bottom || elFuturePos.top + elRegion.height < visibleRect.top;
  878. }
  879. function flip(points, reg, map) {
  880. var ret = [];
  881. utils.each(points, function (p) {
  882. ret.push(p.replace(reg, function (m) {
  883. return map[m];
  884. }));
  885. });
  886. return ret;
  887. }
  888. function flipOffset(offset, index) {
  889. offset[index] = -offset[index];
  890. return offset;
  891. }
  892. function convertOffset(str, offsetLen) {
  893. var n;
  894. if (/%$/.test(str)) {
  895. n = parseInt(str.substring(0, str.length - 1), 10) / 100 * offsetLen;
  896. } else {
  897. n = parseInt(str, 10);
  898. }
  899. return n || 0;
  900. }
  901. function normalizeOffset(offset, el) {
  902. offset[0] = convertOffset(offset[0], el.width);
  903. offset[1] = convertOffset(offset[1], el.height);
  904. }
  905. /**
  906. * @param el
  907. * @param tgtRegion 参照节点所占的区域: { left, top, width, height }
  908. * @param align
  909. */
  910. function doAlign(el, tgtRegion, align, isTgtRegionVisible) {
  911. var points = align.points;
  912. var offset = align.offset || [0, 0];
  913. var targetOffset = align.targetOffset || [0, 0];
  914. var overflow = align.overflow;
  915. var source = align.source || el;
  916. offset = [].concat(offset);
  917. targetOffset = [].concat(targetOffset);
  918. overflow = overflow || {};
  919. var newOverflowCfg = {};
  920. var fail = 0;
  921. var alwaysByViewport = !!(overflow && overflow.alwaysByViewport); // 当前节点可以被放置的显示区域
  922. var visibleRect = getVisibleRectForElement(source, alwaysByViewport); // 当前节点所占的区域, left/top/width/height
  923. var elRegion = getRegion(source); // 将 offset 转换成数值,支持百分比
  924. normalizeOffset(offset, elRegion);
  925. normalizeOffset(targetOffset, tgtRegion); // 当前节点将要被放置的位置
  926. var elFuturePos = getElFuturePos(elRegion, tgtRegion, points, offset, targetOffset); // 当前节点将要所处的区域
  927. var newElRegion = utils.merge(elRegion, elFuturePos); // 如果可视区域不能完全放置当前节点时允许调整
  928. if (visibleRect && (overflow.adjustX || overflow.adjustY) && isTgtRegionVisible) {
  929. if (overflow.adjustX) {
  930. // 如果横向不能放下
  931. if (isFailX(elFuturePos, elRegion, visibleRect)) {
  932. // 对齐位置反下
  933. var newPoints = flip(points, /[lr]/gi, {
  934. l: 'r',
  935. r: 'l'
  936. }); // 偏移量也反下
  937. var newOffset = flipOffset(offset, 0);
  938. var newTargetOffset = flipOffset(targetOffset, 0);
  939. var newElFuturePos = getElFuturePos(elRegion, tgtRegion, newPoints, newOffset, newTargetOffset);
  940. if (!isCompleteFailX(newElFuturePos, elRegion, visibleRect)) {
  941. fail = 1;
  942. points = newPoints;
  943. offset = newOffset;
  944. targetOffset = newTargetOffset;
  945. }
  946. }
  947. }
  948. if (overflow.adjustY) {
  949. // 如果纵向不能放下
  950. if (isFailY(elFuturePos, elRegion, visibleRect)) {
  951. // 对齐位置反下
  952. var _newPoints = flip(points, /[tb]/gi, {
  953. t: 'b',
  954. b: 't'
  955. }); // 偏移量也反下
  956. var _newOffset = flipOffset(offset, 1);
  957. var _newTargetOffset = flipOffset(targetOffset, 1);
  958. var _newElFuturePos = getElFuturePos(elRegion, tgtRegion, _newPoints, _newOffset, _newTargetOffset);
  959. if (!isCompleteFailY(_newElFuturePos, elRegion, visibleRect)) {
  960. fail = 1;
  961. points = _newPoints;
  962. offset = _newOffset;
  963. targetOffset = _newTargetOffset;
  964. }
  965. }
  966. } // 如果失败,重新计算当前节点将要被放置的位置
  967. if (fail) {
  968. elFuturePos = getElFuturePos(elRegion, tgtRegion, points, offset, targetOffset);
  969. utils.mix(newElRegion, elFuturePos);
  970. }
  971. var isStillFailX = isFailX(elFuturePos, elRegion, visibleRect);
  972. var isStillFailY = isFailY(elFuturePos, elRegion, visibleRect); // 检查反下后的位置是否可以放下了,如果仍然放不下:
  973. // 1. 复原修改过的定位参数
  974. if (isStillFailX || isStillFailY) {
  975. points = align.points;
  976. offset = align.offset || [0, 0];
  977. targetOffset = align.targetOffset || [0, 0];
  978. } // 2. 只有指定了可以调整当前方向才调整
  979. newOverflowCfg.adjustX = overflow.adjustX && isStillFailX;
  980. newOverflowCfg.adjustY = overflow.adjustY && isStillFailY; // 确实要调整,甚至可能会调整高度宽度
  981. if (newOverflowCfg.adjustX || newOverflowCfg.adjustY) {
  982. newElRegion = adjustForViewport(elFuturePos, elRegion, visibleRect, newOverflowCfg);
  983. }
  984. } // need judge to in case set fixed with in css on height auto element
  985. if (newElRegion.width !== elRegion.width) {
  986. utils.css(source, 'width', utils.width(source) + newElRegion.width - elRegion.width);
  987. }
  988. if (newElRegion.height !== elRegion.height) {
  989. utils.css(source, 'height', utils.height(source) + newElRegion.height - elRegion.height);
  990. } // https://github.com/kissyteam/kissy/issues/190
  991. // 相对于屏幕位置没变,而 left/top 变了
  992. // 例如 <div 'relative'><el absolute></div>
  993. utils.offset(source, {
  994. left: newElRegion.left,
  995. top: newElRegion.top
  996. }, {
  997. useCssRight: align.useCssRight,
  998. useCssBottom: align.useCssBottom,
  999. useCssTransform: align.useCssTransform,
  1000. ignoreShake: align.ignoreShake
  1001. });
  1002. return {
  1003. points: points,
  1004. offset: offset,
  1005. targetOffset: targetOffset,
  1006. overflow: newOverflowCfg
  1007. };
  1008. }
  1009. /**
  1010. * 2012-04-26 yiminghe@gmail.com
  1011. * - 优化智能对齐算法
  1012. * - 慎用 resizeXX
  1013. *
  1014. * 2011-07-13 yiminghe@gmail.com note:
  1015. * - 增加智能对齐,以及大小调整选项
  1016. **/
  1017. function isOutOfVisibleRect(target, alwaysByViewport) {
  1018. var visibleRect = getVisibleRectForElement(target, alwaysByViewport);
  1019. var targetRegion = getRegion(target);
  1020. return !visibleRect || targetRegion.left + targetRegion.width <= visibleRect.left || targetRegion.top + targetRegion.height <= visibleRect.top || targetRegion.left >= visibleRect.right || targetRegion.top >= visibleRect.bottom;
  1021. }
  1022. function alignElement(el, refNode, align) {
  1023. var target = align.target || refNode;
  1024. var refNodeRegion = getRegion(target);
  1025. var isTargetNotOutOfVisible = !isOutOfVisibleRect(target, align.overflow && align.overflow.alwaysByViewport);
  1026. return doAlign(el, refNodeRegion, align, isTargetNotOutOfVisible);
  1027. }
  1028. alignElement.__getOffsetParent = getOffsetParent;
  1029. alignElement.__getVisibleRectForElement = getVisibleRectForElement;
  1030. /**
  1031. * `tgtPoint`: { pageX, pageY } or { clientX, clientY }.
  1032. * If client position provided, will internal convert to page position.
  1033. */
  1034. function alignPoint(el, tgtPoint, align) {
  1035. var pageX;
  1036. var pageY;
  1037. var doc = utils.getDocument(el);
  1038. var win = doc.defaultView || doc.parentWindow;
  1039. var scrollX = utils.getWindowScrollLeft(win);
  1040. var scrollY = utils.getWindowScrollTop(win);
  1041. var viewportWidth = utils.viewportWidth(win);
  1042. var viewportHeight = utils.viewportHeight(win);
  1043. if ('pageX' in tgtPoint) {
  1044. pageX = tgtPoint.pageX;
  1045. } else {
  1046. pageX = scrollX + tgtPoint.clientX;
  1047. }
  1048. if ('pageY' in tgtPoint) {
  1049. pageY = tgtPoint.pageY;
  1050. } else {
  1051. pageY = scrollY + tgtPoint.clientY;
  1052. }
  1053. var tgtRegion = {
  1054. left: pageX,
  1055. top: pageY,
  1056. width: 0,
  1057. height: 0
  1058. };
  1059. var pointInView = pageX >= 0 && pageX <= scrollX + viewportWidth && pageY >= 0 && pageY <= scrollY + viewportHeight; // Provide default target point
  1060. var points = [align.points[0], 'cc'];
  1061. return doAlign(el, tgtRegion, _objectSpread2({}, align, {
  1062. points: points
  1063. }), pointInView);
  1064. }
  1065. export default alignElement;
  1066. export { alignElement, alignPoint };
  1067. //# sourceMappingURL=index.js.map