跨浏览器方法确定Javascript中的垂直滚动百分比

maj*_*oat 42 javascript scroll cross-browser

如何找出用户在任何给定点移动的垂直滚动条的百分比?

当用户向下滚动页面时,很容易捕获"onscroll"事件,但是如何在该事件中找出它们滚动了多远?在这种情况下,特别是百分比是重要的.我并不特别担心IE6的解决方案.

是否有任何主要框架(Dojo,jQuery,Prototype,Mootools)以简单的跨浏览器兼容方式公开它?

干杯,

Phi*_*tts 91

2016年10月:修正.jsbin演示中的括号在答案中丢失了.哎呀.

Chrome,Firefox,IE9 +.在jsbin上现场演示

var h = document.documentElement, 
    b = document.body,
    st = 'scrollTop',
    sh = 'scrollHeight';

var percent = (h[st]||b[st]) / ((h[sh]||b[sh]) - h.clientHeight) * 100;
Run Code Online (Sandbox Code Playgroud)

作为功​​能:

function getScrollPercent() {
    var h = document.documentElement, 
        b = document.body,
        st = 'scrollTop',
        sh = 'scrollHeight';
    return (h[st]||b[st]) / ((h[sh]||b[sh]) - h.clientHeight) * 100;
}
Run Code Online (Sandbox Code Playgroud)

如果您愿意jQuery(原始答案):

$(window).on('scroll', function(){
  var s = $(window).scrollTop(),
      d = $(document).height(),
      c = $(window).height();

  var scrollPercent = (s / (d - c)) * 100;
  
  console.clear();
  console.log(scrollPercent);
})
Run Code Online (Sandbox Code Playgroud)
html{ height:100%; }
body{ height:300%; }
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

  • 为什么使用这么短的变量名?它使代码立即变得不可读。对我来说,它看起来像是缩小版的 JS。 (7认同)
  • 废话,这是如此直观,我不知道为什么我在这上面的stackoverflow ... (4认同)
  • 通过添加以下CSS代码,修复了[`Infinity`问题](/sf/ask/167099551/#comment97125199_8028584) html {高度:100%; }` (2认同)

Edu*_*rdo 23

我想我找到了一个不依赖于任何库的好解决方案:

/**
 * Get current browser viewpane heigtht
 */
function _get_window_height() {
    return window.innerHeight || 
           document.documentElement.clientHeight ||
           document.body.clientHeight || 0;
}

/**
 * Get current absolute window scroll position
 */
function _get_window_Yscroll() {
    return window.pageYOffset || 
           document.body.scrollTop ||
           document.documentElement.scrollTop || 0;
}

/**
 * Get current absolute document height
 */
function _get_doc_height() {
    return Math.max(
        document.body.scrollHeight || 0, 
        document.documentElement.scrollHeight || 0,
        document.body.offsetHeight || 0, 
        document.documentElement.offsetHeight || 0,
        document.body.clientHeight || 0, 
        document.documentElement.clientHeight || 0
    );
}


/**
 * Get current vertical scroll percentage
 */
function _get_scroll_percentage() {
    return (
        (_get_window_Yscroll() + _get_window_height()) / _get_doc_height()
    ) * 100;
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*ell 9

这应该可以解决问题,不需要库:

function currentScrollPercentage()
{
    return ((document.documentElement.scrollTop + document.body.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight) * 100);
}
Run Code Online (Sandbox Code Playgroud)

  • 嗯,刚刚测试过,它适用于Chrome,IE和Firefox.您使用的是官方最新版本的Chrome吗? (3认同)

tos*_*ske 7

这些在 Chrome 19.0、FF12、IE9 中对我来说非常有效:

function getElementScrollScale(domElement){
        return domElement.scrollTop / (domElement.scrollHeight - domElement.clientHeight);
    }

function setElementScrollScale(domElement,scale){
        domElement.scrollTop = (domElement.scrollHeight - domElement.clientHeight) * scale;
    }
Run Code Online (Sandbox Code Playgroud)


lua*_*ava 7

每个人都有很好的答案,但我只需要一个答案作为一个变量。我不需要事件侦听器,我只是想获取滚动百分比。这就是我得到的:

const scrolledPercentage = 
    window.scrollY / (document.documentElement.scrollHeight - document.documentElement.clientHeight)
Run Code Online (Sandbox Code Playgroud)

const scrolledPercentage = 
    window.scrollY / (document.documentElement.scrollHeight - document.documentElement.clientHeight)
Run Code Online (Sandbox Code Playgroud)
document.addEventListener("scroll", function() {
  const height = window.scrollY / (document.documentElement.scrollHeight - document.documentElement.clientHeight)

  document.getElementById("height").innerHTML = `Height: ${height}`
})
Run Code Online (Sandbox Code Playgroud)
.container {
  position: relative;
  height: 200vh;
}

.sticky-div {
  position: sticky;
  top: 0;
}
Run Code Online (Sandbox Code Playgroud)