tid=159& 本帖最後由 media 於 2014-12-10 19:08 編輯
& k, j, S+ W$ U" e" Z1 ?
- o' D7 f9 n* Y* q( I最近用javascript取視窗的大小時,遇到不同瀏覽器無法偵測或不同的寬高的問題。後來找到下段這段程式碼,當用於點圖放大並顯示於前景中央位置時非常有用。; ~$ Z! q- h1 A$ L- w
- <script type="text/javascript">
+ L8 N( u* C+ A+ r9 J+ p - <!--5 \ O7 W; L5 l& Q
; N; b0 @; P# z- r2 L- var viewportwidth; H3 g" \% W& l+ D: B7 o
- var viewportheight;
& v+ o3 [ x* _7 u% f - . d+ F' M7 u# R& i9 m% ~" F* C* H
- // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
1 A) [1 r- [& l$ V/ } - 4 T& `+ D6 Z8 K- U% G) c
- if (typeof window.innerWidth != 'undefined')7 v3 p8 B i! ]& R+ c" W) w
- {/ t% d- K6 Y8 V0 W* d
- viewportwidth = window.innerWidth,
9 @9 k* x2 O. O% I% j - viewportheight = window.innerHeight
1 F0 H; X2 P9 n2 g$ c( G - }
9 Z* h! N: ^8 r+ p7 S -
7 e1 p0 S/ |6 ]1 W9 d# M& A; i - // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
0 I z7 R! c9 w" D - 6 F% l# ]% _' d0 A, F# D
- else if (typeof document.documentElement != 'undefined'
5 q1 B7 E5 o! p1 D7 C - && typeof document.documentElement.clientWidth !=
- b% Z& |" `: H8 D* ] - 'undefined' && document.documentElement.clientWidth != 0)0 z. j+ a8 s4 U: M; X% `
- {
5 e* j0 P9 p6 X/ |5 D - viewportwidth = document.documentElement.clientWidth,
{8 ~5 V7 D# ^$ ^# K - viewportheight = document.documentElement.clientHeight
! O. [/ L4 K; I! w( w7 ]/ q - }9 A' W0 v5 F: R6 F4 c
- ) t+ O0 Y5 c( A. B. z
- // older versions of IE
F9 [8 i4 @7 K( g5 x* y - 9 j; o* s2 x: o7 }( |% [
- else
7 `2 |- \7 E5 T - {
3 B% U8 a- Q* {% O b2 _: d8 J6 X2 a - viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
7 b x2 @) E% f2 K' i! [ W - viewportheight = document.getElementsByTagName('body')[0].clientHeight
( ]& ~- s+ T1 k8 f8 I - }
. F8 a6 t! W7 [2 ^' ? - document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');+ E( e: \# g: a# o
- //-->
( e) o8 C& [! g8 E9 K - </script>
複製代碼 結果: 在google chrome 顯示結果7 J% v7 x) ]8 c5 c" }2 {
0 k. ^# i0 B7 }$ j- l) h
5 t; n: J" {; @3 {來源: Get viewport size (width and height) with javascript
2 O5 C$ |7 R, a/ ]5 `, r8 T
" l- Z1 r% Y0 S2 d1 h* c& B j! q) o4 ?0 h t. i
|