52AV手機A片王|52AV.one

 找回密碼
 立即註冊
查看: 2227|回復: 0

[HTML] 取得目前可見視窗內容的尺寸--Javascript[含1張圖]

[複製鏈接]
發表於 2014-12-10 17:42:11 | 顯示全部樓層 |閱讀模式
tid=159& 本帖最後由 media 於 2014-12-10 19:08 編輯
/ R! V9 E$ o# N! _6 |) `- q( U* T
最近用javascript取視窗的大小時,遇到不同瀏覽器無法偵測或不同的寬高的問題。後來找到下段這段程式碼,當用於點圖放大並顯示於前景中央位置時非常有用。
; w: u8 A3 [3 \  ~0 n
  1. <script type="text/javascript">
    # l3 B! |4 ]+ N+ v4 N( n1 f: q
  2. <!--
    # z; e  M0 E1 p9 y  r

  3. " ?8 f7 s" i" P* K. D
  4. var viewportwidth;4 R9 e/ \& L( ?9 D1 D5 G; J( n+ ?
  5. var viewportheight;
    1 O% {1 V4 H2 s. P: S
  6. % Z, V+ i% c- n
  7. // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    9 x% E  |/ Y, S
  8. 1 c* m0 k$ g& K
  9. if (typeof window.innerWidth != 'undefined')
    . C& Q$ a* ]/ k; c9 L- S; I
  10. {
    & S) C: F1 c8 _
  11.       viewportwidth = window.innerWidth,) z3 W0 z/ r: h5 O
  12.       viewportheight = window.innerHeight
    / ?9 n  N0 u, u1 v( R8 G" B/ x
  13. }$ p3 F) C) _4 W( |1 p6 p

  14. , C- A1 K: R: i7 [: v
  15. // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    7 I/ d' P( b8 f$ S) Q1 S3 A
  16. / F9 ~: L9 [, P& i; p% F- Q3 F
  17. else if (typeof document.documentElement != 'undefined': [, D! k3 e# W7 _- j; b
  18.      && typeof document.documentElement.clientWidth !=/ s' X/ {; o$ J: x
  19.      'undefined' && document.documentElement.clientWidth != 0)
    , q+ K8 V# F* f' K; i* }" K* t
  20. {
    / w  c+ x' g! E: |5 x# h/ p* }4 K
  21.        viewportwidth = document.documentElement.clientWidth,/ V& N/ k' Y% V) @$ ]
  22.        viewportheight = document.documentElement.clientHeight
    * i- r: H  O/ ~- {% C
  23. }
    6 Y/ p8 A/ F' u# Q
  24. + C( `* {, F# M9 {
  25. // older versions of IE
    ( Q! @  T9 s; e' k: L
  26. 0 T. q& `) k8 H1 J% N
  27. else& H' P  R8 d( I7 N+ ~
  28. {. V* E# }; U( L
  29.        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
    - X# Q, M. A# s) _1 P/ o
  30.        viewportheight = document.getElementsByTagName('body')[0].clientHeight5 Q( m  S8 m9 G
  31. }4 z9 i6 ^4 D' q$ T8 j* K: p/ r& h
  32. document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');7 c( ^- x' B; `) ?- B
  33. //-->
    & J% u: V6 M( j# j& ~2 P
  34. </script>
複製代碼
結果: 在google chrome 顯示結果
9 }, r9 f( R5 t3 k* T 1.png
% r. o. w! O( s* M6 q5 m
5 L" T. H$ U: Y3 f2 x  P
來源:
Get viewport size (width and height) with javascript

& t# g( L: y6 C! d
4 i" F. M' B) h1 I) h) s! C6 J

3 K2 R& ]' Q' ^
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則



中文酷站排行榜

本區塊內容依據『電腦網路內容分級處理辦法』為限制級網站,限定年滿18歲以上或達當地國家法定年齡人士方可進入,且願接受本站各項條款,未滿18歲 謝絕進入瀏覽。為防範未滿18歲之未成年網友瀏覽網路上限制級內容的圖文資訊,建議您可進行網路內容分級組織ICRA分級服務的安裝與設定。 (為還給愛護 本站的網友一個純淨的論壇環境,本站設有管理員)

QQ|小黑屋|手機板| 52AV手機A片王

GMT+8, 2026-3-23 22:21 , Processed in 0.063072 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表