单击链接时,jQuery 从页面顶部滚动 x 像素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12719741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:57:08  来源:igfitidea点击:

jQuery Scroll x pixels from top of the page when clicking a link

jqueryscrollsmooth-scrolling

提问by user1715848

I have a link which changes its vertical position on scrolling. I want to go to a certain postion (smoothly) on my page when I click this link, which is located exactly 1080px from the top of the page.

我有一个链接,它在滚动时更改其垂直位置。当我单击此链接时,我想(平滑地)转到我页面上的某个位置,该链接距离页面顶部正好 1080 像素。

I can't get it work, hope someone can help me out.

我无法得到它的工作,希望有人可以帮助我。

The link:

链接:

<img src="img/clickme.png" style="cursor: pointer;" class="scroll"/>

The script:

剧本:

<script type="text/javascript">
$(document).ready(function() {
$(".scroll").click(function(event){     
$('html, body').animate({scrollTo({ top: '+1080px',}, 800);
});
});
</script>

回答by Blender

Try this instead. Your syntax was off a little:

试试这个。你的语法有点不对:

$(document).ready(function() {
    $(".scroll").click(function(event){
        $('html, body').animate({scrollTop: '+=1080px'}, 800);
    });
});

Demo: http://jsfiddle.net/m4Aaz/2/

演示:http: //jsfiddle.net/m4Aaz/2/