开发者

How to position fancybox on top

How to position the fancybox 100px from top 开发者_开发知识库by default it is center aligned.

I believe there is no api settings for that. Can anyone help me out in doing it on the actual script.


The solution I got working was styling the wrapper like this:

#fancybox-wrap {
  position: absolute;
  top: 100px !important;
}

Hope it helps!


$.fancybox({
   'width'     : 620,
   'height'    : 490,
   'speedIn'    :600,
   'speedOut'   :200,
   'onComplete': function() {
      $("#fancybox-wrap").css({'top':'20px', 'bottom':'auto'});
   }
});


In fancyBox 2 I use topRatio : 0, this propery move fancyBox at top, then I used jQuery().css() to put it where I want. Example:

$("#myfancy").fancybox({
    wrapCSS     : 'fancybox-custom',
    maxWidth    : 500,
    maxHeight   : 360,
    fitToView   : false,
    width       : '85%',
    height      : '95%',
    autoSize    : false,
    openEffect  : 'fade',
    openSpeed   : 'slow',
    closeEffect : 'fade',
    modal       : true,
    closeSpeed  : 'slow',
    topRatio    : 0,
    helpers         : {
        overlay     : {
            css     : {
                'background-color' : '#fff'
            }
        }
    }
}).trigger('click');
jQuery('.fancybox-wrap').css('margin-top', '120px');


scrollTop page.

$.fancybox({
       'width'     : 620,
       'height'    : 490,
       'speedIn'    :600,
       'speedOut'   :200,
       'onComplete': function() {
          $(document).scrollTop(0);
          $("#fancybox-wrap").css({'top':'20px', 'bottom':'auto'});
       }
    });


This one works. Just add the style after loading the fancybox css. This will overide any movement.

<link rel="stylesheet" type="text/css" href="../css/fancybox/jquery.fancybox-1.3.4.css" media="screen" >
<style type="text/css">div#fancybox-wrap {top:100px !important;}</style>

Source

http://groups.google.com/group/fancybox/browse_thread/thread/b874901b11414835/111301916450df93?lnk=gst&q=top#111301916450df93


If you just want to position the fancybox a little bit high, you can use this parameter: "topRatio", check out the document at http://fancyapps.com/fancybox/#docs.


$('.fancybox').fancybox({
    topRatio: 0, 
    afterShow: function(){
        $(".fancybox-wrap").css({"top":0, "margin":"100px 0 0"});
    }
});


I tweaked the above code a little bit. And its working fine for me.

<script type="text/javascript">
    $(document).ready(function() {
         $('a.fancybox').each(function(){
            var fWidth = parseInt($(this).attr('href').match(/width=[0-9]+/i)[0].replace('width=',''));
            var fHeight = parseInt($(this).attr('href').match(/height=[0-9]+/i)[0].replace('height=',''));
            var posTop = parseInt($(this).attr('href').match(/top=[0-9]+/i)[0].replace('top=',''));
            var posLeft = parseInt($(this).attr('href').match(/left=[0-9]+/i)[0].replace('left=',''));
            var marLeft = parseInt($(this).attr('href').match(/marleft=[0-9]+/i)[0].replace('marleft=',''));
            $(this).fancybox({'type':'iframe','transitionIn':'elastic','transitionOut':'elastic','width':fWidth,'height':fHeight,onStart: function () {
        //create a style that will position the fancy box relative to the element that clicked it.
            var leftT=posLeft;
            var topT=posTop;
            var style = document.createElement('style');
            style.type = 'text/css';
            style.innerHTML = '.shekhar{ top: ' + topT + 'px !important; left: ' + leftT + '% !important; margin-left: -' + marLeft + 'px !important;  }';
            document.getElementsByTagName('head')[0].appendChild(style);
            $('#fancybox-wrap').addClass("shekhar");
            }
            });
      }); 
    });
</script>

HTML is here:

<ul>
    <li><a class="fancybox" href="http://iacampaigns.co.in/fiama/WAP/index.html?iframe&width=100&height=100&top=100&left=50&marleft=200">Iframe</a></li>
    <li><a class="fancybox" href="http://iacampaigns.co.in/fiama/WAP/index.html?iframe&width=200&height=200&top=100&left=50&marleft=100">Iframe</a></li>
  </ul>


don'y know if version of fancybox you're using is 2.xxx, but in this case you can set the default margin like this:

$("#myfancy").fancybox({
margin     : [100, 0, 20, 0],
...

where margin set the position (px) of the wrapper (first number in the array is TOP)


If using V2, the below helper method would do the trick on mobile devices:

$('.fancybox_success').fancybox({
    afterShow: function () {
        if ($portWidth <= $mobileBreakpoint) {
            $('html,body').animate({ scrollTop: 0 }, 'slow');
        }
    },...


A two pronged attack but by using position 'fixed' as opposed to absolute will disable scrolling an stop the frame from moving off the page

Internal CSS:

.fancybox-wrap { position:fixed !important; top: 100px !important; };

fancyBox jQuery:

onComplete  : function(){
        $('.fancybox-wrap').css({'position':'fixed', 'top':'100px'});
    }


For fancyBox 2.0 and later as mentioned in this doc http://fancyapps.com/fancybox/#docs

margin : Minimum space between viewport and fancyBox. Can be set as array - [top, right, bottom, left]. Right and bottom margins are ignored if content dimensions exceeds viewport
Integer, Array; Default value: 20

Example:

$('.fc-event').fancybox({
    autoCenter: true,
    margin: [100, 0, 0, 0], // [top, right, bottom, left]
    type: 'ajax',


I've never used fancybox myself but it looks like you need to set centerOnScroll to false and then position the #fancybox-wrap with css however you see fit.


    //pass in the id of the link that was clicked and set the width and height for the iframe
    $.fn.fancybox_new = function (options) {
        for (i = 0; i < this.length; i++) {
            options._id = $(this[i]).attr("id");
            switch (options._id) {
                case "elementid": //set window size based on id
                    options.width = 375;
                    options.height = 190;
                    break;
            }
            $(this[i]).fancybox(options);
        }
    };

    $("a[rel*=formWin]").fancybox_new({
        onStart: function () {
            //create a style that will position the fancy box relative to the element that clicked it.
            if (this._id != "" && !$(this).hasClass(this._id)) {
                var style = document.createElement('style');
                var pos = $("#" + this._id).position();
                var mRight = parseInt($("#" + this._id).css("margin-right").replace("px")) + parseInt($("#" + this._id).css("padding-right").replace("px")) + parseInt($("#" + this._id).css("margin-left").replace("px")) + parseInt($("#" + this._id).css("padding-left").replace("px"));
                var mTop = parseInt($("#" + this._id).css("margin-top").replace("px", "")) + parseInt($("#" + this._id).css("padding-top").replace("px", "")) + parseInt($("#" + this._id).css("margin-bottom").replace("px", "")) + parseInt($("#" + this._id).css("padding-bottom").replace("px", ""));
                style.type = 'text/css';
                style.innerHTML = '.' + this._id + ' { top: ' + (pos.top + $("#" + this._id).height() + mTop) + 'px !important; right: ' + ($("body").width() - pos.left - $("#" + this._id).width() - mRight).toString() + 'px !important; left:auto !important; }';
                document.getElementsByTagName('head')[0].appendChild(style);
                $('#fancybox-wrap').addClass(this._id);
            }
        }
    });


Two possible issues:

  1. Fancybox is not getting absolute position or z-index. Solution: add !important in wrapper where position is absolute.
  2. Div underneath them have absolute position too or positive z-index. Solution: give fixed position or relative to underneath divs and adjust or add negative z-index.


I've modified fancybox library

Version: 1.3.4 (11/11/2010)

for making this work:

In file jquery.fancybox.js find line:

    $.fancybox.center = function() {

in this scope find:

     wrap
        .stop()
        .animate({
            'top' : parseInt(Math.max(view[3] - 20, view[3] + ((view[1] - content.height() - 40) * 0.5) - currentOpts.padding)),

and change it to:

    var topFromOptions = parseInt(Math.max(view[3] - 20, view[3] + ((view[1] - content.height() - 40) * 0.5) - currentOpts.padding));
    if (currentOpts.top) {
        topFromOptions = currentOpts.top;
    }
    wrap
        .stop()
        .animate({
            'top' : topFromOptions,

While revoking fancybox on selector pass option (for example):

top: '100px' 

or what ever You want :)

$('#fancybox-selector').fancybox({ 
    top: '20px',
    // transitionOut:'elsatic'
});


this work fine for me

<script>
    $.fancybox({
       'width'     : 620,
       'height'    : 490,
       'speedIn'    :600,
       'speedOut'   :200,
                'onStart' :function()
                {

                        $("#fancybox-wrap").addClass('my_class');
                },
    });
</script>
    <style>
    .my_class{top:20px !important; }
    </style>


In my case, such a simple solution helped me:

CSS

#YOUR_FANCYBOX_WINDOW_SELECTOR {
    vertical-align: top;
}

FancyBox v3.5.2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜