Detect scroll to bottom in an iframe?

2019-07-20 12:38发布

问题:

I'm using the following event to detect if the bottom of a page is reached when scrolling :

     $(window).scroll(function() {  
       if($(window).scrollTop() == $(document).height()-$(window).height())
         alert('Bottom reached');    
       }
     });

This works fine on a normal page, but now I want this to work on a page that is inside an iframe. This event must be declared inside the page in the iframe. But window points to the page that contains the iframe, so I can't use that.

Any ideas?

回答1:

var currAgreementTab = this.get('parentController').get('currAgreement'); var contractContainer = Ember.$('div#marginContractContainer'); var iframe = contractContainer.children(currAgreementTab.element)[0].contentWindow;

        if (iframe) {
            iframe.onscroll = function() {
                var scrolledHeight = Ember.$(iframe).height() === Ember.$(iframe.document).innerHeight() ? Ember.$(iframe).height() : Ember.$(iframe.document).innerHeight() - Ember.$(iframe).height();

                if (Ember.$(iframe).scrollTop()  === scrolledHeight) {
                    var currAgreementTab = that.get('parentController').get('currAgreement');
                    Ember.set(currAgreementTab, 'checkDisabled', false);
                }
            }
        }