簡體   English   中英

如何使用jquery從內部滾動iframe?

[英]How do you scroll an iframe from within using jquery?

我正在使用一個影子框,該影子框會生成一個iframe以在頁面上顯示產品詳細信息。 由於詳細信息頁面可能會很長,因此客戶端需要一個“更多”按鈕來向下滾動頁面(顯然,iframe右側的滾動條是不夠的)。

這是我為了使iframe滾動而嘗試的代碼:

$(document).ready(function()
{
$(".moreButton img").click(function() {
    scrollbottom();
});
});

function scrollbottom() {
var x = 250; // this number is a temporary placeholder
var t = 500;
$("iframe").animate({ scrollTop: x }, t);
}

我也嘗試使用body代替iframe,但無濟於事。 有任何想法嗎? 謝謝!

像這樣:(已測試)

$("iframe").contents().children().animate({ scrollTop: x }, t);

在Chrome中,我必須專門選擇body元素:

$('#my-iframe').contents().find('body').animate({scrollTop:90},500);

最終工作:

$('html,body').animate({ scrollTop: x }, t);

在父javascript中創建一個函數,如下所示:

function scrollToPoint(top) {
    $("html, body").animate({ scrollTop: top }, "slow")
}

然后在iframe中調用該函數,如下所示:

window.parent.scrollToPoint(top);

它應該在chrome中工作,尚未在Firefox中測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM