tid=159& 本帖最後由 media 於 2014-12-10 19:08 編輯
3 W; T- s% r- p0 R
; F5 i0 h6 A, t+ T! U( l5 a最近用javascript取視窗的大小時,遇到不同瀏覽器無法偵測或不同的寬高的問題。後來找到下段這段程式碼,當用於點圖放大並顯示於前景中央位置時非常有用。
% k' B H! H! ^2 a% l7 F- <script type="text/javascript">
& r9 F, ~$ h/ g - <!--% _4 m$ ]9 v9 V5 U
/ j% _- s& Z9 n- J/ o4 Y- var viewportwidth;( `6 w9 W5 z! i2 L5 ~5 A
- var viewportheight;
6 U! s" I3 c+ m - , u. Z$ N; j! B& x& v
- // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight' c: Z( A: X6 c8 d( b* D
- + G J: M9 t5 K% S5 A
- if (typeof window.innerWidth != 'undefined')
7 _0 V0 `0 Y" m. Z$ q J" `/ x" N - {
$ ~9 h5 e# J- G% g+ `$ q9 i - viewportwidth = window.innerWidth,4 c) H w6 h3 D; \1 _3 L3 A# S
- viewportheight = window.innerHeight" X' [ a! O$ d
- }
! N- M0 q, D: S& V - 5 G7 x) u/ X# P/ o+ R* k8 V
- // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)8 G) e5 c% _6 F8 P' U: u' h' c
- & ~- H6 `" e G v% a \
- else if (typeof document.documentElement != 'undefined'
% u" r4 r$ l9 g7 | - && typeof document.documentElement.clientWidth !=6 W* j9 n1 A8 R3 s( b- Q
- 'undefined' && document.documentElement.clientWidth != 0)8 M( U, ^6 d; p" }
- {, H/ I7 e. [$ t( a( }
- viewportwidth = document.documentElement.clientWidth,
- t- `: W, Y: G - viewportheight = document.documentElement.clientHeight7 W4 O) D1 E# V7 ~
- }
, ^! ~4 J7 _ s! M! a -
4 Z) x3 v8 a+ ^1 g+ l) `; T - // older versions of IE+ U" M2 r/ \9 w& H* F6 i" \
- ! w9 W( X, i( V8 c! d2 y. H5 w
- else
; `1 W6 `2 c* Q* K! R$ C9 B - {( a8 i/ {' }3 U+ C% ^
- viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
' z' z5 R! k$ m8 l - viewportheight = document.getElementsByTagName('body')[0].clientHeight+ h ~/ }, E2 C9 `( |# F
- }3 @; p$ N6 X2 q; C
- document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
) Z9 \+ `+ k z; {* Y1 [) t - //-->) m; Z/ N1 [) U7 _! S- A$ c
- </script>
複製代碼 結果: 在google chrome 顯示結果) }& `$ u) ]0 I! |2 b( l r% X
0 D$ A$ b: `# l% K' ^* R
& Z1 j6 f( A3 ?. Y4 ^8 I來源: Get viewport size (width and height) with javascript
1 c! V) ~. o9 ^$ c' F7 }' P- V/ d( m% ^8 L+ O/ V! t; |1 ]
$ x1 v1 U1 y# _& f' S1 b |