Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:02:12+00:00 2026-05-30T15:02:12+00:00

I need to load content with dynamic width within Fancybox iframe. To achieve resizing

  • 0

I need to load content with dynamic width within Fancybox iframe. To achieve resizing of the iframe to fit the content, I need to pass the content width to Fancybox call. I have done this using the following:

jQuery('#container').on('click', '.fancybox-iframe', function(e){
    e.preventDefault();

    var iframeWidth = parseInt(jQuery(this).attr('id'));

    jQuery(this).fancybox({
        width           : iframeWidth,
        height          : '100%',
        autoSize        : true,
        fixed           : true,
        type            : 'iframe',
        loop            : false,
        padding         : 0,
        closeBtn        : false,
        afterLoad       : function() {
                            jQuery('body').css('overflow', 'hidden');
        },
        afterClose      : function() {
                            jQuery('body').css('overflow', 'auto');
        },
        helpers         : {title: {type: 'outside'}, overlay: {opacity: 0.8, css: {'background-color': '#FFFFFF'}}, thumbs: {width: 50, height: 50}, buttons: {}}
    });
});

The HTML for the above:

<a id="<?php echo $content_width; ?>" class="fancybox-iframe fancybox.iframe" rel="gallery" href="<?php the_permalink(); ?>"></a>
<a id="<?php echo $content_width; ?>" class="fancybox-iframe fancybox.iframe" rel="gallery" href="<?php the_permalink(); ?>"></a>
<a id="<?php echo $content_width; ?>" class="fancybox-iframe fancybox.iframe" rel="gallery" href="<?php the_permalink(); ?>"></a>

The above code has two issues: First, I have to click the hyperlink twice, every time for Fancybox to appear. The second issue is that, this does not work with the rel attribute of the HREF and therefore does not display content as a Fancybox gallery. To overcome the gallery issue, I replaced jQuery(this).fancybox({ with jQuery('.fancybox-iframe').fancybox({. This made the galleries feature work. However, Fancybox now only takes the value of iframeWidth variable for the first link clicked.

Any ideas on how to pass content width variable to Fancybox and also maintain the gallery feature? BTW, I am working with Fancybox 2.0.5

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:02:13+00:00Added an answer on May 30, 2026 at 3:02 pm

    If you are using fancybox v2.x, you don’t need to use jQuery .on() method since fancybox already uses “live” so

      $(".fancybox-iframe").bind('click', function(){
        var iframeWidth = parseInt(jQuery(this).attr('id'));
        }).fancybox({
       //options
        width : iframeWidth,
      });
    

    should do the trick.

    UPDATE:

    I have revised my code above and actually things can be much simpler than expected.

    The following code allows you to pass the width parameter dynamically through the ID AND have a gallery of iframes (with just a single click). Just add to the afterLoad callback this :

    jQuery(".fancybox-iframe").fancybox({
     //other API options here
     afterLoad: function(){
      var iframeWidth = parseInt(jQuery(".fancybox-iframe").eq(this.index).attr('id'));
      this.width = iframeWidth
     }
    });// fancybox
    

    Notice that you don’t need to set the option width.

    OPTION 2

    Alternatively you could also pass the width parameter dynamically using the data-width attribute (if you are using HTML5 DOCTYPE) instead of the ID (and use the ID for what it matters). I think that would be a more elegant solution.

    Your html then should look like

    <a data-width="<?php echo $content_width; ?>" id="myID" class="fancybox-iframe fancybox.iframe" rel="gallery" href="<?php the_permalink(); ?>"></a>
    

    and the javascript is pretty much the same

    jQuery(".fancybox-iframe").fancybox({
     //options
     afterLoad: function(){
      var iframeWidth = jQuery(".fancybox-iframe").eq(this.index).data("width");
      this.width = iframeWidth
     }
    });// fancybox
    

    I have set a DEMO with the two options that you can see here

    Last note: Fancybox uses a padding of 15px so the total width will be iframeWidth + 30, unless you set the padding option to 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to load database content into a JTable . Many times I search
I have a UI using dynamic tabs, so content can be loaded into a
I need to dynamically load content from HTML emails into some type of content
I'm creating a website where I'm doing ajax requests to load some dynamic content
I need to load content from an external page when the user clicks on
Across my application, I need to load a content block into a jQuery UI
I have a blog, dynamic (php) and static content (images, css, js). I googled
So I'm trying to load dynamic content straight into my checkbox container (group_checkboxes) <div
for my app I need to load some content from my webservice. I want
I need help with jQuery and ajax. need to load textarea content from file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.