简体   繁体   中英

Scroll to top from iframe

I want to call jquery to jump to top from iframe.

 <script>
 $(document).ready(function() {
  window.parent.$("body,html").animate({scrollTop:0}, 'slow');
 });
 </script>

Doesn't work..

//On same domain...

If the iframe is in the same domain as parent then try this

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

window.parent.$("body,html") will use window.parent.$ jQuery object but will still look for element inside iframe because you are not specifying the context here.

It's probably because it's restriction of Same Origin Policy . This is not possible if you're on same domain.

If it's on same domain, try to change

window.parent.$("body,html")

to

$("body,html", window.top)

To load to top iframe in main page you should write your javascript function.

function loadToTop() {
    parent.self.scrollTo(0, 0);
}

It worked! Let's enjoy

我在JQuery中不知道,但是在简单的JS中,我使用了以下方法:

parent.scroll(0, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM