简体   繁体   English

全页平滑滚动

[英]Full Page Smooth Scrolling

I'm using this code to use smooth scrolling in my website ( demo ): 我正在使用以下代码在我的网站( demo )中使用平滑滚动:

$("#click-me").click(function(){
  $('html,body').animate({
    scrollTop: window.screen.availHeight
  }, 200);
});

I'm trying to scroll so exactly the height of the page. 我正在尝试如此精确地滚动页面的高度。 However, it seems to scroll past this point. 但是,似乎已经超过了这一点。 I've tried putting in "100%" as a value, but it didn't work. 我尝试将“ 100%”作为值,但没有用。

What is causing this problem, and what should I do to fix it? 是什么导致此问题,我应该怎么做才能解决它?

Thanks! 谢谢!

It's working correctly but unless you add this (or account for padding and margin on body), the result is slightly off. 它可以正常工作,但是除非您添加此内容(或考虑到正文的填充和边距),否则结果会略有不同。

body{
  padding:0;
  margin:0;
}

http://jsfiddle.net/bb9ux/2/ (non-working version: http://jsfiddle.net/bb9ux/3/ ) http://jsfiddle.net/bb9ux/2/ (非工作版本: http//jsfiddle.net/bb9ux/3/

Scroll to particular div: 滚动到特定的div:

$("#click-me").click(function(){
  $('html, body').animate({
        scrollTop: $('#scroll-here').offset().top
    }, 2000);
});

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM