2011-12-24 3 views
0

마우스의 좌표를 얻으려면 e.pageY와 e.pageX를 사용합니다. 클래스 또는 ID는 어떻게됩니까? 모양은 다음과 같습니다 :요소의 좌표 얻기

$(this).pageY; 또는 이와 비슷한? 할 수 있을까요? 감사.

답변

2

당신은 position 또는 offset 당신이 원하는에 따라 사용할 수 있습니다. position은 오프셋 부모를 기준으로 요소의 위치를 ​​알려주고 offset은 문서와 관련된 위치를 제공합니다. http://jsfiddle.net/ZBeWq/4/

2

offset() 메서드는 요소의 위치 (위쪽과 왼쪽)를 반환합니다.

var offset = $("selector").offset(); 
offset.top // Top position, relative to the top of the page 
offset.left // Left position, relative to the left of the page 
1

사용은 여기

$(".divItem").click(function(){ 
    alert("Left is : "+$(this).offset().left) 
}); 

은 샘플 오프셋 . 그렇지 않으면 상위 요소에 상대적인 좌표를 가져 오려면 position() 메서드를 사용할 수 있습니다. 이 두 함수는 요소의 위쪽과 왼쪽을 반환합니다.

var absolute_coordinates = $('#mydiv').offset(); // top and left with respect to document 

    var relative_coordinates = $('#mydiv').position(); //top and left with respect to parent 

    alert('#mydiv is ' + relative_coordinates.top 
      + 'px below from parent and absolute_coordinates.top + 
     'px below from document'); 
2

당신이 내용을 나타내는 전체 페이지 즉, 문서의 일부 요소 상대의 좌표를 얻을 완, 당신은 offset() 방법을 사용할 수 있습니다 :