开发者

jQuery scroll to top of iframe parent

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:

$('htm开发者_Python百科l, body').animate({scrollTop:0}, 'slow');

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


Give a try for this one :

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


This seems to do the trick as well:

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

The second parameter to $ is the context to search in.


<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>


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');
};


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

In the body of the iframe's html page

Its as simple as you can do!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜