js to get the distance of the browser scroll bar from the top

1. Related methods obtained by
 
jQuery jquery Get the height of the scroll bar

Get the height of the browser display area:
$(window).height();

Get the width of the browser display area:
$(window).width();

Get the document of the page Height:
$(document).height();

Get the document width of the page:
$(document).width();

Get the vertical height of the scroll bar to the top:
$(document).scrollTop();

Get the scroll bar to the left Vertical width:
$(document).scrollLeft();

Calculate element position and offset: $(id).offset();

The offset method is a useful method, which returns the offset information containing the first element of the collection . By default, the offset information is relative to the body.
The result contains two properties top and left.
offset(options, results)

options.relativeTop  
specifies the relative offset position of the calculated ancestor element. This element should be positioned relative or absolute. If omitted, it is relative to body.

Whether options.scroll  
counts scroll bars, default TRUE

options.padding  
counts padding, default false

options.margin
Whether the margin is counted, the default is true

options.border
Whether the border is counted, the default is true


2. Use js to get the relevant method
// go back to the top of the page

    $("#goTotop").click(function(){  
        $ ('body,html').animate({scrollTop:0},1500); //Click the button to go back to the top of the page  
    });  
      
    $(window).scroll(function() {  
        var yheight1=window.pageYOffset; //The distance of the scroll bar from the top  
        var yheight=getScrollTop(); //The distance of the scroll bar from the top  
        var height =document.documentElement.clientHeight//The size of the browser visualization window  
        var top=parseInt(yheight)+parseInt(height )-217;  
        var divobj=$(".kf");  
        divobj.attr('style','top:'+top+'px;');  
    })  
      
/**
 * Get the distance from the top of the scroll bar
 * @return {} supports IE6
 */  
function getScrollTop() {  
        var scrollPos;  
        if (window.pageYOffset) {  
        scrollPos = window.pageYOffset; }  
        else if (document.compatMode && document.compatMode != 'BackCompat')  
        { scrollPos = document.documentElement.scrollTop; }  
        else if (document.body) { scrollPos = document.body.scrollTop; }   
        return scrollPos;   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326218583&siteId=291194637