tid=159& 本帖最後由 media 於 2014-12-10 19:08 編輯 ; {" U' `- E; I+ F# |. n
5 R. I2 a/ i+ r1 L5 j最近用javascript取視窗的大小時,遇到不同瀏覽器無法偵測或不同的寬高的問題。後來找到下段這段程式碼,當用於點圖放大並顯示於前景中央位置時非常有用。) W) V* P: r8 ?8 y) B& o
- <script type="text/javascript">
5 F. L: w5 S2 U8 M - <!--0 J+ C( l, W' K4 B: r2 t
! F* o# r* K1 p$ y- var viewportwidth;
: [" D0 O9 S9 l" C& r; M7 P - var viewportheight;6 S1 l Y ^. ^
-
- x \( o8 X* g( N0 }* B - // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
* R) [/ s" Q _& {5 P# B - + J( L# p9 }2 @* U
- if (typeof window.innerWidth != 'undefined')
e/ n, W' V2 I, u; Y - {, B+ K) F& B" g* d
- viewportwidth = window.innerWidth,
9 B3 Q: y7 H. _: ] - viewportheight = window.innerHeight0 k6 |4 y; @3 s( p& i
- }
z1 e/ X9 z% U4 k/ n9 q- K -
! x5 A3 `6 L$ U, P - // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
7 `1 U& \8 {: A- ]
% _, m& I8 G4 _& j/ I: y: C- else if (typeof document.documentElement != 'undefined'
3 K% X7 ]) U2 ~ - && typeof document.documentElement.clientWidth !=
& S: f" C% p$ q - 'undefined' && document.documentElement.clientWidth != 0)
( x" H- A# l8 @' Z - {
0 z3 x- z6 t5 |, t8 _! j! p/ p6 D - viewportwidth = document.documentElement.clientWidth," _3 k% {+ J9 Q. S
- viewportheight = document.documentElement.clientHeight
* H! }% V- f9 r/ A- _8 I" P - }
+ Z% n9 v4 y4 h8 Y8 X -
, m$ [* c& i+ [ - // older versions of IE
' I3 b9 n- N2 c+ ~ -
' R" P0 H3 b3 r3 Z - else
9 \ i3 l* K+ l( u$ s+ J4 ] - {
0 O+ g$ x3 _* U# N - viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
0 b* Q, U2 b3 F# |" q, T; r2 N* a+ o - viewportheight = document.getElementsByTagName('body')[0].clientHeight2 G5 \& W6 t( {1 F
- }
* s- d+ G* s0 I- T, R - document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
8 P* L2 S/ X5 B3 f3 e! \ - //-->% G3 B, L8 r5 z3 W( Z& m" s
- </script>
複製代碼 結果: 在google chrome 顯示結果* @) p& R, \ q
$ A8 |; g& k# o, h2 `
" O/ h- U$ S# d& B; t來源: Get viewport size (width and height) with javascript
* Q- ~ C. c( V# F; I1 \; C, G2 {# O2 z$ t
. A/ [1 m- _$ e |