简体   繁体   English

如何获取元素当前 position 相对于 window 的百分比?

[英]How do I get the percentage of an element's current position relative to the window?

I'm trying to return an element's position as a percentage based on where the element is in the window using jquery.我正在尝试根据元素在 window 中的位置使用 jquery 以百分比的形式返回元素的 position。

$(window).on('resize scroll', function() {
var docHeight = (function(){
              return Math.max(
                  $(document).height(),
                  $(window).height()
              );
            })();
var percent = (($(element).offset().top / docHeight) * 100);
console.log(percent);
});

I'm expecting the console to log the percentage of where the top of the element is in the window, ie if the element's top is at the bottom of window, the percentage is 0%, if the element's top is in the middle of the window, the percentage is 50%, etc. However, it is returning the position as a percentage in relation to the document and does not change when you scroll.我希望控制台记录元素顶部在 window 中的位置的百分比,即如果元素顶部位于 window 的底部,则百分比为 0%,如果元素顶部位于 window 的中间window,百分比为 50%,等等。但是,它以相对于文档的百分比形式返回 position,并且在您滚动时不会更改。

Probably wanna take a look at Element​.get​Bounding​Client​Rect() . 可能想看看Element .get Bounding Client Rect()

Returns information about an element's position. 返回有关元素位置的信息。

Using getBoundindClientRect to get the element's position and innerHeight to get the viewport height, you can calculate the element's relative position:使用getBoundindClientRect获取元素的 position 和innerHeight获取视口高度,可以计算出元素的相对 position:

$(window).scroll(function(){
    var elem = document.getElementById("myElement"),
        rect = elem.getBoundingClientRect(),
        pct  = (window.innerHeight-rect.top)/window.innerHeight;

    console.log(pct);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得内部SVG元素相对于外部SVG元素视口的位置? - How do I get an internal SVG element's position relative to the viewport of an outer SVG element? 如何从当前屏幕位置获取元素的偏移量? - How do I get the offset of an element from the current screen position? 如何获取元素相对于浏览器视口的顶部 position? - How to get an element's top position relative to the browser's viewport? 如何获得作为相对元素的子元素的DOM元素的鼠标位置? - How do I get the mouse position of a DOM element that is a child of a relative element? 如何获取 iframe 相对于顶部窗口视口的位置? - How to get the position of an iframe relative to the top window's viewport? 如何使用 nodejs readline 获取当前 cursor position 相对于 output - How do I get the current cursor position relative to the output with nodejs readline 如何相对于父级边框的外边缘定位元素? - how do I position an element relative to the outer edge of parent's border? 如何获得相对于当前帧的结束拖动位置? - How can I get the ending drag position relative to the current frame? 如何在`transform:scale()`之前获取元素的位置(相对于其容器)? - How can I get an element's position (relative to its container) before `transform: scale()`? 如何获取用户选择相对于文档/窗口的中心锚点位置(x,y)? - How can I get the user's selection's center anchor point position (x, y) relative to the document/window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM