javascript - 如何从 iFrame 内部滚动到 iFrame 顶部
问题描述
我有一个iframe
, 加载到同源页面中。这iframe
有两个div
可见的一个或另一个。单击按钮,当前活动div
的将被隐藏,而另一个div
则可见。这是有效的。
问题是,点击后button
,下一个div
是可见的,但 div 的按钮在屏幕上。div
当它可见时,我想滚动到顶部。
我试过的代码: 片段 1
$('form').on('submit', function(e) {
// other code to handle visibility
window.scrollTo({
top: 0,
behavior: 'smooth'
});
}
适用于页面本身,但在作为 iframe 嵌入其他页面时无效。
片段 2
$('form').on('submit', function(e) {
// other code to handle visibility
window.parent.scrollTo({
top: 0,
behavior: 'smooth'
});
}
带到父页面的顶部
如何修改此代码以将我带到 iframe 的顶部?
解决方案
我能够解决这个问题。
这是有效的代码。
$('form').on('submit', function(e) {
// other code to handle visibility
$('.step2')[0].scrollIntoView();
// step2 is the class name of the second div
// that is to be visible
}