所以我试图让百分比向下滚动。所以说我向下滚动一半页面,我会返回50%。现在这是我尝试这样做的方式
var a = $("#container").scrollTop;
var b = $("body").scrollHeight - $("body").clientHeight ;
var c = a / b ;
console.log(c);
我的标记
<body style="height: 100%;">
<div id="container" style="height: 100%;">
<!--Bunch of h1s here-->
</div>
</body>
现在我跑了。在控制台中,我得到NaN
....即使我向下滚动......任何想法?
答案 0 :(得分:4)
scrolltop()
是一个函数,你的逻辑有点偏。试试这个:
$(window).on('scroll', function () {
var $this = $(this),
$body = $('body');
var percent = Math.round($this.scrollTop() / ($body.height() - $this.height()) * 100);
});
答案 1 :(得分:0)
scrollTop是一种方法而非属性。
<script>
var a = $("#container").scrollTop();
var b = document.body.scrollHeight - document.body.clientHeight ;
var c = a / b ;
console.log(c);
</script>
虽然我不相信这是你正在寻找的功能。
答案 2 :(得分:0)
如果你想使用jquery就会这样。希望它有所帮助
var a = $("#container").scrollTop();
var b = $('body').prop('scrollHeight') - $('body').prop('clientHeight') ;
var c = a / b ;
console.log(c);