简体   繁体   English

Fancybox onComplete-定位可瞬间进行

[英]Fancybox onComplete - Positioning works for a split second

I'm trying to position the fancybox that is inside a Facebook app with scrolling disabled. 我试图将禁用滚动的Facebook应用程序中的fancybox定位。

The onComplete function works for a fraction of a second before going back to the vertical center of the frame. onComplete函数在返回框架的垂直中心之前会工作一秒钟。 What is making it snap back to the center? 是什么使它迅速回到中心位置?

$("#footer-test-link").fancybox({
    "transitionIn"  : "fade",
    "transitionOut" : "fade",
    "speedIn" : 300,
    "speedOut" : 300,
    "easingIn" : "fade",
    "easingOut" : "fade",
    "type": "ajax",
    "overlayShow" : true,
    "overlayOpacity" : 0.3,
    "titleShow" : false,
    "padding" : 0,
    "scrolling" : "no",
    "onComplete" : function() {
        $("#fancybox-wrap").css({"top": 80 +"px"});
    }
});

just try to set the css-property of '#fancybox-wrap' like following 只需尝试设置'#fancybox-wrap'的css属性,如下所示

#fancybox-wrap {
    top: 80px !important;
}

the !important-option ensure that jQuery or an other JavaScript isn't able to change/override this css-setting. !important-option确保jQuery或其他JavaScript无法更改/覆盖此CSS设置。

FancyBox is showing your positioning for a split second because there is a race condition between the event that vertically centers the FancyBox wrapper ( #fancybox-wrap ) and the onComplete callback. FancyBox暂时显示您的位置,因为在垂直居中FancyBox包装器( #fancybox-wrap )与onComplete回调之间的事件之间存在竞争条件 onComplete will usually finish first, so the css({"top": 80 +"px"}) is then over-written. onComplete通常会先完成,因此css({"top": 80 +"px"})随后会被覆盖。 You could try any number of ways to fix this, but I prefer to add a CSS rule onComplete rather than force the position of every FancyBox ever, as algorhythm's solution does. 您可以尝试多种方法来解决此问题,但是我更喜欢在Complete上添加CSS规则,而不是像algorhythm的解决方案那样强行设置每个FancyBox的位置。

'onComplete' : function () {
    // Create a pseudo-unique ID - this is actually pretty weak and you may want to use a more involved method
        var dt = new Date();
        var className = ('cl'+dt.getTime()).substr(0,10); // Create a unique class name
    // Create a style element, set the contents, and add it to the page header
        var style = document.createElement('style');
        jQuery(style).attr('type','text/css').html('.'+className+' { top: 80px !important; }').appendTo(jQuery('head'));
    // Add the pseudo-unique class name to the FancyBox wrapper to apply the CSS
        jQuery('#fancybox-wrap').addClass(className);
}

You may want to look into a better unique id for this, but this was sufficient for my needs. 您可能想要为此找到更好的唯一ID ,但这足以满足我的需求。 Please note that this answer is in regard to FancyBox v1.x (I used v1.3.4). 请注意,此答案与FancyBox v1.x有关(我使用v1.3.4)。 v2.x may have a different logical flow, and DOES have different callbacks. v2.x可能具有不同的逻辑流程,并且DOES具有不同的回调。 Instead of using onComplete, in v2.x you would need to use either afterLoad or afterShow as per the FancyBox docs . 在v2.x中,您需要按照FancyBox docs使用afterLoad或afterShow来代替onComplete。

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

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