在 iframe 中单击子页面时如何将父页面滚动到顶部?

发布于 2024-09-05 18:22:09 字数 251 浏览 2 评论 0原文

当有人点击 iframe(子页面)内的链接时,如何让父页面滚动到顶部?问题是子页面将保留在页面的同一位置,因为 iframe 的高度比父页面大很多。

请注意:父页面和子页面位于不同的子域中。

我创建了一个演示来展示这一点: http://www.apus.edu/_test/iframe/index.htm

When someone clicks on a link within an iframe (child page), how do I get the parent page to scroll to the top? The issue is the child page will remain in the same spot of the page, because the iframe has a lot of height larger than the parent page.

Please note: the parent and child pages are on different sub domains.

I created a demo to show this:
http://www.apus.edu/_test/iframe/index.htm

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北方的巷 2024-09-12 18:22:09

诀窍是将以下 onload="window.parent.parent.scrollTo(0,0)" 附加到 iframe 即可!

The trick is to append the following onload="window.parent.parent.scrollTo(0,0)" to the iframe and that should do it!

或十年 2024-09-12 18:22:09

如果您有跨源(iframe 和父级具有不同的域),则仅调用 window.scrollTo(0,0) 将不起作用。

跨域的一种解决方案是从 iframe 向父级发送可信消息。

在 iframe 中编码:

var parent_origin = 'http://your/iframe/domain/here'
parent.postMessage({'task': 'scroll_top'}, parent_origin);

然后在父级中编码:

function handleMessage(event) {
    var accepted_origin = 'http://your/iframe/domain/here';
    if (event.origin == accepted_origin){
        if (event.data['task'] == 'scroll_top'){
           window.scrollTo(0,0);
        }
        // you can have more tasks
    } else{
        console.error('Unknown origin', event.origin);
    }
}

window.onload = function() {
    window.addEventListener("message", handleMessage, false);
}

If you have cross origins (the iframe and the parent have different domains), then just calling window.scrollTo(0,0) won't work.

One solution to cross-origin is to send a trusted message from the iframe to the parent.

Code inside the iframe:

var parent_origin = 'http://your/iframe/domain/here'
parent.postMessage({'task': 'scroll_top'}, parent_origin);

Then code in the parent:

function handleMessage(event) {
    var accepted_origin = 'http://your/iframe/domain/here';
    if (event.origin == accepted_origin){
        if (event.data['task'] == 'scroll_top'){
           window.scrollTo(0,0);
        }
        // you can have more tasks
    } else{
        console.error('Unknown origin', event.origin);
    }
}

window.onload = function() {
    window.addEventListener("message", handleMessage, false);
}
演多会厌 2024-09-12 18:22:09

在 iframe 中使用 JavaScript,引用父级并调用 scroll( ) 方法。

window.parent.scroll(0,0);

(注意。如果 iframe Url 与父级的域不同,则此操作将不起作用。)

Using JavaScript within the iframe, reference the parent and call the scroll() method.

window.parent.scroll(0,0);

(Note. This will not work if the iframe Url is a different domain than the parent.)

心的位置 2024-09-12 18:22:09

在 Iframe 页面内。

window.parent.ScrollToTop(); // Scroll to top function

在父页面上:

window.ScrollToTop = function(){
  $('html,body', window.document).animate({
    scrollTop: '0px'
  }, 'fast');
};

Within the Iframe page.

window.parent.ScrollToTop(); // Scroll to top function

On The parrent page:

window.ScrollToTop = function(){
  $('html,body', window.document).animate({
    scrollTop: '0px'
  }, 'fast');
};
夜无邪 2024-09-12 18:22:09

带动画

           window.parent.scroll({
              top: 0,
              left: 0,
              behavior: 'smooth'
            });

With animation

           window.parent.scroll({
              top: 0,
              left: 0,
              behavior: 'smooth'
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文