tid=159& 本帖最後由 media 於 2014-12-10 19:08 編輯 f$ O8 U: |. I& ^9 h5 C ?
+ q3 s2 K) n' ~) ^1 R7 [最近用javascript取視窗的大小時,遇到不同瀏覽器無法偵測或不同的寬高的問題。後來找到下段這段程式碼,當用於點圖放大並顯示於前景中央位置時非常有用。
j" a* W' [9 G" Q: B, T- <script type="text/javascript">
' _$ ^6 L* \/ u/ F) T - <!-- l; X: X) U4 E* Q. b
- @" D( Q8 C$ U" e: G- var viewportwidth;
$ V/ `- L9 J, h3 }0 v - var viewportheight;
% L* f+ M9 y ^ - 7 ?" Y. v, U6 H0 l
- // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight+ ^ F% w: p* L, o# R
- - @3 `+ s. K4 r' t2 ]+ ~
- if (typeof window.innerWidth != 'undefined')# G6 U/ J( s o6 J( J' W% E
- {3 T1 ?5 y$ Y( a
- viewportwidth = window.innerWidth,
, C' C) ]; q2 z! |0 ~1 B - viewportheight = window.innerHeight
, {; z0 C7 k9 o6 I7 j - }. k4 Q+ m$ A$ u) z# @
-
% t( [5 ?1 Q# ^. c* j/ [ - // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)8 H4 Y* r5 q5 P8 W, ^& [: S* L
1 H+ p5 ~5 N6 N0 ], u4 \- else if (typeof document.documentElement != 'undefined'" `; ]( ~+ N. g4 _1 }
- && typeof document.documentElement.clientWidth !=1 H% y: \6 G T/ t8 f/ F0 o
- 'undefined' && document.documentElement.clientWidth != 0)# Q, `. w( H1 Z7 L; i! T
- { F$ g7 r A0 r/ S) y; x
- viewportwidth = document.documentElement.clientWidth,2 l, p) g* \5 l/ c* y
- viewportheight = document.documentElement.clientHeight. H) o) E2 _0 i% U/ L; J" L
- }+ `* c& V- ?' X5 c1 s! a
- ) H" N* R) T: s
- // older versions of IE
( m- [" _0 S; j8 C4 L+ n% H0 g, X | -
2 {9 M9 ]# _% V o+ z; Q! q - else
; \) A: }4 G& Z2 m - {; P- s9 _, g- t) N1 q
- viewportwidth = document.getElementsByTagName('body')[0].clientWidth,8 l& K o7 B% X
- viewportheight = document.getElementsByTagName('body')[0].clientHeight$ _9 G7 G: E4 O' h7 l# m; r
- }- L0 e+ n' [ P5 l0 X
- document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
8 `& m9 A" Q- r% [, N - //-->
u T2 `9 l$ b3 } - </script>
複製代碼 結果: 在google chrome 顯示結果
# J2 m) I2 g' M* B8 Q
0 T* A2 P7 _" G! G- @' I
2 P- R' X) ~9 O2 c& j3 w
來源: Get viewport size (width and height) with javascript r7 Q1 ?1 I' L T1 @4 X% Q
6 h* C4 _" I" f- M, Z
0 p8 `' p( {$ c2 x: A' h |