我尝试使用jquery实现滚动条的自上而下和自下而上。最近我尝试了百分比方式。使用像素从上到下似乎没问题(意味着它可以工作),但是如果我将百分比设置为100,自下而上的滚动只能滚动部分内容。
$('#spnTop').on("click",function(){
var percentageToScroll = 100;
var percentage = percentageToScroll/100;
var height = jQuery(document).height();
var documentHeight = $(document).height();
var scroll = $(window).scrollTop();
alert(scroll);
var scrollAmount = Math.round((height) * percentageToScroll/ 100)-scroll;
//alert(point);
alert(scrollAmount);
$('html,body').animate({ scrollTop: scrollAmount }, 'slow', function () {
alert("reached top"); });
});
这里是 fiddle。 例如: percentageToScroll现在是100,但滚动结束时并没有完全完成。(从底部到顶部) 对于从上到下是100,那么它会完全滚动到页面底部。 我不确定如何使其可用。 谢谢。 Vicky
scrollTop()
。 - Jamesconsole.log(...);
而不是alert(...);
,因为弹出框会干扰你的代码。 - Richard de Wit