温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jquery回到页面顶部代码案例

发布时间:2020-07-03 18:18:51 来源:网络 阅读:805 作者:为梦加速 栏目:web开发

 方法一:

效果图

jquery回到页面顶部代码案例

css代码:

 

  1. .backToTop { 
  2.     displaynone
  3.     width18px
  4.     line-height1.2
  5.     padding5px 0
  6.     background-color#000
  7.     color#fff
  8.     font-size12px
  9.     text-aligncenter
  10.     positionfixed
  11.     _positionabsolute
  12.     right: 10px
  13.     bottom: 100px
  14.     _bottom: "auto"
  15.     cursorpointer
  16.     opacity: .6
  17.     filter: Alpha(opacity=60); 

js代码:

 

  1. (function() { 
  2.     var $backToTopTxt = "返回顶部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body")) 
  3.         .text($backToTopTxt).attr("title", $backToTopTxt).click(function() { 
  4.             $("html, body").animate({ scrollTop: 0 }, 120); 
  5.     }), $backToTopFun = function() { 
  6.         var st = $(document).scrollTop(), winh = $(window).height(); 
  7.         (st > 0)? $backToTopEle.show(): $backToTopEle.hide();     
  8.         //IE6下的定位 
  9.         if (!window.XMLHttpRequest) { 
  10.             $backToTopEle.css("top", st + winh - 166);     
  11.         } 
  12.     }; 
  13.     $(window).bind("scroll", $backToTopFun); 
  14.     $(function() { $backToTopFun(); }); 
  15. })(); 

 方法二:

效果图

 

jquery回到页面顶部代码案例

代码:

需要引人jquery和一张向上箭头图片up.png

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"> 
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
  6. <title>Insert title here</title> 
  7. <script type="text/javascript" src="../javascript/jquery-1.6.js"></script> 
  8. <style type="text/css"> 
  9.     body { 
  10.         height:1200px; 
  11.     } 
  12.      
  13.     #testIE6 { 
  14.         _height:1200px; 
  15.         _width:200px; 
  16.     } 
  17.      
  18.     .scroll-up { 
  19.         background: #dcdcdc url('up.png') no-repeat center center; 
  20.         width:48px !important; 
  21.         height:48px !important; 
  22.         _width: 58px; 
  23.         _height: 58px; 
  24.         position: fixed; 
  25.         _position: absolute; /*for IE6*/ 
  26.         opacity: .6; 
  27.         filter: Alpha(opacity=60); /*for IE*/ 
  28.         padding:5px;  
  29.         cursor: pointer; 
  30.         display: none; 
  31.  
  32.         border-radius:5px; 
  33.         -webkit-transition-property: background-color, opacity; 
  34.         -webkit-transition-duration: 1s; 
  35.         -webkit-transition-timing-function: ease; 
  36.          
  37.         -moz-transition-property: background-color; 
  38.         -moz-transition-duration: 1s; 
  39.         -moz-transition-timing-function: ease; 
  40.     } 
  41.     .scroll-up:hover { 
  42.         background-color:#B9B9B9; 
  43.         opacity: .8; 
  44.     } 
  45. </style> 
  46. </head> 
  47. <body> 
  48.     <div id="testIE6"></div> 
  49.     <script type="text/javascript"> 
  50.         ;(function($) { 
  51.             $.scrollBtn = function(options) { 
  52.                 var opts = $.extend({}, $.scrollBtn.defaults, options); 
  53.  
  54.                 var $scrollBtn = $('<div></div>').css({ 
  55.                                     bottom: opts.bottom + 'px', 
  56.                                     right: opts.right + 'px' 
  57.                                 }).addClass('scroll-up') 
  58.                                 .attr('title', opts.title) 
  59.                                 .click(function() { 
  60.                                     $('html, body').animate({scrollTop: 0}, opts.duration); 
  61.                                 }).appendTo('body'); 
  62.                                                                                      
  63.                 $(window).bind('scroll', function() { 
  64.                     var scrollTop = $(document).scrollTop(), 
  65.                         viewHeight = $(window).height(); 
  66.  
  67.                     if(scrollTop <= opts.showScale) { 
  68.                         if($scrollBtn.is(':visible')) 
  69.                             $scrollBtn.fadeOut(500); 
  70.                     } else { 
  71.                         if($scrollBtn.is(':hidden'))  
  72.                             $scrollBtn.fadeIn(500); 
  73.                     } 
  74.  
  75.                     if(isIE6()) { 
  76.                         var top = viewHeight + scrollTop - $scrollBtn.outerHeight() - opts.bottom; 
  77.                         $scrollBtn.css('top', top + 'px'); 
  78.                     } 
  79.                 }); 
  80.  
  81.                 function isIE6() { 
  82.                     if($.browser.msie) { 
  83.                         if($.browser.version == '6.0') return true; 
  84.                     } 
  85.                 } 
  86.             }; 
  87.  
  88.             /** 
  89.              * -params  
  90.              *  -showScale: scroll down how much to show the scrollup button 
  91.              *  -right: to right of scrollable container  
  92.              *  -bottom: to bottom of scrollable container  
  93.              */ 
  94.             $.scrollBtn.defaults = { 
  95.                 showScale: 100,   
  96.                 right:10, 
  97.                 bottom:10, 
  98.                 duration:200, 
  99.                 title:'back to top' 
  100.             } 
  101.         })(jQuery); 
  102.  
  103.         $.scrollBtn({ 
  104.             showScale: 200, 
  105.             bottom:20, 
  106.             right:20 
  107.         }); 
  108.     </script> 
  109. </body> 
  110. </html> 

 

附件:http://down.51cto.com/data/2362200
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI