How to get an element's top position relative to the browser's viewport?

后端 未结 12 955
你的背包
你的背包 2020-11-29 17:17

I want to get the position of an element relative to the browser\'s viewport (the viewport in which the page is displayed, not the whole page). How can this be done in JavaS

相关标签:
12条回答
  • 2020-11-29 17:31

    Edit: Add some code to account for the page scrolling.

    function findPos(id) {
        var node = document.getElementById(id);     
        var curtop = 0;
        var curtopscroll = 0;
        if (node.offsetParent) {
            do {
                curtop += node.offsetTop;
                curtopscroll += node.offsetParent ? node.offsetParent.scrollTop : 0;
            } while (node = node.offsetParent);
    
            alert(curtop - curtopscroll);
        }
    }
    

    The id argument is the id of the element whose offset you want. Adapted from a quirksmode post.

    0 讨论(0)
  • 2020-11-29 17:31

    You can try:

    node.offsetTop - window.scrollY

    It works on Opera with viewport meta tag defined.

    0 讨论(0)
  • 2020-11-29 17:33
    var element =  document.querySelector('selector');
    var bodyRect = document.body.getBoundingClientRect(),
        elemRect = element.getBoundingClientRect(),
        offset   = elemRect.top - bodyRect.top;
    
    0 讨论(0)
  • 2020-11-29 17:33
    function inViewport(element) {
        let bounds = element.getBoundingClientRect();
        let viewWidth = document.documentElement.clientWidth;
        let viewHeight = document.documentElement.clientHeight;
    
        if (bounds['left'] < 0) return false;
        if (bounds['top'] < 0) return false;
        if (bounds['right'] > viewWidth) return false;
        if (bounds['bottom'] > viewHeight) return false;
    
        return true;
    }
    

    source

    0 讨论(0)
  • 2020-11-29 17:37

    jQuery implements this quite elegantly. If you look at the source for jQuery's offset, you'll find this is basically how it's implemented:

    var rect = elem.getBoundingClientRect();
    var win = elem.ownerDocument.defaultView;
    
    return {
        top: rect.top + win.pageYOffset,
        left: rect.left + win.pageXOffset
    };
    
    0 讨论(0)
  • 2020-11-29 17:38

    The function on this page will return a rectangle with the top, left, height and width co ordinates of a passed element relative to the browser view port.

        localToGlobal: function( _el ) {
           var target = _el,
           target_width = target.offsetWidth,
           target_height = target.offsetHeight,
           target_left = target.offsetLeft,
           target_top = target.offsetTop,
           gleft = 0,
           gtop = 0,
           rect = {};
    
           var moonwalk = function( _parent ) {
            if (!!_parent) {
                gleft += _parent.offsetLeft;
                gtop += _parent.offsetTop;
                moonwalk( _parent.offsetParent );
            } else {
                return rect = {
                top: target.offsetTop + gtop,
                left: target.offsetLeft + gleft,
                bottom: (target.offsetTop + gtop) + target_height,
                right: (target.offsetLeft + gleft) + target_width
                };
            }
        };
            moonwalk( target.offsetParent );
            return rect;
    }
    
    0 讨论(0)
提交回复
热议问题