메뉴 건너뛰기

조회 수 7297 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

// 데스크톱에 뛰운 브라우저 창의 전체크기

var windowWidth = window.outerWidth;

var windowHeight = window.outerHeight;

 

//Internet Explorer 8, 7, 6, 5, 은 

//outerWidth, outerHeight프로퍼티들이 지원되지 않음. 비슷한 기능이 없는걸로 확인.

 

 

// 데스크톱에 뛰운 브라우저 창의 위치 ( 파이어폭스, 사파리, IE10+, 크롬 )

var windowX = window.screenX;

var windowY = window.screenY;

 

//For Internet Explorer 8, 7, 6, 5, 사파리, IE8이하버전, 오페라

var windowX = window.screenLeft;

var windowY = window.screenTop; 

 

// HTMl문서가 표시되는 화상 표시 영역인 뷰포트(viewport)의 크기

// 이것은 브라우저 창 크기에서 메뉴, 툴바, 스크롤바 등의 크기를 뺀 나머지

var viewportWidth = window.innerWidth;

var viewportHeight = window.innerHeight;

 

//For Internet Explorer 8, 7, 6, 5:​ 

var viewportWidth​ = document.documentElement.clientWidth

var viewportHeight​ = document.documentElement.clientHeight

//or

var viewportWidth​ =​ document.body.clientWidth

var viewportHeight =​ document.body.clientHeight

 

 

// 수평, 수직 스크롤바의 위치를 표시

// 문서 좌표와 창 좌표를 상호 변환하는데 사용.

// 이값들은 화면의 좌측 상단 모서리에 문서의 어느 부분이 위치하는지 나타냄

var horizontalScroll = window.pageXOffset;

var verticalScroll = window.pageYOffset;

 

//For Internet Explorer 8, 7, 6, 5:​ 

var horizontalScroll ​ = document.documentElement.scrollLeft

var verticalScroll ​ = document.documentElement.scrollTop

//or

var viewportWidth​ =​ document.body.scrollLeft

var viewportHeight =​ document.body.scrollTop 

 

 

 

※ 위에서 살펴본 프로퍼티는 읽기전용 프로퍼티임.

 

화면 좌표(screen coordinates)는 데스크톱 상에서 브라우저 창이 떠 있는 곳의 위치를 나타내며 데스크톱의 좌측 상단 모서리에서 상대적으로 계산

창 좌표(window coordinates)는 웹 브라우저의 뷰포트(viewport, 화상표시 영역) 안의 위치를 나타내며 뷰포트의 좌측 상단 모서리에서 상대적으로 계산

문서 좌표(document coordinates)는 HTML문서 안의 위치를 나타내며 문서의 좌측 상단 모서리에서 상대적으로 계산

 

 

인터넷 익스플로러(For Internet Explorer 8, 7, 6, 5) 는 이런 창 위치와 크기에 관련 프로퍼티들을 HTML 문서의 <body> 부분에 두었다.

더 혼란스러운 것은, IE 6에서 <!DOCTYPE> 선언부가 있는 문서를 출력할때 이 프로퍼티들을 document.body 대시 document.documentElement 엘리먼트에 둔다는 것이다.

 

다행이도 IE 9버전이상에서는 창 위치와 크기에 관련 프로퍼티​들이 Window객체를 통해 지원한다.

 

 

다음으로 살펴볼 코드는 

IE6도 포함하여 브라우저의 종류에 상관없이 뷰포트의 크기와 스크롤바의 위치, 그리고 화면위치를 알애내기 위한 메서드들을 가진 

Geometry객체를 정의한것이다.

 

 


 [코드] Geometry.js

Colored By Color Scripter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/** 
 * 브라우저 종류에 관계없이 창 위치와 크기 알아내기
 * Geometry.js:  창, 문서의 위치와 크기를 알기 위한 이식 가능한 함수들.
 * 
 * 이 모듈은 창, 문서의 위치와 크기를 알기 위한 함수들을 정의한다.
 * 
 * getWindowX/Y()  : 화면 상에서 브라우저 창이 뜨워진 위치를 반환.
 * getViewportWindth/Height() : 브라우저 뷰포트 영역의 크기를 반환.
 * getDocumentWidth/Height() : 문서의 크기를 반환.
 * getHorizontalScroll() : 수평 스크롤바의 위치를 반환.
 * getVerticalScroll() : 수직 스크롤바위 위치를 반환.
 * 
 * 브라우저의 종류에 상관없이 브라우저 창의 크기를 반환하기 위한 방법은 존재하지 않는다.
 * (IE8이하버전들) 따라서 getWindowWidth/Height() 함수가 빠져있음을 주의하라.
 */


var Geometry = {};

//데스크톱에 띄운 브라우저 창의 위치
if (window.screenLeft) { // IE 등의 브라우저에서는
    Geometry.getWindowX = function() { return window.screenLeft; };
    Geometry.getWindowY = function() { return window.screenTop; };
} else if (window.screenX) { // 파이어폭스 등의 브라우저에서는
    Geometry.getWindowX = function() { return window.screenX; };
    Geometry.getWindowY = function() { return window.screenY; };
}


if(window.innerWidth) { //IE를 제외한 모든 브라우저에서는
    Geometry.getViewportWidth = function() { return window.innerWidth; };
    Geometry.getViewportHeight = function() { return window.innerHeight; };
    Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
    Geometry.getVerticalScroll = function() { return window.pageYOffset; };
} else if (document.documentElement && document.documentElement.clientWidth) {
    // 이들 함수는 DOCTYPE이 존재할때의 IE6을 위한것.
    Geometry.getViewportWidth = 
        function() { return document.documentElement.clientWidth; };
    Geometry.getViewportHeight = 
        function() { return document.documentElement.clientHeight; };
    Geometry.getHorizontalScroll = 
        function() { return document.documentElement.scrollLeft; };
    Geometry.getVerticalScroll =
        function() { return document.documentElement.scrollTop; };

} else if (document.body.clientWidth) {
    // DOCTYPE 이 없을때의 IE 6을 위한것
    Geometry.getViewportWidth = 
        function() { return document.body.clientWidth; };
    Geometry.getViewportHeight = 
        function() { return document.body.clientHeight; };
    Geometry.getHorizontalScroll = 
        function() { return document.body.scrollLeft; };
    Geometry.getVerticalScroll =
        function() { return document.body.scrollTop; };
}

//문서의 전체 크기를 반환
if(document.documentElement && document.documentElement.scrollWidth) {
    Geometry.getDocumentWidth =
        function() { return document.documentElement.scrollWidth; };
    Geometry.getDocumentHeight = 
        function() { return document.documentElement.scrollHeight; };
} else if (doxument.body.scrollWidth) {
    Geometry.getDocumentWidth =
        function() { return document.body.scrollWidth; };
    Geometry.getDocumentHeight = 
        function() { return document.body.scrollHeight; };
}

 

 

[속성 설명]

int scrollHeight, scrollWidth

엘리먼트의 전체높이와 폭을 픽셀 단위로 나타낸다. 

엘리먼트에 스크롤바가 있으면(예를 들어 CSS overflow 속성때문에) 이 프로퍼티의 값은 엘리먼트가 화면에 보이는 부븐의 크기를 알려주는 offsetHeight나 offsetWidth프로퍼티의 값과 다르다. 비표준 프로퍼티이긴 하지만 잘 지원된다.  

 

 [코드] GeometryExam.html

Colored By Color Scripter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!doctype html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>브라우저 종류에 관계없이 창 위치와 크기 알아내기</title>
  <script type="text/javascript" src="Geometry.js"></script>
  <script>
    document.write("Geometry.getWindowX():"+Geometry.getWindowX()+"<br/>");
    document.write("Geometry.getWindowY():"+Geometry.getWindowY()+"<br/>");
    document.write("Geometry.getViewportWidth():"+Geometry.getViewportWidth()+"<br/>");
    document.write("Geometry.getViewportHeight():"+Geometry.getViewportHeight()+"<br/>");
    document.write("Geometry.getDocumentWidth():"+Geometry.getDocumentWidth()+"<br/>");
    document.write("Geometry.getDocumentHeight():"+Geometry.getDocumentHeight()+"<br/>");
    document.write("Geometry.getHorizontalScroll():"+Geometry.getHorizontalScroll()+"<br/>");
    document.write("Geometry.getVerticalScroll():"+Geometry.getVerticalScroll()+"<br/>");

  </script>
 </head>
 <body>
 </body>
</html>

 


 

 

 

 [코드] HTML 문서가 표시되는 뷰포트(viewport)의 크기 정보를 가지고 있는 객체를 반환하는 함수 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Return the viewport size as w and h properties of an object
function getViewportSize(w) {
    // Use the specified window or the current window if no argument
    w = w || window;  

    // This works for all browsers except IE8 and before
    if (w.innerWidth != null) return {w: w.innerWidth, h:w.innerHeight};

    // For IE (or any browser) in Standards mode
    var d = w.document;
    if (document.compatMode == "CSS1Compat")        return { w: d.documentElement.clientWidth,
                 h: d.documentElement.clientHeight };

    // For browsers in Quirks mode
    return { w: d.body.clientWidth, h: d.body.clientWidth };
}

 

 

  

 [코드] 수평, 수직 스크롤바의 위치의 정보를 가지고 있는 객체를 반환하는 함수

Colored By Color Scripter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Return the current scrollbar offsets as the x and y properties of an object
function getScrollOffsets(w) {
    // Use the specified window or the current window if no argument
    w = w || window;

    // This works for all browsers except IE versions 8 and before
    if (w.pageXOffset != null) return {x: w.pageXOffset, y:w.pageYOffset};

    // For IE (or any browser) in Standards mode
    var d = w.document;
    if (document.compatMode == "CSS1Compat")
        return {x:d.documentElement.scrollLeft, y:d.documentElement.scrollTop};

    // For browsers in Quirks mode
    return { x: d.body.scrollLeft, y: d.body.scrollTop };
}

 

 

 


List of Articles
번호 제목 날짜 조회 수
247 활용예제 : 체크박스 전체선택 전체해제 /라디오버튼 2014.03.01 7020
246 현재시간 기준 날짜 계산 2016.09.21 6103
245 현재 날짜, 시간 ( Month + 1 에 대해서 ) 2021.03.25 218
244 핸드폰 번호 일부 마스킹크 작업 (정규식 이용) 2015.06.19 10740
243 해상도에 따라 배경 바꾸기 2014.03.01 6083
242 함수의 arguments 를 이름(key)으로 접근하기 2016.09.21 5976
241 한글 짜르기 2019.01.16 1178
240 한글 또는 영문만이 존재하는지 체크 2019.01.16 1163
239 풍선도움말 2019.01.16 1167
238 폼안에 태그명, 함수명 같을때 오류 2021.03.26 214
237 페이지 이동 2021.03.26 190
236 페이지 로드 할때 컨트롤에 포커스 주기 2015.02.03 8500
235 패스워드, 확인패스워드가 맞는지 체크 2023.01.12 129
234 팝업창 차단 "허용 메시지" 2018.09.28 1738
233 팝업창 맨위로 올라오게 하기 2021.03.26 848
232 팝업창 가운데 띄우기 2016.12.23 6702
231 특정부위 마우스 오버시 설명을 보여주는 소스 2014.03.17 8690
230 특정 부분 인쇄 자바스크립트 2019.01.16 1579
229 특정 HTML DOM 엘레멘트로 스크롤 이동하기 2016.09.09 7075
228 텍스트박스(input type = "text") 숫자 증가, 감소 시키기 - 쇼핑몰 주문 수량 file 2015.06.19 12272
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved