話不多說,請看代碼:
<!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><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>JS點擊按鈕到頁面最底部/返回頁面頂部代碼</title><style type="text/css"> #back-to-top{ position: fixed; bottom: 50px; right: 10%; }</style></head><body><div style="height: 3000px;"></div><div id="back-to-top"> <a href="#top" rel="external nofollow" >返回頂部</a></div><script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ //首先將#back-to-top隱藏 $("#back-to-top").hide(); //當(dāng)滾動條的位置處于距頂部100像素以下時,跳轉(zhuǎn)鏈接出現(xiàn),否則消失 $(function () { $(window).scroll(function(){ if ($(window).scrollTop()>100){ $("#back-to-top").fadeIn(100); } else { $("#back-to-top").fadeOut(100); } }); //當(dāng)點擊跳轉(zhuǎn)鏈接后,回到頁面頂部位置 $("#back-to-top").click(function(){ $('body,html').animate({scrollTop:0},"speed"); return false; }); }); }); </script></body></html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持武林網(wǎng)!
|
新聞熱點
疑難解答