回到頂部和返回首頁jquery插件


 第一種方法:在網頁<body></body>之間插入:

  1. <!--返回頂部開始--> 
  2. <div id="full" style="width:50px; height:95px; position:fixed; left:50%; top:425px; margin-left:493px; z-index:100; text-align:center; cursor:pointer;"><a><img src="images/up.png" border=0 alt="返回頂部"></a></div> 
  3. <script src="/js/gotop.js" type="text/javascript"></script> 
  4. <!--返回頂部結束--> 

 修改圖片路徑和js路徑,保證圖片和JS都 能打開就行了 !

JS文件下載:點擊下載

第二種方法:回到頂部功能:在網頁<body></body>之間插入:

  1. <!--引入jquery庫文件--> 
  2. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>  
  3. <!--功能代碼開始--> 
  4. <script>  
  5. (function() { 
  6.     var $backToTopTxt = "回頂部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body")) 
  7.         .text($backToTopTxt).attr("title", $backToTopTxt).click(function() { 
  8.             $("html, body").animate({ scrollTop: 0 }, 120); 
  9.     }), $backToTopFun = function() { 
  10.         var st = $(document).scrollTop(), winh = $(window).height(); 
  11.         (st > 0)? $backToTopEle.show(): $backToTopEle.hide();    
  12.         //IE6下的定位 
  13.         if (!window.XMLHttpRequest) { 
  14.             $backToTopEle.css("top", st + winh - 166);   
  15.         } 
  16.     }; 
  17.     $(window).bind("scroll", $backToTopFun); 
  18.     $(function() { $backToTopFun(); }); 
  19. })(); 
  20. </script>  

返回首頁功能:

原理就是在基於上段代碼中的.click(function() {$("html, body").animate({ scrollTop: 0 }, 120);})點擊事件中加入轉向鏈接!具體代碼如下:

  1. <script>  
  2. (function() { 
  3.     var $backToTopind = "回首頁", $backToTopEle = $('<div class="backToTop1"></div>').appendTo($("body")) 
  4.         .text($backToTopind).attr("title", $backToTopind).click(function() {location.href='http://www.meowpet.com'
  5.     }), $backToTopFun = function() { 
  6.         var st = $(document).scrollTop(), winh = $(window).height(); 
  7.         (st > 0)? $backToTopEle.show(): $backToTopEle.hide(); 
  8.  
  9.         //IE6下的定位 
  10.         if (!window.XMLHttpRequest) { 
  11.             $backToTopEle.css("top", st + winh);     
  12.         } 
  13.     }; 
  14.     $(window).bind("scroll", $backToTopFun); 
  15.     $(function() { $backToTopFun(); }); 
  16. })(); 
  17. </script>  

 第三種方法:

  1. <script type="text/javascript"
  2. var w = 240; 
  3. var h = 59; 
  4. var str = ""
  5. var obj = document.getElementById("divStayTopLeft"); 
  6. if (obj)str = obj.innerHTML; 
  7. iftypeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat'){ 
  8. document.writeln('<DIV  style="z-index:9;right:0;bottom:0;height:'+h+'px;width:'+w+'px;overflow:hidden;POSITION:fixed;_position:absolute; _margin-top:expression(document.documentElement.clientHeight-this.style.pixelHeight+document.documentElement.scrollTop);">'); 
  9. else { 
  10. document.writeln('<DIV  style="z-index:9;right:0;bottom:0;height:'+h+'px;width:'+w+'px;overflow:hidden;POSITION:fixed;*position:absolute; *top:expression(eval(document.body.scrollTop)+eval(document.body.clientHeight)-this.style.pixelHeight);">'); 
  11. document.writeln('<div style="clear:both;margin:auto;height:40px;font-size:14px;overflow:hidden;font-weight:bold;text-align:left;"><a href="javascript:scroll(0,0)" hidefocus="true"><img src="http://images.cnblogs.com/top.gif" alt="回到頂部" style="border: 0px;" /></a><a href="http://www.meowpet.com/" hidefocus="true"><img src="http://images.cnblogs.com/hsyg.gif" alt="返回首頁" style="border: 0px;" /></a></div> '); 
  12. document.write('<div style="clear:both;margin:auto;overflow:hidden;text-align:left;">'+str+'</div>'); 
  13. document.writeln('</DIV>'); 
  14.  
  15. </script> 

里邊有2張圖片,可自行替換,這里就不提供了,找不到圖片路徑的話會顯示成alt里的漢字!


免責聲明!

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



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