2012-04-16 17 views
26

Ho alcuni calcoli che si basano su window.innerHeight. Ma come ho scoperto, questo non è disponibile in alcun IE prima di IE9. Tutte le altre opzioni che ho visto non si avvicinano nemmeno alla figura che ottengo quando uso window.innerHeight.window.innerHeight ie8 alternative

Qualcuno ha un lavoro in giro?

+2

Questo problema è stato risolto in diverse librerie come [jQuery] (http://jquery.com), [Prototype] (http://prototypejs.org), [YUI] (http://developer.yahoo.com/yui/), [Closure] (http://code.google.com/closure/library) o [any of many others] (http://en.wikipedia.org)/wiki/List_of_Java Script_libraries). Piuttosto che risolverlo da solo, potresti prendere in considerazione l'idea di sfruttare il lavoro degli altri per concentrarti sulla scrittura di codice specifico per le tue esigenze. In alternativa, guarda come lo fanno e usa quel codice anche se non usi la libreria. –

+2

Questa risposta è stata fatta qualche tempo fa. Controlla [this] (http://stackoverflow.com/questions/5864467/internet-explorer-innerheight) – AurA

+0

Tranne che iOS '$ (window) .height()' e 'window.innerHeight' restituiscono valori diversi. Quindi direi che il problema deve ancora essere risolto in tali librerie e richieste di garanzie. – cjbarth

risposta

48

Si potrebbe desiderare di provare:

document.documentElement.clientHeight; 

In alternativa è possibile utilizzare jQuery's .height() metodo:

$(window).height(); 
+5

+1 'document.body.clientHeight' non ha funzionato per me in tutti gli scenari, ma ho trovato' $ (window) .height() 'per essere affidabile – BornToCode

+4

todo: leggi su' document.documentElement.clientHeight' – Dan

+4

Dan ha ragione, il vero equivalente a '$ (window) .height()' è 'document.documentElement.clientHeight', non' document.body.clientHeight'. Qui sta testando fiddle: http://jsfiddle.net/tzdcx/1/ o http://jsfiddle.net/tzdcx/1/show (funziona bene con IE7 e successivi) – Stano

14

Ho fatto un semplice spessore per IE8 e altri:

  • window.innerWidth
  • window.innerHeight
  • window.scrollX
  • window.scrollY
  • document.width
  • document.height

559 byte

(function(d,b){var c=b.documentElement;var a=b.body;var e=function(g,h,f){if(typeof g[h]==="undefined"){Object.defineProperty(g,h,{get:f})}};e(d,"innerWidth",function(){return c.clientWidth});e(d,"innerHeight",function(){return c.clientHeight});e(d,"scrollX",function(){return d.pageXOffset||c.scrollLeft});e(d,"scrollY",function(){return d.pageYOffset||c.scrollTop});e(b,"width",function(){return Math.max(a.scrollWidth,c.scrollWidth,a.offsetWidth,c.offsetWidth,a.clientWidth,c.clientWidth)});e(b,"height",function(){return Math.max(a.scrollHeight,c.scrollHeight,a.offsetHeight,c.offsetHeight,a.clientHeight,c.clientHeight)});return e}(window,document)); 

Per aggiornamenti, date un'occhiata a questo succo: yckart/9128d824c7bdbab2832e

(function (window, document) { 

    var html = document.documentElement; 
    var body = document.body; 

    var define = function (object, property, getter) { 
    if (typeof object[property] === 'undefined') { 
     Object.defineProperty(object, property, { get: getter }); 
    } 
    }; 

    define(window, 'innerWidth', function() { return html.clientWidth }); 
    define(window, 'innerHeight', function() { return html.clientHeight }); 

    define(window, 'scrollX', function() { return window.pageXOffset || html.scrollLeft }); 
    define(window, 'scrollY', function() { return window.pageYOffset || html.scrollTop }); 

    define(document, 'width', function() { return Math.max(body.scrollWidth, html.scrollWidth, body.offsetWidth, html.offsetWidth, body.clientWidth, html.clientWidth) }); 
    define(document, 'height', function() { return Math.max(body.scrollHeight, html.scrollHeight, body.offsetHeight, html.offsetHeight, body.clientHeight, html.clientHeight) }); 

    return define; 

}(window, document)); 
metodo
+0

Questo è estremamente utile! Grazie! – Luc

1

Javascript per ottenere la piena altezza della finestra ..:

window.outerHeight; 

metodi JavaScript per ottenere l'altezza completa del documento ..:

document.body.clientHeight; 

o

document.body.offsetHeight; 

metodi Javascript per ottenere l'altezza dell'area del documento visibile ..:

questa è l'altezza della finestra senza barre.

window.innerHeight; 

metodi di jQuery per ottenere visibile altezza area del documento e la finestra senza sbarre altezza zona troppo ..:

$(window).height(); 

o

$(window).outerHeight(); 

metodi di jQuery per ottenere l'altezza completa del documento ..:

$(document).height(); 

o

$(document).outerHeight(); 

questo è tutto, spero vi aiuterà :)

3

Uso seguente nel document.ready e definire in modo esplicito innerHeight.

window.innerHeight = window.innerHeight || document.documentElement.clientHeight 
1

Ho cercato su perché nessuna delle funzioni qui Pubblicato sembrava funzionare, ho sempre avuto la windowviewheight non i documenti a tutta altezza ...

questo funziona in tutti i browser che ho provato ... anche IExplore 8:

var D = document; 
var myHeight = Math.max(
    D.body.scrollHeight, D.documentElement.scrollHeight, 
    D.body.offsetHeight, D.documentElement.offsetHeight, 
    D.body.clientHeight, D.documentElement.clientHeight 
); 

alert(myHeight); 
+0

Dove tutto il resto è fallito, questo è stato tutto per me. Grazie! –