jQuery contents() find the page height of anchor link in iFrame
I have an iFrame that when loaded stretches his whole length without scrollbars.
After 1 second I want to sc开发者_运维百科roll 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 ...
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);
});
精彩评论