如何使用jquery从内部滚动iframe?

时间:2010-05-18 15:34:52

标签: javascript jquery iframe scroll vertical-scrolling

我正在使用一个生成iframe的shadowbox来显示页面上的产品详细信息。因为详细信息页面可能很长,所以客户端需要一个“更多”按钮来向下滚动页面(显然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,但无济于事。有任何想法吗?谢谢!

4 个答案:

答案 0 :(得分:1)

像这样:(经过测试)

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

答案 1 :(得分:1)

这最终起作用了:

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

答案 2 :(得分:1)

在Chrome中,我必须专门选择body元素:

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

答案 3 :(得分:1)

在父javascript中创建一个函数,如下所示:

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

并在iframe中调用该函数,如下所示:

window.parent.scrollToPoint(top);

它应该在chrome中工作,而不是在Firefox中测试