如何使用动画滚动中 75% 百分比的链接来滚动整个页面?
我希望它在我的页面上滚动 75%,并在代码中以百分比形式写入,同时按原样设置动画。
动画有效,但我无法以百分比方式滚动。
我的 JavaScript 查询代码:
function scrollTo75() {
var body = document.body,
html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
console.log(height);
$('html, body').animate({
scrollTop: height/100 - window.innerHeight/75
}, 200);
}
我的 body 代码:
<a href="javascript:void(0);" onmouseover="scrollTo75();" title="Scroll 75%">75%</a>
最佳答案
将 scrollTop: height/100 - window.innerHeight/75
更改为 scrollTop: height * .75
:
function scrollTo75() {
var body = document.body,
html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
console.log(height);
$('html, body').animate({
scrollTop: height * .75
}, 200);
}
编辑:如果您需要使用 75 而不是 0.75,则只需执行 scrollTop: height * (75/100)
关于javascript - 如何在动画滚动中将整个页面从链接滚动到 75%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59605409/