单击时从窗口顶部滚动到特定位置

Scroll to certain position from top of window on click

本文关键字:定位 位置 滚动 顶部 窗口 单击      更新时间:2023-09-26

有人知道使用JS点击链接时如何滚动到页面上的某个位置吗?例如,单击时,滚动到距离窗口顶部500像素的位置。

我正在使用ScrollMagic插件,我的网站内容被滚动位置激活,所以我不可能只使用锚链接。此外,它不能从当前位置偏移,因为这也不起作用。

有什么想法吗?

这应该在纯js:中完成任务

document.body.scrollTop = 500;

I reccomand doing by jQuery:)下面是对每个设备版本的操作

$(document).ready(function()  //When the page is ready, load function
{
    $("#some_id").click(function()  // When arrow is clicked
    {
        $("body,html").animate(
        {
            scrollTop : 500                       // Scroll 500px from top of body
        }, 400);  //how fast the scrolling animation will be in miliseconds
    });
});

这样的东西行吗?无论你连接到哪个链接,它都会让你平滑地滚动到那个位置。

$('a[href*=#]').click(function() {
    $('html, body').animate({scrollTop: 500}, 500);
}