2010-10-04 88 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