Programing

요소의 하단 및 오른쪽 위치 가져 오기

lottogame 2020. 10. 17. 08:57
반응형

요소의 하단 및 오른쪽 위치 가져 오기


다음과 같이 창 내에서 요소의 위치를 ​​얻으려고합니다.

var link = $(element);

var offset = link.offset();
var top = offset.top;
var left = offset.left;
var bottom = $(window).height() - link.height();
bottom = offset.top - bottom;
var right = $(window).width() - link.width();
right = offset.left - right;

그런데 -아래와 오른쪽이 앞에 있는데 ... 왜 이래요? 숫자가 정확하기 때문에 마이너스가 아니어야합니다.


대신에

var bottom = $(window).height() - link.height();
bottom = offset.top - bottom;

왜 안하는거야

var bottom = $(window).height() - top - link.height();

편집 : 당신의 실수는 당신이하고 있다는 것입니다

bottom = offset.top - bottom;

대신에

bottom = bottom - offset.top; // or bottom -= offset.top;

var link = $ (element);
var offset = link.offset ();

var top = offset.top;
var left = offset.left;

var bottom = top + link.outerHeight ();
var right = left + link.outerWidth ();

// Returns bottom offset value + or - from viewport top
function offsetBottom(el, i) { i = i || 0; return $(el)[i].getBoundingClientRect().bottom }

// Returns right offset value
function offsetRight(el, i) { i = i || 0; return $(el)[i].getBoundingClientRect().right }

var bottom = offsetBottom('#logo');
var right = offsetRight('#logo');

그러면 뷰포트의 상단과 왼쪽에서 요소의 정확한 가장자리까지의 거리를 찾을 수 있습니다. 따라서 로고가 350px이고 왼쪽 여백이 50px라고 가정하면 변수 'right'는 400의 값을 유지합니다. 이는 더 많은 패딩이 있더라도 요소의 가장자리에 도달하는 데 걸린 실제 거리 (픽셀 단위)이기 때문입니다. 또는 오른쪽 여백.

box-sizing CSS 속성이 border-box로 설정된 경우 기본 콘텐츠 상자로 설정된 것처럼 계속 작동합니다.


이를 위해 .position ()사용할 수 있습니다.

var link = $(element);
var position = link.position(); //cache the position
var right = $(window).width() - position.left - link.width();
var bottom = $(window).height() - position.top - link.height();

다음은 페이지의 모든 클래스 또는 ID의 객체를 반환하는 jquery 함수입니다.

var elementPosition = function(idClass) {
            var element = $(idClass);
            var offset = element.offset();

            return {
                'top': offset.top,
                'right': offset.left + element.outerWidth(),
                'bottom': offset.top + element.outerHeight(),
                'left': offset.left,
            };
        };


        console.log(elementPosition('#my-class-or-id'));

나는 생각한다

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<div>Testing</div>
<div id="result" style="margin:1em 4em; background:rgb(200,200,255); height:500px"></div>
<div style="background:rgb(200,255,200); height:3000px; width:5000px;"></div>

<script>
(function(){
    var link=$("#result");

    var top = link.offset().top; // position from $(document).offset().top
    var bottom = top + link.height(); // position from $(document).offset().top

    var left = link.offset().left; // position from $(document).offset().left
    var right = left + link.width(); // position from $(document).offset().left

    var bottomFromBottom = $(document).height() - bottom;
    // distance from document's bottom
    var rightFromRight = $(document).width() - right;
    // distance from document's right

    var str="";
    str+="top: "+top+"<br>";
    str+="bottom: "+bottom+"<br>";
    str+="left: "+left+"<br>";
    str+="right: "+right+"<br>";
    str+="bottomFromBottom: "+bottomFromBottom+"<br>";
    str+="rightFromRight: "+rightFromRight+"<br>";
    link.html(str);
})();
</script>

결과는

top: 44
bottom: 544
left: 72
right: 1277
bottomFromBottom: 3068
rightFromRight: 3731

내 크롬 브라우저에서.

When the document is scrollable, $(window).height() returns height of browser viewport, not the width of document of which some parts are hiden in scroll. See http://api.jquery.com/height/ .

참고URL : https://stackoverflow.com/questions/9872128/get-bottom-and-right-position-of-an-element

반응형