Find JavaScript scroll top property without using .scrollTop?

安稳与你 提交于 2019-12-12 02:53:28

问题


Is There a way to find scroll top property with JavaScript without using .scrollTop?


回答1:


EDITED - measuring position in scrollable div using temporary element (as first-child) - this should not interfere with any styles as it is used only for measurement time:

http://codepen.io/themeler/pen/yOjXGp

Scroll position is saved in data attribute.

$(function () {

    function scrollMeasurer($elem) {
        var $measurer     = $('<div/>'),
            initialOffset = 0

        // measuring function
        function measure ($target) {
            var $m = $measurer.clone(),
                position = 0
            // prepend measurer
            $target.prepend($m)
            // calc position
            position = Math.abs($m.position().top - initialOffset)
            // remove measurer
            $m.remove()
            // save scroll position in data-attribute
            $target.attr('data-scrolled-value', position)
            // return position
            return position
        }
        // init (calculate initial offset)
        initialOffset = measure($elem)
        // recount when initial offset is calculated
        measure($elem)

        // bind measure on scroll
        $elem.on('scroll', function (e) {
            measure($(this))
        })
    }

})



回答2:


You can use $("body").offset().top




回答3:


I'm not sure if I fully understand your question or what it is that you are trying to find as these two should work, but only if your target audience uses < IE9 would I go for option 2:

  1. window.pageYOffset
  2. (document.documentElement || document.body.parentNode || document.body).scrollTop

However, if you are trying to find the position of an element, read on.


function getOffsetY(elem) {

    var elem = document.body.querySelector(elem);

    return elem.getBoundingClientRect().top + window.pageYOffset - elem.clientTop;

}

Then getOffsetY('#header') or getOffsetY('.footer') will return the Y offset of any element passed in.

Here's an example:

var footerOffset = getOffsetY('.footer') + 'px';

Now footerOffset will give you the element's offset in px units.


1. window.pageYOffset

The amount of pixels the entire page currently has scrolled. You can also think of this as "How many pixels do I need to scroll up to get to the top of the page?"

2. getBoundingClientRect().top

The Element.getBoundingClientRect() method returns ... its position relative to the viewport.

Relative to the viewport is key here. The viewport is the area of the web page that is currently visible.

getBoundingClientRect().top will include the size of the border and padding in pixels.

3. clientTop

The width of the top border of an element in pixels

If you wish to not take the size of the border into the calculation, you can subtract this from our result by using clientTop, as it will return the size of the top border.




回答4:


I thik you can this jQuery API,

https://api.jquery.com/scrollTop/

$( "p:last" ).text( "scrollTop:" + p.scrollTop() );



来源:https://stackoverflow.com/questions/36671044/find-javascript-scroll-top-property-without-using-scrolltop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!