How to get scrollTop of an iframe

后端 未结 10 1738
一生所求
一生所求 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条回答
  •  猫巷女王i
    2020-12-03 03:52

    I have faced the same problem in my iframe. My whole website comming white iframe and scrolling not working So I used this code and it's work fine.

    Step 1. put this code to your iframe

    var offsettop =  120;
    window.parent.postMessage({"setAnchor": offsettop}, "*");
    

    Step 2. put this code where your call you iframe

    window.addEventListener('message', function(event) {
      if(event.data['setAnchor']>0) {
        var offsetHeight =event.data['setAnchor'];
        jQuery('html, body').animate({
          scrollTop: offsetHeight
        }, 200);
      } 
    })
    

提交回复
热议问题