Astuce Auto resize Iframe

Répondre
ctmoi
Membre
Messages : 71
Inscription : lun. nov. 22, 2010 7:53 pm
Localisation : Brioude (43)
Contact :

Astuce Auto resize Iframe

Message par ctmoi »

Bonjour,

Venant de refaire mon site et étant embêté avec le dimensionnement des Iframes qui sont fixes et peuvent parfois faire des pages longues pour rien du tout (particulièrement sur les stats NOAA).
J'ai fini par trouver un bout de code pour que la longueur s'adapte automatiquement. J'ai testé sur les dernières versions de IE, FF, Chrome et Opera.

A mettre dans <head> (changer indexInlineFrame1 par le nom de votre iframe) :

Code : Tout sélectionner

<script type="text/javascript">
/* free code from dyn-web.com */

function getDocHeight(doc) {
    doc = doc || document;
    // from http://stackoverflow.com/questions/1145850/get-height-of-entire-document-with-javascript
    var body = doc.body, html = doc.documentElement;
    var height = Math.max( body.scrollHeight, body.offsetHeight, 
        html.clientHeight, html.scrollHeight, html.offsetHeight );
    return height;
}

function setIframeHeight(id) {
    var indexInlineFrame1 = document.getElementById(id);
    var doc = indexInlineFrame1.contentDocument? indexInlineFrame1.contentDocument: indexInlineFrame1.contentWindow.document;
    indexInlineFrame1.style.visibility = 'hidden';
    indexInlineFrame1.style.height = "10px"; // reset to minimal height in case going from longer to shorter doc
    indexInlineFrame1.style.height = getDocHeight( doc ) + "px";
    indexInlineFrame1.style.visibility = 'visible';
}
</script>
Puis modiifer l'Iframe:

Code : Tout sélectionner

<iframe name="indexInlineFrame1" id="indexInlineFrame1"  src="./page1.html" onload="setIframeHeight(this.id)" style="position:absolute;text-align:center;left:167px;top:115px;width:920px;" scrolling="no"></iframe>
Les sources sont dans le code et pour ma part j'ai trouvé l'info ici:
http://www.willmaster.com/library/tutor ... hanges.php

Bonne journée.
VP2 - Graphweather 2.0.321 - XNet Meteo 2011-10-14a - win10 x64
Image
Avatar de l’utilisateur
paaubert
Membre
Messages : 2624
Inscription : dim. janv. 17, 2010 7:16 pm
Localisation : 1555 Villarzel - Switzerland
Contact :

Re: Astuce Auto resize Iframe

Message par paaubert »

Salut,
merci pour l'astuce, c'est très intéressant.
Je vais essayer ça.

A+
_________________________________________________________________________________________________________
Station Vantage Pro2+ avec station agricole - Weatherlink 6.0.3 - GraphWeather 3.0.15b - Cumulus 1.9.4 - Windows W10 ultimate x64
http://www.boock.ch/meteo-villarzel.php
Image
Répondre