views:

293

answers:

4

I have a script that dynamically loads iframes inside a webpage. You can see the demo here: DEMO

I want to use jquery to achieve a similar rseult, but with a nice sliding effect, and external links that load the iframe in another div. Basically, I have 10 links on a page. When the link is clicked, I want the iframe window to load in the new content and apply an effect to the transition. Does anyone know of any plugins that exist for this? I am not experienced enough to write a script from scratch, so any help would be much appreciated!

A: 

jQuery has a slide effect built into it. You may be able to achieve what you are looking for by using two iframes (call them old and new) and using the slide effect to transition between them.

Justin Ethier
there will be at least 10 iframes
JCHASE11
+2  A: 

Changed based on comments...

Given a page like

<input type="button" value="Load Frames" id="submit" name="submit" onclick="loadFrame()" />
<iframe class="frameToChange" link="http://www.apple.com" style="display: none" ></iframe>
<iframe class="frameToChange" link="http://www.google.com" style="display: none" ></iframe>

Try something like the below

function loadFrame() {
    var j$ = jQuery.noConflict();
    var frames = j$('.frameToChange');

    frames.each(function(index) {
        j$(this).slideDown('fast');
        j$(this).attr('src', j$(this).attr('link'));
    });
}
Christopherous 5000
this looks great, but how would I specify 10 separate iframes URLS? Wouldnt it better to give it a url attribute (in the HTML), and use jquery to load the url in the form of an iframe?
JCHASE11
@JCHASE11, updated based on your comments. Just add as many iframes with class="frameToChange" as you need
Christopherous 5000
Chris, looks good, but its not quite there yet. You can see your code on this demo page I created: http://vitaminjdesign.com/iframe/When the button is clicked, nothing happens. I think it is because in your jquery, it doesnt say where to load the iframe. I will create a div called #test that I want the iframe to load in. How do I adjust the code to make it load into the div?
JCHASE11
the firebug console says "loadPlayBook is not defined" . when the button is clicked, there is nothing telling it what to do next
JCHASE11
Sorry I thought I corrected that in time, please update from above. The function called should be loadFrame. And you only need to call it once for the whole page.
Christopherous 5000
see the changes on the URL i provided. Still no luck, but very close. I believe it is because the variable "frames" is undefined (as firebug states). Check out the site and let me know. THanks so much Chris
JCHASE11
for this line: var frames = $('.frameToChange');it says $ is not a function
JCHASE11
just need to change $xxxxx to j$xxxxx, see changes to answer. Also keep in mind the function will load ALL iframes with the given class, so you only need one button. If you want a button for each iFrame is would be easier to pass the name of the iFrame and url to the function from each button.
Christopherous 5000
+1  A: 

You can either animate the iframe using jQuery or you could animate the content of the iframe.

The downside of doing anything with iframes is that they do not allow the script in the parent to communicate with the script running inside the iframe. It's called Cross Domain Restriction for protecting against malicious scripts from different domains.

Hence if you intend to animate the content of your iframe, you need to implement the animation as part of the page that loads inside the iframe. Otherwise to animate the iframe itself, jQuery already has some basic animation capabilities that are described here.

Am
at this point, the animation is secondary to the functionality. Great to know though!
JCHASE11
That's not necessarily true, if both the parent and iframe are on the same domain they can interact with each other. Also, if the iFrame is pulling in content from a subdomain of the same site you can force the domain to be the same as the parent by doing document.domain = 'mydomain.com' to match the parent.
Sam
good point. The iframe will be on the same domain (a subdomain) as the static page, so I will be able to animate it!
JCHASE11
+1  A: 

I use the following:

$('iframe').load(function() {
    $(this).animate({
        'height': this.contentWindow.document.body.scrollHeight + 'px'
    });
}); 
Sam
Sam, what would the HTML look like? I still dont see how this loads the iframe into another div dynamically.
JCHASE11