如何在页面加载时将iframe滚动到底部? [英] How to scroll an iframe to the bottom when page loads?

查看:458
本文介绍了如何在页面加载时将iframe滚动到底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我就像在网上的每一个问题,尝试像50差异方法,首先我试图滚动div - 然后当它没有工作,我试图iframe文件与div并滚动iframe。什么都行不通!

I was like on every question about this on the net, tried like 50 diff methods, first i tried to scroll the div - then when its not worked i've tried to iframe the file with the div and scroll the iframe.. nothing just works!

查看我设置的最后一个脚本:

Look on the last script i have set up:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var t = 100; //arbitrary time in ms
$("#frame").animate({ scrollTop: $("#frame").height()}, t);
});
</script>

<center><iframe id="frame" src="xxx.php" frameborder="0" width="630px" height="500px"></iframe></center>

我尝试了#frame和frame而没有(#),两者都是(document).ready和没有它。
似乎没有什么东西可以在页面上运行并且它让我疯狂。

I tried both #frame and frame without the (#), both with (document).ready and without it. Nothing seems to work on the page and its driving me nutz.

谢谢。 :)

推荐答案

使用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
});

这篇关于如何在页面加载时将iframe滚动到底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆