iframe Auto Adjust Height as content changes

前端 未结 4 1313
無奈伤痛
無奈伤痛 2020-12-01 09:36

I have an iframe as you can see on the following link;-

http://one2onecars.com

The iframe is the online booking in the centre of the screen. The problem I ha

4条回答
  •  时光取名叫无心
    2020-12-01 10:06

    use this script:

    $(document).ready(function () {
      // Set specific variable to represent all iframe tags.
      var iFrames = document.getElementsByTagName('iframe');
    
      // Resize heights.
      function iResize() {
        // Iterate through all iframes in the page.
        for (var i = 0, j = iFrames.length; i < j; i++) {
        // Set inline style to equal the body height of the iframed content.
          iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
        }
      }
    
     // Check if browser is Safari or Opera.
     if ($.browser.safari || $.browser.opera) {
        // Start timer when loaded.
        $('iframe').load(function () {
        setTimeout(iResize, 0);
        });
    
       // Safari and Opera need a kick-start.
       for (var i = 0, j = iFrames.length; i < j; i++) {
         var iSource = iFrames[i].src;
         iFrames[i].src = '';
         iFrames[i].src = iSource;
       }
     } else {
        // For other good browsers.
        $('iframe').load(function () {
        // Set inline style to equal the body height of the iframed content.
        this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
        });
      }
    });
    

    Note : use it on webserver.

提交回复
热议问题