本帖最後由 media 於 2014-12-10 19:08 編輯 " ?4 U7 i, o) _, W! h
3 O2 J, C9 Y2 e. H& E/ @
最近用javascript取視窗的大小時,遇到不同瀏覽器無法偵測或不同的寬高的問題。後來找到下段這段程式碼,當用於點圖放大並顯示於前景中央位置時非常有用。- d- s/ P; Q# _3 A5 c5 l. \
- <script type="text/javascript">
2 _+ |9 j1 ~3 G - <!--
; j7 ^* N" w! M
8 n7 I1 t& n+ r5 j" ]. Z8 c6 y- var viewportwidth;3 d- u) A6 r5 _# F* Y
- var viewportheight;
2 h5 U B( ^/ k - " U; T! d; z; M2 ?% [
- // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight9 e3 N c: g8 a+ |8 B- C
-
, |1 e6 p, T, Q% J5 ` - if (typeof window.innerWidth != 'undefined')
- e9 H5 }. B# F& P3 u. D - {5 y h, |, ~0 U# q: q( k# E+ Q1 y
- viewportwidth = window.innerWidth,
1 P* Q1 @6 k+ Q j - viewportheight = window.innerHeight
+ N' W- `$ m2 ]# \4 K, A - }( p$ l! g6 |* H+ T! O& p9 s7 Q
-
; p: ~; M& g1 \9 v# ^5 z" v - // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document) r7 w# Y" ?- e2 t2 l$ X8 P" {
! B) o7 Q5 k8 p3 Y8 A( y0 l- else if (typeof document.documentElement != 'undefined'# s# q: B& a. }. S8 \7 s( t0 I
- && typeof document.documentElement.clientWidth !=
: ^( e& s+ u+ }* u" a4 p7 Y& N - 'undefined' && document.documentElement.clientWidth != 0)
* ]& E& n$ [% {( v8 b - {
( B9 {" R' z( V - viewportwidth = document.documentElement.clientWidth,) @) o: S# L) R3 }: g
- viewportheight = document.documentElement.clientHeight
$ M) z0 M3 ~, U3 w, q) P5 C - }. h& x8 V5 ]8 l9 U
-
( c& a" M* T) j, P3 Q - // older versions of IE
0 Q* `. K" u+ o2 i4 J -
7 Z( _; S. z V7 Z0 d - else
7 j- J7 F9 l5 n) c* {* x0 p - {( p+ E' d; x' A. V! T
- viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
1 w" q* S6 k/ h - viewportheight = document.getElementsByTagName('body')[0].clientHeight
# T. T2 M8 U U, F( g - }6 q9 V# i# l$ A8 L1 y
- document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');6 @3 o9 Q# f8 p8 X" b
- //--># L3 v- @& e$ t+ c& Z/ Y3 m
- </script>
複製代碼 結果: 在google chrome 顯示結果3 c1 a+ w Y0 d, }- h
3 b6 i! e- q) F$ H- H8 [" O3 z, l6 t4 m$ s
來源: Get viewport size (width and height) with javascript
; m; B4 Z8 ^4 f# y9 K
" p6 B* L8 p: W5 z
h3 o* W" c. n |
|