2014-10-10 2 views
0

두 요소 사이의 거리를 계산하는 방법, 이렇게?두 요소 사이의 거리를 계산하는 방법, 이렇게 하시겠습니까?

은 내가 id="element_bottom"id="ref_box"

사이의 거리를 계산하려면이 내가 id="element_bottom"id="ref_box" 사이에 계산 된 거리를이 코드를 적용 할 수있는 방법을 mouseid="element_bottom"

사이에 계산 된 거리 바이올린입니까?

고맙다는 아마 이런

http://jsfiddle.net/t5Kts/808/

(function() { 

    var mY, distance_bottom, 
     $distance_bottom = $('#distance_bottom span'), 
     $element_bottom = $('#element_bottom');    

    function calculatedistance_bottom(elem , mouseY) { 
     return Math.floor(Math.sqrt(Math.pow(mouseY - (elem.offset().top+(elem.height()/2)), 2))); 
    } 

    $(document).mousemove(function(e) {   
     mY = e.pageY; 
     distance_bottom = calculatedistance_bottom($element_bottom , mY); 
     $distance_bottom.text(distance_bottom);   
    }); 
})(); 
+0

여기 : http://jsfiddle.net/t5Kts/809/ – kraysak

답변

0

를,이 작업을 시도 할 수 있습니다 :

DEMO

(function() { 
    var btm = $('#element_bottom'), 
     ref = $('#ref_box'), 
     refTop = ref.position().top; 


    $(window).scroll(function(){ 
     var btmTop = btm.position().top; 
     $('#distance_bottom span').text(Math.abs(refTop-btmTop)); 

    });  

})(); 
0

뭔가를해야한다 : 거리에 대한 당신의 참조가 각 요소의 꼭대기가 있다고 가정

$('.foo').offset().top - $('.bar').offset().top 
+0

내 피들을 업데이트 해 주실 수 있습니까? –

관련 문제