javascript - 如何在动画滚动中将整个页面从链接滚动到 75%

标签 javascript jquery html scroll hyperlink

如何使用动画滚动中 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/

相关文章:

javascript - nodejs无法用momentjs解析日期

php - 如何让 jQuery 从 mysql_fetch_array 获取 PHP 参数?

javascript - 将图像扩展到完整的可用宽度和高度

javascript - node.js 需要文件夹中的所有文件

javascript - 鼠标悬停时更改 DIV 内容

javascript - 多个 Accordion 均默认打开选项卡

jquery - 切换具有类的元素($(this).next() 除外)

javascript - 当我使用history.go(-1)时,jQuery多选长度返回默认值;

css - 调整浏览器大小时显示右边距

javascript - jquery mobile,使用 .live() 或 .bind() 在导航上执行 ajax 的正确方法