Dynamically resize iframe

后端 未结 5 943
执笔经年
执笔经年 2020-12-30 05:32

Situation: I\'m working on a responsive design that involves the typical HTML/CSS combo. Everything is working nicely except in one case where there is an i

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 06:30

    For those using Prestashop, this is how I used the code.

    In the cms.tpl file I added the below code:

    {if $cms->id==2}
    
    
    
    {/if}

    Then created a new js file: formj.js and added the below code:

    $(function () {
    
        //setup these vars only once since they are static
        var $myIFRAME   = $(".iframe-class"),//unless this collection of elements changes over time, you only need to select them once
            ogWidth     = 970,
            ogHeight    = 800,
            ogRatio     = ogWidth / ogHeight,
            windowWidth = 0,//store windowWidth here, this is just a different way to store this data
            resizeTimer = null;
    
        function setIFrameSize() {
            if (windowWidth < 480) {
    
                var parentDivWidth = $myIFRAME.parent().width(),//be aware this will still only get the height of the first element in this set of elements, you'll have to loop over them if you want to support more than one iframe on a page
                    newHeight      = (parentDivWidth / ogRatio);
    
                $myIFRAME.addClass("iframe-class-resize").css({ height : newHeight, width : parentDivWidth });
            } else {
                $myIFRAME.removeClass("iframe-class-resize").css({ width : '', height : '' });
            }
        }
    
        $(window).resize(function () {
    
            //only run this once per resize event, if a user drags the window to a different size, this will wait until they finish, then run the resize function
            //this way you don't blow up someone's browser with your resize function running hundreds of times a second
            clearTimeout(resizeTimer);
            resizeTimer = setTimeout(function () {
                //make sure to update windowWidth before calling resize function
                windowWidth = $(window).width();
    
                setIFrameSize();
    
            }, 75);
    
        }).trigger("click");//run this once initially, just a different way to initialize
    });
    

提交回复
热议问题