jQuery jQuery滚动到iframe父级的顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3805243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:08:15  来源:igfitidea点击:

jQuery scroll to top of iframe parent

jqueryscroll

提问by iltdev

I have an iFrame with a feedback form in it. After the user clicks submit, I want use parent window to scroll to the top. I know I can scroll to the top of a page with:

我有一个带有反馈表的 iFrame。用户点击提交后,我想使用父窗口滚动到顶部。我知道我可以滚动到页面顶部:

$('html, body').animate({scrollTop:0}, 'slow');

However, this will only scroll the contents of the iframe, not the parent page. Any suggestions?

但是,这只会滚动 iframe 的内容,而不是父页面。有什么建议?

回答by xar

Give a try for this one :

试试这个:

window.parent.$("body").animate({scrollTop:0}, 'slow');

回答by Jeshurun

This seems to do the trick as well:

这似乎也能解决问题:

$('html, body', window.parent.document).animate({scrollTop:0}, 'slow');

$('html, body', window.parent.document).animate({scrollTop:0}, 'slow');

The second parameterto $is the context to search in.

第二个参数,以$在搜索范围内。

回答by Angrej Singh

<script>
    jQuery(document).ready(function($) {
        FB.Canvas.scrollTo(0,0);
        FB.Canvas.setSize({width: 760, height:$('body').height()+20});
    });

    function scrollTo(x,y) {
        $("body").append('<iframe id="scrollTop" style="border:none;width:1px;height:1px;position:absolute;top:-10000px;left:-100px;" src="http://static.ak.facebook.com/xd_receiver_v0.4.php?r=1#{%22id%22%3A0%2C%22sc%22%3Anull%2C%22sf%22%3A%22%22%2C%22sr%22%3A2%2C%22h%22%3A%22iframeOuterServer%22%2C%22sid%22%3A%220.957%22%2C%22t%22%3A0}[0%2C%22iframeInnerClient%22%2C%22scrollTo%22%2C{%22x%22%3A'+x+'%2C%22y%22%3A'+y+'}%2Cfalse]" onload="$(\'#scrollTop\').remove();"></iframe>');
    }
</script>

回答by steffanjj

Within the Iframe page.

在 Iframe 页面内。

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

On The parrent page:

在父页面上:

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

回答by Naveen V

<body onLoad="window.parent.scroll(0,0);">

<body onLoad="window.parent.scroll(0,0);">

In the body of the iframe's html page

在 iframe 的 html 页面的正文中

Its as simple as you can do!

它就像你能做的那样简单!