views:

27

answers:

1

I have an iFrame that when loaded stretches his whole length without scrollbars.
After 1 second I want to scroll to an anchor in the iFrame.

I think I need to get it's height?
How can I do this?

HTML:

<iframe id="help-frame" src="help.php" scrolling="no"><\/iframe>

JS:

$("#help-frame").load(function() {
    document.getElementById("help-frame").style.height = 
    document.getElementById("help-frame").contentWindow.document.body.offsetHeight + 'px';
    setTimeout(iScroll, 1000);
});
function iScroll() {
    var anchorH = $("#help-frame").contents().find("a.ordering").height();
    alert(anchorH);
    // smooth scroll to #ordering
}

HTML help.php:

<!-- added a class as well -->
<a name="ordering" class="ordering"></a>
<h2>Ordering</h2>
... long content ...
+1  A: 

You don't need to re-query help-frame within an event handler. this will refer the object of invocation.

$('#help-frame').load(function(){
    var self = $(this);
    setTimeout(function() {
       var pos = self.contents().find('a.ordering').offset();
       self.animate({scrollTop:  pos.top}, 1000);
    }, 1000);
});
jAndy
Thanks a ton Andy, a few more tricks for the bag. Because I rather want to animate the main page, not the iFrame content (no scrollbars, 100% height) I used $("html, body").animate()
FFish
Is there a way to find the name="ordering" instead of the class btw?
FFish
@FFish: if name is an attribue you can query it with `$('a[name=ordering]')`
jAndy