【问题标题】:Prevent location.hash in an iframe from scrolling the parent window in Chrome防止 iframe 中的 location.hash 在 Chrome 中滚动父窗口
【发布时间】:2013-03-30 19:18:40
【问题描述】:

场景:

我正在构建一个在 Chrome 中运行的信息亭应用。在其中,我通过带有锚点的链接(http://www.whatever.com/#specific_content)将另一个域的 iframe 加载到模式中。

问题:

当该内容加载时,iframe 会跳转到#specific_content,但父页面也会跳转。

这是一个单页自助服务终端应用,因此父页面的位置非常重要。 场景的实际细节比上面的要复杂一些:

  1. 为了禁止在应用中滚动,我有 body {overflow:hidden;}
  2. 为了便于定位,我使用了一个设置为视口大小的包装容器。
  3. 为了模拟滚动,我要么绝对地重新定位包装器,要么绝对地定位包装器内的内容。

为了演示问题,我设置了一个 jsFiddle,但您需要查看没有 jsFiddle chrome 的输出才能看到问题的全部范围:

第 1 步)点击“链接到内容”,这将重新定位包装器

第 2 步)点击“链接加载”,这将加载 iFrame 内容

演示:http://jsfiddle.net/w47GT/8/embedded/result/

小提琴:http://jsfiddle.net/w47GT/8/

代码:

CSS:

html, body { height:100%; padding:0; margin: 0;overflow:hidden; }
.background {
    width : 100%;
    height:400%;
    background:#333 url('http://incurablystircrazy.files.wordpress.com/2012/04/runner-at-sunset.jpg');
}
.wrapper { position:absolute;width:100%;height:200%; }
iframe  { width:50%;height:50%;position:fixed;top:25%;left:50%;margin-left:-25%;border:2px solid red;background-color:#ddd; }
.link   { width:100%;height:25px;position:absolute;top:25px;background-color:green; }
.anchor { position:absolute;top:400px;width:100%;height:25px;background-color:red; }

HTML

<div class="wrapper">
    <div class="background"></div>

    <a class="link" href="#linked">Link to content</a>
    <a class="anchor" name="linked" href="http://www.smashingmagazine.com#footer" >Link to Load iFrame</a>
</div>
<iframe></iframe>

JS

$(function(){
    $('.link').on('click',function(){ $('.wrapper').css({top:'-400px'}); });
    $('.anchor').on('click',function(e){
        e.preventDefault();
        var href = $(this).prop('href');
        $('iframe')
        .attr({
            src: href,
            name: 'testing'
        });
    });
});

【问题讨论】:

标签: javascript jquery css google-chrome browser


【解决方案1】:

可以在这里找到答案:Loading iframe with an #element specified moves the parent viewpoint to the same location

解决方案:

隐藏 iFrame 直到它完全加载后绕过影响父页面的锚。

演示: http://jsfiddle.net/w47GT/12/embedded/result/

小提琴: http://jsfiddle.net/w47GT/12/

JS

$(function(){
    // Move the content wrapper up to simulate scroll
    $('.link').on('click',function(){ $('.wrapper').css({top:'-400px'}); });

    // Load iFrame content (previously hidden via CSS)
    $('.anchor').on('click',function(e){
        e.preventDefault();
        var href = $(this).prop('href');
        $('iframe')
        .attr({
            src: href,
            name: 'testing'
        }).on('load',function(){
            $(this).show();
        });
    });
});

感谢@DJdavid98 指出这个答案。

【讨论】:

  • 优秀的答案!这种方法帮助我修复了一个非常烦人的错误,即动态加载 iframe 以播放来自 twilio 的通话录音会使界面跳下。谢谢!
【解决方案2】:

我在这里有点摸不着头脑,但是您是否尝试过将类似的东西附加到外部页面的 DOM 上?

$(window).on("hashchange", function(e) {
  e.preventDefault();
});

【讨论】:

  • 该死的。抱歉,我无法提供更多帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
相关资源
最近更新 更多