scrollIntoViewIfNeeded

scrollIntoViewIfNeeded

如果可以的话,scrollIntoViewIfNeeded似乎是仅由WebKit提供的功能。但是,我正在研究需要与IE 7兼容的东西,并且我需要检测是否可见,如果不可见,请在其上调用scrollIntoView()。

还需要注意的是,我不是在处理整个窗口,而是在处理一个较小的DIV,其中一个元素可以从可见框中滚动出来。

例如,ID为“pleaseFindMe”的元素只有在从div的可见区域溢出时才应滚动到 View 中。

    <div style='border:1px solid black;width:40%; overflow:scroll;'>
        <span id='e2' style='white-space: nowrap;' >Lorem ipsum aliqua proident veniam et quis consectetur esse dolore non Ut nulla dolor eu culpa. Lorem ipsum sint cupidatat non et adipisicing esse elit officia. proident, sunt in culpa qui officia deserunt mollit anim <span id='pleaseFindMe'>id est</span> laborum. </span>
    </div>

最佳答案

自IE4以来,IE一直支持漂亮的元素方法getBoundingClientRect。尽管它在IE
所以这是窍门:

var findMe = document.getElementById("pleaseFindMe"),
    contRect = container.getBoundingClientRect(),
    findMeRect = findMe.getBoundingClientRect();
if (findMeRect.top < contRect.top || findMeRect.bottom > contRect.bottom
       || findMeRect.right > contRect.right || findMeRect.left < contRect.left)
    findMe.scrollIntoView();

关于javascript - IE7的scrollIntoViewIfNeeded(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11461724/

10-09 18:13