Atea - Hero's Grave

          面向?qū)ο?,開源,框架,敏捷,云計算,NoSQL,商業(yè)智能,編程思想。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            40 隨筆 :: 0 文章 :: 28 評論 :: 0 Trackbacks
          傳統(tǒng)的"Return to Top"置于頁面底部或指定位置,點擊后返回頁面頂部。
          但其存在諸多不便:
          1、頁面頂部必須定義一個相應(yīng)的錨點,其本身也會占用DOM空間,很容易發(fā)生返回的不是絕對的頂部而是該錨點(滾動條會稍微向下一點,而不是在0的位置)
          2、如果頁面很長,想在滾動條任意處回頂部會變得難以處理。

          使用jQuery能大大改善這一用戶體驗。本文章將能做到:
          "Return to Top"在頁面初始化時不顯示,當(dāng)移動滾動條時出現(xiàn)并隨滾動條位置變化而變化。
          點擊"Return to Top"頁面移動到頂部,當(dāng)滾動條到頂部時"Return to Top"自動隱藏。
          以上均伴有動畫效果。
          在線Demo:http://atealxt.appspot.com/guestbook
          下載實例:http://www.aygfsteel.com/Files/atealxt/returnToTop.zip

          首先有一個DIV
          <div id="switcher">
            
          <href="#top" class="return-to-top">Return to Top</a>
          </div>

          其對應(yīng)的CSS為
          #switcher {
            position
          : absolute;
            width
          : 90px;
            padding
          : .5em;
            border
          : 1px solid #777;
            background
          : #ddd;
          }
          .return-to-top 
          {
            clear
          : left;
          }

          在document.ready中令"Return to Top"移動到頁面最右邊,并隱藏。
          $switcher.animate({
              'left': $('#container').outerWidth() 
          - $switcher.outerWidth()
            }, 
          function() {
                 $document.bind('scroll', moveSwitcher);
               }
          );
          $switcher.hide();
          綁定scroll事件要注意,要想支持IE6的話,必須用$window來綁定scroll事件。

          函數(shù)moveSwitcher實現(xiàn)"Return to Top"隨滾動條移動而移動。
          這里設(shè)定了一個事件延遲捕捉,以達到動畫效果。
          var moveSwitcher = function() {
              
            
          var delay = 500;
            
          var executionTimer;
            
          return function() {
              
          if (!!executionTimer) {
                clearTimeout(executionTimer);
              }
              
              executionTimer 
          = setTimeout(function() {
                $switcher.animate({ 'top': $window.height() 
          + $document.scrollTop() - $switcher.height() - 25 }, 'slow', slideSwitcher);
              }, delay);
            };
          }();

          函數(shù)slideSwitcher為頁面移動到頂部時隱藏"Return to Top"。
          var slideSwitcher = function() {
            
          if ($document.scrollTop() == 0) {
              $switcher.slideUp('fast');
            } 
          else {
              $switcher.slideDown('fast');
            }
          };

          為了完善這一效果,我們給resize事件也綁定moveSwitcher
          $window.resize(function(){
            $switcher.animate({ 'left': $('#container').outerWidth() 
          - $switcher.outerWidth() } , moveSwitcher);
          });

          最后,給"Return to Top"添加onclick事件
          1 $document.ready(function() {
          2 
          3   $('a.return-to-top').click(function() {
          4     $('html').animate({scrollTop: 0}, 'fast');
          5     $('body').animate({scrollTop: 0}, 'fast');
          6     return false;
          7   });
          8 });
          第5行是專門為chrome和safari而追加的(感謝Wyatt同學(xué)提醒)
          參考http://stackoverflow.com/questions/1830080/jquery-scrolltop-doesnt-seem-to-work-in-safari-or-chrome-windows

          好了,大功告成了。其實需要寫的代碼很少,真是贊嘆jQuery的強大:D
          posted on 2010-05-13 11:14 Atea 閱讀(2173) 評論(2)  編輯  收藏 所屬分類: Javascript

          評論

          # re: 使用jQuery改善"Return to Top" 2010-05-13 11:32 Wyatt
          chrome里面不好使~~  回復(fù)  更多評論
            

          # re: 使用jQuery改善"Return to Top" 2010-05-13 13:16 Atea
          @Wyatt
          謝謝提醒,已修改:P  回復(fù)  更多評論
            

          主站蜘蛛池模板: 大渡口区| 大悟县| 会泽县| 个旧市| 永春县| 盱眙县| 林芝县| 边坝县| 精河县| 青州市| 淳安县| 青冈县| 武川县| 牟定县| 英德市| 如东县| 阿拉尔市| 海晏县| 分宜县| 偃师市| 三都| 洪湖市| 阿荣旗| 罗定市| 乐亭县| 牡丹江市| 五指山市| 泽库县| 平陆县| 卢氏县| 茶陵县| 册亨县| 乌鲁木齐市| 永康市| 盐边县| 东源县| 三门县| 娄底市| 锡林郭勒盟| 景泰县| 铜梁县|