简体   繁体   English

Fancybox 2.1.3阻止iframe滚动条

[英]Fancybox 2.1.3 preventing iframe from haivng scrollbars

I am using fancybox.js version 2.1.3 to place external content into an iframe. 我正在使用fancybox.js版本2.1.3将外部内容放入iframe。 I am doing this by placing the url followed by a div id (eg http://somesite.com#about ), this seems to work however the problem is I have not found away to stop fancybox from scrolling. 我这样做是通过在URL后面加上div ID(例如http://somesite.com#about )来完成的,这似乎可行,但是问题是我还没有找到阻止fancybox滚动的方法。 preventing scrolling is necessary since i will be placing several div ids on the same external page and then placing them in iframes with fancybox, and i do not want to give the viewer the ability to scroll down within the iframe to view other div ids on this external page. 防止滚动是必要的,因为我将在同一个外部页面上放置多个div ID,然后使用fancybox将其放置在iframe中,并且我不想让查看者能够在iframe中向下滚动以查看此其他div id外部页面。

//fancybox window script
$(document).ready(function() {
    $(".various").fancybox({
        type        :'iframe',
            scrolling   : 'no',
        maxWidth    : 800,
        maxHeight   : 400,
        fitToView   : true,
        width       : '70%',
        height      : '70%',
        autoSize    : false,
        closeClick  : false,
        openEffect  : 'none',
        closeEffect : 'none',
    });
});

You can disable the scrolling by using a helper as follows: 您可以通过使用帮助器来禁用滚动,如下所示:

    iframe : {
        scrolling : 'no'
    }

Why don't you load only the fragment you need rather than the whole document? 为什么不只加载所需的片段,而不加载整个文档? In that way, you don't need to prevent the scrolling. 这样,您无需阻止滚动。

If that is an option, you would need to change the way you are loading the file ... a sort of ajax method would work better instead of iframe. 如果可以选择的话,则需要更改加载文件的方式...一种ajax方法比iframe更好。

I suggest to use jQuery .load() to pull only the requested inner div referenced by its ID .... so if you are targeting href="http://somesite.com#about" for instance, then we would need to call $("#targetContainer").load("http://somesite.com #about"); 我建议使用jQuery .load()仅提取其ID引用的所请求的内部div ...。因此,例如,如果您定位的是href="http://somesite.com#about" ,则我们需要调用$("#targetContainer").load("http://somesite.com #about");

In order to achieve that, we would need to split the hash ( #about ) from the url to pass the proper format into the .load() method (notice the space between the url and the hash ) ... so having this html 为了实现这一点,我们需要将urlhash#about )拆分为适当的格式,并将其传递给#about .load()方法(注意urlhash之间的空间)...因此需要使用html

<a class="various" href="http://somesite.com#about">about</a>

... this script should work : ...此脚本应该工作:

// open only selected div (hash)
var thisHref, thisHash;
$(".various").on("click", function() {
    thisHref = this.href.split("#")[0];
    thisHash = this.href.split("#")[1];
    targetContent = $("<div />").load(thisHref + " #" + thisHash);
    $.fancybox(targetContent, {
        maxWidth: 800,
        maxHeight: 400,
        fitToView: true,
        width: '70%',
        height: '70%',
        autoSize: false,
        closeClick: false,
        openEffect: 'none',
        closeEffect: 'none'
    }); // fancybox
    return false; // prevent default
}); // on

See this working DEMO . 参见此工作演示 The first link pulls the whole file so scroll bars appear while the following only the requested fragment. 第一个链接拉动整个文件,因此出现滚动条,而第二个仅显示请求的片段。

NOTE : Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy ; 注意 :由于浏览器的安全限制,大多数“ Ajax”请求都受相同的原始策略约束; the request cannot successfully retrieve data from a different domain, subdomain, or protocol. 该请求无法成功从其他域,子域或协议检索数据。

BTW, .on() requires jQuery 1.7+ 顺便说一句, .on()需要jQuery 1.7+

Very easy: just add 非常简单:只需添加

  .fancybox-inner {
    overflow: hidden !important;
  }

And the scroll bars are gone! 滚动条不见了!

You can simply edit Fancybox file "jquery.fancybox.js". 您可以简单地编辑Fancybox文件“ jquery.fancybox.js”。

Change: 更改:

iframe : {
                scrolling : 'auto',
                preload   : true
            }

into this: 到这个:

iframe : {
                scrolling : 'no',
                preload   : true
            }

Checked for Fancybox 2.1.5. 检查了Fancybox 2.1.5。

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

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