jQuery返回顶部效果



1、首先是CSS样式:

  1.  /*updown*/  
  2. #shangxia{position:fixed;top:83%;right:42%;margin-right:-543px;display:block;_display:none}  
  3. #shang,#comt,#xia{background:url(../images/sprite.png) no-repeat;position:relative;cursor:pointer;height:25px;width:29px;margin:10px 0 0}  
  4. #shang{background-position:0 -308px}#comt{background-position:0 -338px;height:32px}  
  5. #xia{background-position:0 -376px}  
  6. #comt:hover{background-position:-31px -338px}#shang:hover{background-position:-31px -308px}  
  7. #xia:hover{background-position:-31px -376px}  

2、接着是jquery代码:(要引入jQuery核心库)
[javascript] view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. /*updown*/  
  2. jQuery(document).ready(function($) {$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $("html") : $("body")) : $("html,body");$("#shang").mouseover(function() {up()}).mouseout(function() {clearTimeout(fq)}).click(function() {$body.animate({scrollTop: 0},400)});$("#xia").mouseover(function() {dn()}).mouseout(function() {clearTimeout(fq)}).click(function() {$body.animate({scrollTop: $(document).height()},400)});$("#comt").click(function() {$body.animate({scrollTop: $("#comments").offset().top},400)});});function up() {$wd = jQuery(window);$wd.scrollTop($wd.scrollTop() - 1);fq = setTimeout("up()", 50)};function dn() {$wd = jQuery(window);$wd.scrollTop($wd.scrollTop() + 1);fq = setTimeout("dn()", 50)};  

3、引用页面:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.   <head>  
  4.     <base href="<%=basePath%>">  
  5.       
  6.     <title>My JSP 'index.jsp' starting page</title>  
  7.     <meta http-equiv="pragma" content="no-cache">  
  8.     <meta http-equiv="cache-control" content="no-cache">  
  9.     <meta http-equiv="expires" content="0">      
  10.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  11.     <meta http-equiv="description" content="This is my page">  
  12.     <!-- 
  13.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  14.     -->  
  15.     <style type="text/css">  
  16.        /*updown*/  
  17.         #shangxia{position:fixed;top:60%;right:50%;margin-right:-543px;display:block;_display:none}  
  18.         #shang,#comt,#xia{background:url(images/sprite.png) no-repeat;position:relative;cursor:pointer;height:25px;width:29px;margin:10px 0 0}  
  19.         #shang{background-position:0 -308px}#comt{background-position:0 -338px;height:32px}#xia{background-position:0 -376px}  
  20.         #comt:hover{background-position:-31px -338px}#shang:hover{background-position:-31px -308px}#xia:hover{background-position:-31px -376px}  
  21.         /*存档*/  
  22.     </style>  
  23.     ///上面的是内部样式,上面已经有外部样式,如果不使用内部样式可以引入外部样式  
  24.     <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>  
  25.     <script type="text/javascript" src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>  
  26.     <script type="text/javascript" src="js/updown/devework.js"></script>  
  27.   </head>  
  28.      
  29.     
  30.   <body>  
  31.     
  32.    <p>1</p>  
  33.     <p>1</p>  
  34.      <p>1</p>  
  35.       <p>1</p>  
  36.        <p>1</p>  
  37.        <p>1</p>  
  38.     <p>1</p>  
  39.      <p>1</p>  
  40.       <p>1</p>  
  41.        <p>1</p>  
  42.        <p>1</p>  
  43.     <p>1</p>  
  44.      <p>1</p>  
  45.       <p>1</p>  
  46.        <p>1</p>  
  47.        <p>1</p>  
  48.     <p>1</p>  
  49.      <p>1</p>  
  50.       <p>1</p>  
  51.        <p>1</p>  
  52.        <p>1</p>  
  53.     <p>1</p>  
  54.      <p>1</p>  
  55.       <p>1</p>  
  56.        <p>1</p>  
  57.        <p>1</p>  
  58.     <p>1</p>  
  59.      <p>1</p>  
  60.       <p>1</p>  
  61.        <p>1</p>  
  62.        <p>1</p>  
  63.     <p>1</p>  
  64.      <p>1</p>  
  65.       <p>1</p>  
  66.        <p>1</p>  
  67.        <p>333</p>  
  68.         <p>333</p>  
  69.          <p>333</p>  
  70.           <p>333</p>  
  71.            <p>333</p>  
  72.             <p>333</p>  
  73.              <p>333</p>  
  74.               <p>333</p>  
  75.         <p>333</p>  
  76.          <p>333</p>  
  77.           <p>333</p>  
  78.            <p>333</p>  
  79.             <p>333</p>  
  80.              <p>333</p>  
  81.               <p>333</p>  
  82.         <p>333</p>  
  83.          <p>333</p>  
  84.           <p>333</p>  
  85.            <p>333</p>  
  86.             <p>333</p>  
  87.              <p>333</p>  
  88.               <p>333</p>  
  89.         <p>333</p>  
  90.          <p>333</p>  
  91.           <p>333</p>  
  92.            <p>333</p>  
  93.             <p>333</p>  
  94.              <p>333</p>  
  95.               <p>333</p>  
  96.         <p>333</p>  
  97.          <p>333</p>  
  98.           <p>333</p>  
  99.            <p>333</p>  
  100.             <p>333</p>  
  101.              <p>333</p>  
  102.               <p>333</p>  
  103.         <p>333</p>  
  104.          <p>333</p>  
  105.           <p>333</p>  
  106.            <p>333</p>  
  107.             <p>333</p>  
  108.              <p>333</p>  
  109.               <p>333</p>  
  110.         <p>333</p>  
  111.          <p>333</p>  
  112.           <p>333</p>  
  113.            <p>333</p>  
  114.             <p>333</p>  
  115.              <p>333</p>  
  116.               <p>333</p>  
  117.         <p>333</p>  
  118.          <p>333</p>  
  119.           <p>333</p>  
  120.            <p>333</p>  
  121.             <p>333</p>  
  122.              <p>333</p>  
  123.               <p>333</p>  
  124.         <p>333</p>  
  125.          <p>333</p>  
  126.           <p>333</p>  
  127.            <p>333</p>  
  128.             <p>333</p>  
  129.              <p>333</p>  
  130.                
  131.         <div id="shangxia">  
  132.             <div id="shang" title="↑ 返回顶部"></div>  
  133.             <div id="comt" title="沙发还没有被抢哦"></div>  
  134.             <div id="xia" title="↓ 移至底部"></div>  
  135.         </div>  
  136.   </body>  
  137. </html> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值