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

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

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

[複製鏈接]
發表於 2014-12-10 17:42:11 | 顯示全部樓層 |閱讀模式
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
  1. <script type="text/javascript">
    + L8 N( u* C+ A+ r9 J+ p
  2. <!--5 \  O7 W; L5 l& Q

  3. ; N; b0 @; P# z- r2 L
  4. var viewportwidth;  H3 g" \% W& l+ D: B7 o
  5. var viewportheight;
    & v+ o3 [  x* _7 u% f
  6. . d+ F' M7 u# R& i9 m% ~" F* C* H
  7. // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    1 A) [1 r- [& l$ V/ }
  8. 4 T& `+ D6 Z8 K- U% G) c
  9. if (typeof window.innerWidth != 'undefined')7 v3 p8 B  i! ]& R+ c" W) w
  10. {/ t% d- K6 Y8 V0 W* d
  11.       viewportwidth = window.innerWidth,
    9 @9 k* x2 O. O% I% j
  12.       viewportheight = window.innerHeight
    1 F0 H; X2 P9 n2 g$ c( G
  13. }
    9 Z* h! N: ^8 r+ p7 S

  14. 7 e1 p0 S/ |6 ]1 W9 d# M& A; i
  15. // 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
  16. 6 F% l# ]% _' d0 A, F# D
  17. else if (typeof document.documentElement != 'undefined'
    5 q1 B7 E5 o! p1 D7 C
  18.      && typeof document.documentElement.clientWidth !=
    - b% Z& |" `: H8 D* ]
  19.      'undefined' && document.documentElement.clientWidth != 0)0 z. j+ a8 s4 U: M; X% `
  20. {
    5 e* j0 P9 p6 X/ |5 D
  21.        viewportwidth = document.documentElement.clientWidth,
      {8 ~5 V7 D# ^$ ^# K
  22.        viewportheight = document.documentElement.clientHeight
    ! O. [/ L4 K; I! w( w7 ]/ q
  23. }9 A' W0 v5 F: R6 F4 c
  24. ) t+ O0 Y5 c( A. B. z
  25. // older versions of IE
      F9 [8 i4 @7 K( g5 x* y
  26. 9 j; o* s2 x: o7 }( |% [
  27. else
    7 `2 |- \7 E5 T
  28. {
    3 B% U8 a- Q* {% O  b2 _: d8 J6 X2 a
  29.        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
    7 b  x2 @) E% f2 K' i! [  W
  30.        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    ( ]& ~- s+ T1 k8 f8 I
  31. }
    . F8 a6 t! W7 [2 ^' ?
  32. document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');+ E( e: \# g: a# o
  33. //-->
    ( e) o8 C& [! g8 E9 K
  34. </script>
複製代碼
結果: 在google chrome 顯示結果7 J% v7 x) ]8 c5 c" }2 {
1.png
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
回復

使用道具 舉報

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

本版積分規則



中文酷站排行榜

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

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

GMT+8, 2025-12-6 19:16 , Processed in 0.068242 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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