mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-19 09:24:51 +00:00
refactor(polyfill) scrollIntoViewIfNeeded
This commit is contained in:
parent
51b507d1a3
commit
a7ae86b734
1 changed files with 49 additions and 39 deletions
|
|
@ -90,63 +90,73 @@ var Util, DOM, jQuery;
|
|||
}
|
||||
|
||||
DOM.scrollByPages = Util.retFalse;
|
||||
/* function polyfill webkit standart function */
|
||||
/* function polyfill webkit standart function
|
||||
* https://gist.github.com/2581101
|
||||
*/
|
||||
DOM.scrollIntoViewIfNeeded = function(pElement, centerIfNeeded) {
|
||||
if (!window.getComputedStyle)
|
||||
return;
|
||||
/*
|
||||
https://gist.github.com/2581101
|
||||
*/
|
||||
centerIfNeeded = arguments.length === 0 ? true : !!centerIfNeeded;
|
||||
var parent,
|
||||
topWidth,
|
||||
leftWidth,
|
||||
parentComputedStyle,
|
||||
parentBorderTopWidth,
|
||||
parentBorderLeftWidth,
|
||||
overTop,
|
||||
overBottom,
|
||||
overLeft,
|
||||
overRight,
|
||||
alignWithTop;
|
||||
|
||||
var parent = pElement.parentNode,
|
||||
parentComputedStyle = window.getComputedStyle(parent, null),
|
||||
parentBorderTopWidth =
|
||||
parseInt(parentComputedStyle.getPropertyValue('border-top-width'), 10),
|
||||
if (window.getComputedStyle) {
|
||||
centerIfNeeded = arguments.length === 0 ? true : !!centerIfNeeded;
|
||||
parent = pElement.parentNode;
|
||||
parentComputedStyle = window.getComputedStyle(parent, null);
|
||||
|
||||
topWidth = parentComputedStyle.getPropertyValue('border-top-width');
|
||||
leftWidth = parentComputedStyle.getPropertyValue('border-left-width');
|
||||
|
||||
parentBorderTopWidth = parseInt(topWidth, 10);
|
||||
parentBorderLeftWidth = parseInt(leftWidth, 10);
|
||||
|
||||
parentBorderLeftWidth =
|
||||
parseInt(parentComputedStyle.getPropertyValue('border-left-width'), 10),
|
||||
|
||||
overTop = pElement.offsetTop - parent.offsetTop < parent.scrollTop,
|
||||
overBottom =
|
||||
overTop = pElement.offsetTop - parent.offsetTop < parent.scrollTop,
|
||||
overBottom =
|
||||
(pElement.offsetTop -
|
||||
parent.offsetTop +
|
||||
pElement.clientHeight -
|
||||
parentBorderTopWidth) >
|
||||
(parent.scrollTop + parent.clientHeight),
|
||||
|
||||
overLeft = pElement.offsetLeft -
|
||||
overLeft = pElement.offsetLeft -
|
||||
parent.offsetLeft < parent.scrollLeft,
|
||||
|
||||
overRight =
|
||||
overRight =
|
||||
(pElement.offsetLeft -
|
||||
parent.offsetLeft +
|
||||
pElement.clientWidth -
|
||||
parentBorderLeftWidth) >
|
||||
(parent.scrollLeft + parent.clientWidth),
|
||||
|
||||
alignWithTop = overTop && !overBottom;
|
||||
|
||||
if ((overTop || overBottom) && centerIfNeeded)
|
||||
parent.scrollTop =
|
||||
pElement.offsetTop -
|
||||
parent.offsetTop -
|
||||
parent.clientHeight / 2 -
|
||||
parentBorderTopWidth +
|
||||
pElement.clientHeight / 2;
|
||||
|
||||
if ((overLeft || overRight) && centerIfNeeded)
|
||||
parent.scrollLeft =
|
||||
pElement.offsetLeft -
|
||||
parent.offsetLeft -
|
||||
parent.clientWidth / 2 -
|
||||
parentBorderLeftWidth +
|
||||
pElement.clientWidth / 2;
|
||||
|
||||
if ( (overTop || overBottom || overLeft || overRight) &&
|
||||
!centerIfNeeded)
|
||||
alignWithTop = overTop && !overBottom;
|
||||
|
||||
if ((overTop || overBottom) && centerIfNeeded)
|
||||
parent.scrollTop =
|
||||
pElement.offsetTop -
|
||||
parent.offsetTop -
|
||||
parent.clientHeight / 2 -
|
||||
parentBorderTopWidth +
|
||||
pElement.clientHeight / 2;
|
||||
|
||||
if ((overLeft || overRight) && centerIfNeeded)
|
||||
parent.scrollLeft =
|
||||
pElement.offsetLeft -
|
||||
parent.offsetLeft -
|
||||
parent.clientWidth / 2 -
|
||||
parentBorderLeftWidth +
|
||||
pElement.clientWidth / 2;
|
||||
|
||||
if ((overTop || overBottom || overLeft || overRight) && !centerIfNeeded)
|
||||
pElement.scrollIntoView(alignWithTop);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
if (!document.body.classList) {
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue