且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在页面加载时将iframe滚动到底部?

更新时间:2023-12-05 17:12:04

使用jQuery,您需要使用 .contents() 访问iframe内的任何内容。您还需要使用窗口加载事件或iframe的文档就绪事件。

With jQuery you need to use .contents() to access whatever's inside the iframe. You also need to use either the window load event, or the iframe's document ready event.

$(window).load(function ()
{
    var $contents = $('#frame').contents();
    $contents.scrollTop($contents.height());
});

演示: http://jsbin.com/ujuci5/2

我建议回去你提到的div结构,因为它听起来像你想要做的不需要iframe。你会发现使用div比使用iframe要少得多。

I would recommend going back to the div structure you mentioned, since it sounds like what you're trying to do doesn't require an iframe. You'll find that it's a lot less headache to work with a div than an iframe.

$(function ()
{
  var $mydiv = $('#mydiv');
  $mydiv.scrollTop($mydiv.height());
});

演示: http://jsbin.com/ujusa4/2


如何在页面加载后3秒启动滚动功能?

how can i launch the scroll function like 3 seconds after the page loads?

使用 setTimeout

$(window).load(function ()
{
    setTimeout(function ()
    {
        var $contents = $('#frame').contents();
        $contents.scrollTop($contents.height());
    }, 3000); // ms = 3 sec
});