2010-10-04 89 views
0

我有一个iFrame,当加载时伸展他的整个长度没有滚动条。
1秒钟后,我想滚动到iFrame中的锚点。jQuery的内容()找到iFrame中锚链接的页面高度

我想我需要得到它的高度?
我该怎么做?

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

你并不需要一个事件处理程序中重新查询help-framethis将引用调用对象。

$('#help-frame').load(function(){ 
    var self = $(this); 
    setTimeout(function() { 
     var pos = self.contents().find('a.ordering').offset(); 
     self.animate({scrollTop: pos.top}, 1000); 
    }, 1000); 
}); 
+0

谢谢安迪,这个包还有更多的技巧。因为我宁愿要动画主页,而不是iFrame内容(没有滚动条,100%的高度)我使用$(“html,body”)。animate() – FFish 2010-10-04 09:33:00

+0

有没有办法找到name =“ordering”的类btw? – FFish 2010-10-04 11:33:41

+2

@FFish:如果name是一个属性,你可以用$('a [name = ordering]')来查询它。 – jAndy 2010-10-04 11:53:03