问题描述
我在一个带有scollbars的页面中有一个元素,通过JS调用 element.scrollIntoView()
在页面加载时进入视图 - 这个工作正常,正如预期的那样。
I have an element inside of a page with scollbars, which is brought into view on page load via JS call to element.scrollIntoView()
- and this works fine, as expected.
现在,如果我将此页面放入IFRAME,则调用 element.scrollIntoView()
不仅将iframe中的元素带到iframe的顶部 - 如果父页面具有自己的滚动条 - 它也会滚动父页面以将有问题的元素也带到父页面的顶部。
Now, if I place this page into an IFRAME, the call to element.scrollIntoView()
not only brings element inside of iframe to the top of iframe - if the parent page has scrollbars of its own - it scroll the parent page as well to bring the element in question to the top of parent page as well.
我理解这可能是设计上的行为,但有没有办法只将scrollIntoView行为包含在IFRAME中,或者是否有任何替代方法可以有这种行为(不影响父页面) )。
I understand this is probably behavior by design, but is there a way to contain "scrollIntoView" behavior to the IFRAME only, or are there any alternatives to have this behavior (without affecting the parent page).
推荐答案
。我可以将元素的父元素 scrollTop
设置为元素自己的 offsetTop
。这将使父级滚动到元素的位置,效果将是IFRAME的本地。
Figured it out. I can set element's parent's scrollTop
to element's own offsetTop
. This will make parent scroll to element's position and effect will be local to IFRAME.
这篇关于scrollIntoView仅适用于IFRAME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!