jquery 實現錨點滾動效果


jquery實現頁面內鏈接錨點跳轉平滑滾動效果

以下為完整演示代碼,測試環境:ie8、firefox和chrome。注意你的jquery.min.js文件調用。

 

<!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" /> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <title>jquery實現錨點跳轉平滑滾動效果</title> <script type="text/javascript" src="jquery.min.js"></script> <script type="text/JavaScript"> $(document).ready(function() {     $('a[href*=#]').click(function() {         if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {             var $target = $(this.hash);             $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');             if ($target.length) {                 var targetOffset = $target.offset().top;                 $('html,body').animate({                     scrollTop: targetOffset                 },                 1000);                 return false;             }         }     }); }); </script> </head>
<body>
<p>0</p> <p id="top">top</p> <p><a href="#bottom">bottom</a></p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p>0</p> <p id="bottom">bottom</p> <p><a href="#top">top</a></p> <p>0</p>
</body>
</html>

 

測試說以上代碼在Opera瀏覽器下有些問題,以下是網友提供的Opera下的補丁代碼:

$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
 
<a href="javascript:;" onClick="$('html,body').animate({scrollTop: 0}, 1000);">返回頂部</a>
<!--end-->
$(document).ready(function(){
$('.retop').css("cursor","pointer");
$('.retop').click(function(){
$('html,body').animate({scrollTop: 0}, 1000);});
});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM