diff --git a/lib/client/polyfill.js b/lib/client/polyfill.js index 1e913ff3..fc142f29 100644 --- a/lib/client/polyfill.js +++ b/lib/client/polyfill.js @@ -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) {