跳轉至頭部

今天切一個專題頁,頁面比較長,想在右下角加一個返回頭部的按鈕。

一開始模仿w3cplus.com用了普通的錨點定位,代碼如下:

  1. <div id="goToTop"> 
  2. <a title="回到頂部" href="#">TOP</a> 
  3. </div> 
  1. #goToTop { 
  2.     bottom: 5px
  3.     positionfixed
  4.     right: 5px
  5.     z-index9000

發現在ie6底下,居然錯位,跑到左下角去了,心想,應該不會呀,這個網站都是這樣搞滴,於是用ie打開,崇拜已久的w3cplus網站,它的網站居然沒有考慮到ie6,用的都是新技術。哎,沒辦法,只能自己來了。

···插曲:以前在公司只要測IE6,7,8,9,ff,谷歌,現在做國內站,除了這些主流的,還得測,360,搜狗,遨遊,safari,蘋果。。天哪。兼容性問題,像360這樣變態的瀏覽器,似IE內核又不是IE內核,頭疼哪。。

回到正題。。

於是,朋友建議用了jquery,畢竟用ie hack定位,不太好,其實我目前還不是很喜歡js,畢竟不是自己寫的代碼,都是調用的,如果遇到問題,解決不了,那豈不是很糗,於是一直在惡補js,那本厚厚的js權威指南,一番就犯困,太概念性了,還是得實踐實踐在實踐,我相信幾個月之後會有突破的。

一開始一直無效果,原來是js加載順序的問題,ohmygod,超想屎的。現在ok了,ie6下也行,明天去看看那些國內其他瀏覽器的效果。

方法如下:

1,調用jquery的官方類庫。。傳說中jquery-1.3.1.js這個最好用,我個人不喜歡過時的東西。。

2,調用一個別人寫的gototop的js。。(誰來解釋一下)

  1. jQuery.fn.floatdiv=function(location){ 
  2.     var isIE6=false
  3.     var Sys={}; 
  4.     var ua=navigator.userAgent.toLowerCase(); 
  5.     var s; 
  6.     (s=ua.match(/msie ([\d.]+)/))?Sys.ie=s[1]:0; 
  7.     if(Sys.ie&&Sys.ie=="6.0"){isIE6=true;} 
  8.     var windowWidth,windowHeight; 
  9.     if(self.innerHeight){ 
  10.         windowWidth=self.innerWidth; 
  11.         windowHeight=self.innerHeight; 
  12.     } 
  13.     else if(document.documentElement&&document.documentElement.clientHeight){ 
  14.         windowWidth=document.documentElement.clientWidth; 
  15.         windowHeight=document.documentElement.clientHeight; 
  16.     }else if(document.body){ 
  17.         windowWidth=document.body.clientWidth; 
  18.         windowHeight=document.body.clientHeight; 
  19.     } 
  20.     return this.each(function(){ 
  21.         var loc; 
  22.         var wrap=$("<div></div>"); 
  23.         var top=-1; 
  24.         if(location==undefined||location.constructor==String){ 
  25.             switch(location){ 
  26.                 case("rightbottom"): 
  27.                     loc={right:"0px",bottom:"0px"}; 
  28.                     break
  29.                 case("leftbottom"): 
  30.                     loc={left:"0px",bottom:"0px"}; 
  31.                     break
  32.                 case("lefttop"): 
  33.                     loc={left:"0px",top:"0px"}; 
  34.                     top=0; 
  35.                     break
  36.                 case("righttop"): 
  37.                     loc={right:"0px",top:"0px"}; 
  38.                     top=0; 
  39.                     break
  40.                 case("middletop"): 
  41.                     loc={left:windowWidth/2-$(this).width()/2+"px",top:"0px"}; 
  42.                     top=0; 
  43.                     break
  44.                 case("middlebottom"): 
  45.                     loc={left:windowWidth/2-$(this).width()/2+"px",bottom:"0px"}; 
  46.                     break
  47.                 case("leftmiddle"): 
  48.                     loc={left:"0px",top:windowHeight/2-$(this).height()/2+"px"}; 
  49.                     top=windowHeight/2-$(this).height()/2; 
  50.                     break
  51.                 case("rightmiddle"): 
  52.                     loc={right:"0px",top:windowHeight/2-$(this).height()/2+"px"}; 
  53.                     top=windowHeight/2-$(this).height()/2; 
  54.                     break
  55.                 case("middle"): 
  56.                     var l=0; 
  57.                     var t=0; 
  58.                     l=windowWidth/2-$(this).width()/2; 
  59.                     t=windowHeight/2-$(this).height()/2; 
  60.                     top=t; 
  61.                     loc={left:l+"px",top:t+"px"}; 
  62.                     break
  63.                 default
  64.                     location="rightbottom"
  65.                     loc={right:"0px",bottom:"0px"}; 
  66.                     break
  67.             } 
  68.         }else
  69.             loc=location; 
  70.             var str=loc.top; 
  71.             if(typeof(str)!='undefined'){ 
  72.                 str=str.replace("px","");top=str; 
  73.             } 
  74.         } 
  75.         if(isIE6){ 
  76.             if(top>=0){ 
  77.                 wrap=$("<div style=\"top:expression(documentElement.scrollTop+documentElement.clientHeight /2-this.offsetHeight/2);\"></div>"); 
  78.             } 
  79.             else
  80.                 wrap=$("<div style=\"top:expression(documentElement.scrollTop+documentElement.clientHeight-this.offsetHeight);\"></div>"); 
  81.             } 
  82.         } 
  83.         $("body").append(wrap);wrap.css(loc).css({position:"fixed",z_index:"999"}); 
  84.         if(isIE6){ 
  85.             wrap.css("position","absolute"); 
  86.             $("body").css("background-p_w_upload","fixed").css("background-p_w_picpath","url(n1othing.txt)"); 
  87.         } 
  88.         $(this).appendTo(wrap); 
  89.     }); 
  90. };

3,在html頁面中引用

  1. <script> 
  2.     (function(){ 
  3.         var lc = $('#goToTop'); 
  4.         lc.floatdiv('rightbottom'); 
  5.     })() 
  6. </script> 

求解:還有沒有更好,更簡單的辦法啊。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章