How to scroll an iframe to the bottom when page loads?

前端 未结 3 2037
独厮守ぢ
独厮守ぢ 2020-12-18 04:28

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 wit

相关标签:
3条回答
  • 2020-12-18 04:40

    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());
    });
    

    Demo: http://jsbin.com/ujuci5/2


    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());
    });
    

    Demo: http://jsbin.com/ujusa4/2


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

    Use setTimeout.

    $(window).load(function ()
    {
        setTimeout(function ()
        {
            var $contents = $('#frame').contents();
            $contents.scrollTop($contents.height());
        }, 3000); // ms = 3 sec
    });
    
    0 讨论(0)
  • 2020-12-18 04:48

    If the current document is ready, this means that the DOM is ready, but the document inside the iframe has to be loaded too.

    <script type="text/javascript">
    $(document).ready(function() {
      $("#frame").load(function(){this.contentWindow.scrollBy(0,100000)});
    });
    </script>
    
    0 讨论(0)
  • 2020-12-18 04:53

    If you have access to the source code of the iframe contents, one easy trick is to add an <a name='end'>&nbsp; (or the last line of your content)</a> (not sure if you really need anything in the <a> tag).

    Add #end to the iframe source, e.g.

    http://www.yourdomain.com/file.php#end
    

    and it will automatically scroll to the end, no javascript/jquery required

    0 讨论(0)
提交回复
热议问题