2012-05-22 3 views
0

몇 가지 계산을 td 위로 수행 한 후 원을 그리려합니다.Raphael을 사용하여 원을 그리는 중입니다. 동적 일 때 작동하지 않습니다. 동적 일 때 작동하지 않습니다.

$('#priority_one').width($('#learning_streams').width()); 
$('#priority_one').height($('#learning_streams').height()); 
var priority_one_paper = new Raphael('priority_one', $('#priority_one').width(), $('#priority_one').height()); 
var priority_one_circle = priority_one_paper.circle((pos.left).toFixed(0), (pos.top).toFixed(0), (width/2).toFixed(0)); 
priority_one_circle.attr('stroke','#000000'); 

을하지만 그것은 동적으로 만들려고 할 때 더 이상 작동합니다 (TD 변경은 사용자가 입력에 따라 없음) : 나는 다음과 같은 코드를 실행하면 작동합니다. 코드 :

function circlePriorityOne() { 
    //priority_one is a div absolutely positioned over a table called learning_streams 
    //sets size of priority_one based off the table learning_streams 
    $('#priority_one').width($('#learning_streams').width()); 
    $('#priority_one').height($('#learning_streams').height()); 


    //creates the 'paper' to draw the circle on 
    var priority_one_paper = new Raphael('priority_one', $('#priority_one').width(), $('#priority_one').height()); 

    var main = getMax(priority_one_count); //returns the id of the td to circle 
    var pos = $('#'+main).position(); 
    var width = $('#'+main).width(); 

    //using toFixed() to get rid of decimals 
    var priority_one_circle = priority_one_paper.circle((pos.left).toFixed(0), (pos.top).toFixed(0), (width/2).toFixed(0)); 
    priority_one_circle.attr('stroke','#000000'); 
} 

이 항목을 참조하십시오. 감사.

+0

"작동하지 않음"... 식사를 조리하지 않습니까? 차는 운전합니까? "안녕하세요"라고하지 않습니까? : D 뭐하는거야? 콘솔에 오류가 있습니까? 변수 (main, pos, width, ...)를 확인 했습니까? – Andreas

+0

오류가 없습니다. 그것은 원을 그려야합니다. 그렇지 않습니다. 나는 td의 위치가 테이블이 아닌 문서에 상대적으로 캡처되고 있다고 생각한다. 그것은 원의 위치에서 벗어날 것입니다. 나는 테이블 주위에 포장 된 div 태그에 상대적인 위치를 잡기 위해 그것을 변경하고있다. 그게 뭐가 달라지는 지 알게 될거야. – ahhchuu

+0

나머지 질문에 답하지 못했습니다. main, pos 및 width는 모두 예상 값을 나타냅니다. – ahhchuu

답변

1

네 말이 맞아. .position()은 부모 요소를 기준으로 현재 위치를 가져옵니다. 현재 위치를 얻는 대신 .offset()를 사용해야합니다

document

편집
내가 테스트하지 않았기 때문에 나는 그것을 아주 잘 모르겠어요하지만 당신이 원하는 일을해야한다^^

var td = $("#td"), 
    pos_td = td.offset(), 
    table = td.parents("table"), 
    pos_table = table.offset(); 

// td's position relative to table 
console.log("left: " + (pos_td.left - pos_table.left)); 
console.log("top: " + (pos_td.top - pos_td.top)); 
+0

부모 요소 (이 경우 테이블)에 상대적인 위치를 얻고 싶습니다. .position()은 나에게 문서와 관련된 위치를 어떤 이유로 든 제공했습니다. td 요소를 테이블의 자식으로 취급하지 않습니까? – ahhchuu

+0

테이블을 position : absolute로 변경했습니다. 이제 작동합니다. : P Andreas에게 도움을 주셔서 감사합니다. 나는 당신이 나를 올바른 방향으로 생각하게 만들 때 이것을 대답으로 설정할 것입니다. – ahhchuu

관련 문제