When the top button is clicked, the method is executed. The scrollTop attribute obtains the distance between the selected label and the scroll bar. When the bottom label is clicked, the method is executed. Offset() obtains the relative offset of the matching element in the current viewport
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Untitled Page</title> <script type="text/javascript" src=jquery-1.8.0.js></script> <script> $(document).ready(function () { //当点击顶部按钮的时候,执行方法,scrollTop属性获取选中标签距滚动条的距离。 $('#top').click(function () { $('html').animate( { scrollTop: '0px' }, 1000 ); }); //当点击底部标签时候,执行方法,其中offset()获取匹配元素在当前视口的相对偏移,返回的是一个对象,有两个属性top,left //animate,的第二个属性当然我们也可以设置'slow','normal'或'fast' $('#foot').click(function () { $('html').animate( {scrollTop:$('span').offset().top},1000 ); }); }); </script> </head> <body> <br /> <br /> <br /> <br /> <br /> <a href="#" id="foot">底部</a> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <a href="#" id="top">顶部</a> <br /> <br /> <br /> <br /> <br /> <span></span> </body> </html>