本帖最後由 media 於 2014-12-10 19:08 編輯
3 n" ^: v: [ J( \' r% z6 [$ `* `
最近用javascript取視窗的大小時,遇到不同瀏覽器無法偵測或不同的寬高的問題。後來找到下段這段程式碼,當用於點圖放大並顯示於前景中央位置時非常有用。; U6 p1 @* M ]
- <script type="text/javascript">3 Y. L5 A! H! O* D. s$ s
- <!--
" o. ` [8 z9 D& e( u. y; x2 d
# M5 i% ?1 |+ K6 ^) Y- var viewportwidth;
* F- X* @, X3 K; S6 F, W - var viewportheight;
% w: ]: ]3 s4 C9 U8 L9 | -
$ S1 u3 J- W9 H" U - // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
% |" |7 Q1 y: L7 Q$ z -
3 U! A6 _& D6 C - if (typeof window.innerWidth != 'undefined')
' r/ ?& x% Z4 ?% C9 M" c* ~2 A$ R9 M N - {# f; y% W" k5 Y6 A$ _5 T Q8 n
- viewportwidth = window.innerWidth,! A( g/ b6 n x5 F
- viewportheight = window.innerHeight
9 D" h7 `/ e4 h0 Z" o* H- t; I - }
- r) R( x) _$ O: C- A, D/ ~ -
3 W3 ?) A h2 Z, | - // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document), h2 @, [+ x+ a/ `. p% c* C
- * x: c* u2 ~" l
- else if (typeof document.documentElement != 'undefined'
- }8 \7 @8 i. r) k - && typeof document.documentElement.clientWidth !=, Q" |1 L5 P6 j" `5 x7 [' _9 K" B
- 'undefined' && document.documentElement.clientWidth != 0)1 D( ~5 K D8 \6 S) J" t p
- {
3 u! E! k+ V6 V1 \1 N8 {1 m: K# k - viewportwidth = document.documentElement.clientWidth,
* k# S0 h* r; T) b - viewportheight = document.documentElement.clientHeight
7 L; _! p; b. h. I - }# O9 v+ S- Z& r; L; e
-
, P/ {3 n8 m$ D3 ~! K) [ - // older versions of IE3 J' o# p7 R3 u3 S
- - r. `" z8 r! i- [0 q# G8 z# T
- else' [& G4 B& q4 Z
- {/ T& l# _+ _( C6 m4 K& ?1 z9 ~. r2 D' r
- viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
) ~- J0 |2 c. u+ z - viewportheight = document.getElementsByTagName('body')[0].clientHeight
" k! Y- X+ i5 y3 l- Y* @* [: U1 g - }
& E/ Z% _3 ]: ] - document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
; U7 m; e5 v& ?+ ?( u - //-->
! O2 S; `7 Y5 Z& h - </script>
複製代碼 結果: 在google chrome 顯示結果3 Z. y+ O, ]3 w
* c" w0 f2 n. n( k
( A | e; X1 t4 w/ P3 o5 v
來源: Get viewport size (width and height) with javascript' f9 @( f( }$ }' V
6 G1 p1 j) {, N- ~: }2 B/ ?- Q2 ^
& \3 G) D' |2 e6 z- S |
|