简体   繁体   English

从iframe滚动到顶部

[英]Scroll to top from iframe

I want to call jquery to jump to top from iframe. 我想调用jquery从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 如果iframe与父目录位于同一域中,请尝试以下操作

$(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. window.parent.$("body,html")将使用window.parent.$ jQuery对象,但仍将在iframe查找元素,因为您未在此处指定上下文。

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. 要加载到首页的顶部iframe,您应该编写javascript函数。

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

It worked! 有效! Let's enjoy 让我们享受

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

parent.scroll(0, 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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