當點擊頂部按鈕的時候,執行方法,scrollTop屬性獲取選中標簽距滾動條的距離,當點擊底部標簽時候,執行方法,其中offset()獲取匹配元素在當前視口的相對偏移
<!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>
新聞熱點
疑難解答