How to get scrollTop of an iframe

后端 未结 10 1582
一生所求
一生所求 2020-12-03 03:33

jQuery\'s scrollTop returns null when window is an iframe. Has anyone been able to figure out how to get scrollTop of an iframe?

more info:

my script is runn

相关标签:
10条回答
  • 2020-12-03 04:00
    $('#myIframe').contents().scrollTop()
    
    0 讨论(0)
  • 2020-12-03 04:03

    I found this question while trying to SET scrollTop inside an iframe... not exactly your question (you wanted to GET) but here's a solution inside iframes for people that also end up here trying to SET.

    If you want to scroll to the top of the page this will NOT work inside an iframe:

    $("html,body").scrollTop(0);
    

    However, this will work:

    document.getElementById("wrapper").scrollIntoView();
    

    Or the equivilent with jQuery:

     $("#wrapper")[0].scrollIntoView();
    

    For this markup (contained inside an iframe):

    <html>
      <body>
        <div id="wrapper">
          <!-- lots of content -->
        </div>
      </body>
    </html>
    
    0 讨论(0)
  • 2020-12-03 04:05

    Good old javascript:

    scrollTop = document.getElementById(IFRAME_ID).contentWindow.document.body.scrollTop;
    
    0 讨论(0)
  • 2020-12-03 04:05

    or use this one

    sample.showLoader = function() {
    // show cursor wait
    document.getElementsByTagName('body')[0].style.cursor = 'wait';
    var loader = $('#statusloader');
    // check if we're runnign within an iframe
    var isIframe = top !== self;
    if (isIframe) {
        var topPos = top.pageYOffset + 300;
        // show up loader in middle of the screen
        loader.show().css({
            top : topPos,
            left : '50%'
        });
    } else {
        // show up loader in middle of the screen
        loader.show().css({
            top : '50%',
            left : '50%'
        });
    }
    };
    
    0 讨论(0)
提交回复
热议问题