本帖最後由 media 於 2014-12-10 19:08 編輯
9 l: {) x# g* h! q# E. h6 B- O5 T8 ~
H5 \2 M2 N9 c& X( E8 `最近用javascript取視窗的大小時,遇到不同瀏覽器無法偵測或不同的寬高的問題。後來找到下段這段程式碼,當用於點圖放大並顯示於前景中央位置時非常有用。
) N0 {3 _% u) {8 U- <script type="text/javascript">
" m2 N7 e) M! d/ P) i- S - <!--
2 n! y# E x ^0 j3 ?% W/ B
- s( N4 h. C: a& q& O. H/ T- var viewportwidth;
9 L+ e% f8 n% \3 `* v - var viewportheight;. Z; a2 M8 g: O/ X: K
- ! I4 r; X! U3 K
- // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight5 _% _; B7 U2 r4 l- t
-
( E! X n7 O$ t, ~3 M2 U - if (typeof window.innerWidth != 'undefined')
2 E: a; ^) a! W - {; A7 `# F) P2 \- j- \: O* v9 I
- viewportwidth = window.innerWidth,
7 l. R, M% G5 u- M9 i/ s - viewportheight = window.innerHeight/ f6 }: Z8 @# |% u6 T: \
- }
0 f1 z( Z% q' u, H( W -
. n. D0 Q+ g: b! h$ Z1 `% o( d - // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
; l& w+ e" n% o - 8 E/ w* ]. X* c5 s# k
- else if (typeof document.documentElement != 'undefined'0 \- H9 v7 _' p" b, g$ L: d
- && typeof document.documentElement.clientWidth !=- {0 z5 B' |0 R/ o
- 'undefined' && document.documentElement.clientWidth != 0)
; A2 w0 o1 y; P- q3 f: ?1 H - {
7 Z6 h' `; @5 {: j" p - viewportwidth = document.documentElement.clientWidth,
5 K* s) k0 c+ W. q5 a - viewportheight = document.documentElement.clientHeight) T! u' r; }" v) s- t2 G1 y
- }/ d1 J: U+ s1 v
- ) ]6 M2 h( a& W( q5 B$ e( H! v
- // older versions of IE5 l0 k* m/ a1 J a4 ~$ B+ c
-
1 b7 E/ z0 {2 Y - else
( e& p7 j$ `5 ?) M$ w4 `' c) ~ - {3 P& }- n O) w; o% X2 r" Z, E) s7 G
- viewportwidth = document.getElementsByTagName('body')[0].clientWidth,0 `5 w# n; R8 ~( G9 B. d9 B9 a
- viewportheight = document.getElementsByTagName('body')[0].clientHeight2 w2 I6 S) g9 |& R) ?& \9 C
- }' S' s3 Y, ]/ w+ A
- document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
8 Y, w6 b# X- D* T. c - //-->0 w6 q4 k" o) z+ j4 y" \
- </script>
複製代碼 結果: 在google chrome 顯示結果1 B. U* I8 F, V3 l7 B. v( X) p3 ~
. h8 m" W* @8 {' y& A$ v* S1 ?
. o% l: k1 i# Z t8 N0 D$ r
來源: Get viewport size (width and height) with javascript+ b: k3 g |0 ]6 e
) j! m- k {. \! C" i, p6 r* `
0 r( G7 [/ u; G& h! P) k3 [3 _
|
|