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

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

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

[複製鏈接]
發表於 2014-12-10 17:42:11 | 顯示全部樓層 |閱讀模式
tid=159& 本帖最後由 media 於 2014-12-10 19:08 編輯 / i0 ~( _6 O9 |: T. Y/ q& x
8 e, B/ c1 f  D1 N
最近用javascript取視窗的大小時,遇到不同瀏覽器無法偵測或不同的寬高的問題。後來找到下段這段程式碼,當用於點圖放大並顯示於前景中央位置時非常有用。
& I! M& o0 Q  D, E& n# K8 Z1 J3 |
  1. <script type="text/javascript">" w7 z6 V7 e: [) t2 s" H
  2. <!--
    9 [# U) q( ?9 `
  3. - q% C& D, r& T. V& W
  4. var viewportwidth;
    8 z& I* s9 Z' A( l4 m! j: W
  5. var viewportheight;' W8 k* x+ E+ U& i
  6. , K( @) @6 T# `
  7. // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight- G: z$ N9 p& L+ A) d. R
  8. 4 O4 D0 a6 h  Z6 O9 r
  9. if (typeof window.innerWidth != 'undefined')+ V' K; Y7 r4 Q* w' A! C. z, W
  10. {+ N3 V  U) v( E# m
  11.       viewportwidth = window.innerWidth,; e+ I8 y0 T& J7 k2 h6 L0 B
  12.       viewportheight = window.innerHeight1 o4 I, ?7 Z2 N$ b8 q9 z% P- ~
  13. }9 j* x  l/ F, L3 @; H2 [
  14. 3 H. O  a; s# R: F& _
  15. // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)/ @# J7 z1 B4 }- ], f( P0 ~
  16.   i( A; ?! ^+ ~
  17. else if (typeof document.documentElement != 'undefined'
    $ @1 b* J/ P3 i* ?/ I6 M
  18.      && typeof document.documentElement.clientWidth !=' g* c& U; ]: `0 m7 |
  19.      'undefined' && document.documentElement.clientWidth != 0)
    ' F8 R0 z$ Y& z. s# t% i
  20. {) r6 T) U8 m6 a% C$ [8 N. o  m& A
  21.        viewportwidth = document.documentElement.clientWidth,+ J7 L3 _, H# \+ l" e
  22.        viewportheight = document.documentElement.clientHeight
    ) w! T; |# Y; j4 e: h
  23. }0 D/ G& _1 h2 C; t

  24. ) i# b8 }1 b  i, O* C& z5 W5 J+ h
  25. // older versions of IE
    & L! Z( g9 L, I

  26. 4 i" J5 I9 S5 B6 S6 \2 R& Y' Q
  27. else
    3 q% D) b6 f' h) H0 ~' e2 n8 g. v8 X
  28. {, w0 j  Y* c! `6 q* P3 T7 N/ z( E
  29.        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,2 }, {& B$ o. J+ c' l8 Q
  30.        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    5 {* t8 V# G" q0 w. A
  31. }
    , v. C4 z) |% ~9 B' _
  32. document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');' Z% @' f2 ]4 a  W; G1 ]0 ?8 f
  33. //-->
    ) s3 N: X3 o' e0 s4 x* x
  34. </script>
複製代碼
結果: 在google chrome 顯示結果2 U5 G$ n& f5 U4 o
1.png 8 q1 c  u. Z: f% X( Z6 o
1 N; m; V; ~) h# z3 Q$ S  U9 B. L
來源:
Get viewport size (width and height) with javascript

$ H+ }8 x/ ^& D% A
8 Z6 g- z9 W5 K! r( g& V

- k- E% p2 V( u, I- C; v  o' Z
回復

使用道具 舉報

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

本版積分規則



中文酷站排行榜

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

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

GMT+8, 2026-2-4 20:43 , Processed in 0.076352 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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