DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> jquery任意位置浮動固定層插件用法實例
jquery任意位置浮動固定層插件用法實例
編輯:JavaScript綜合知識     

   本文實例講述了jquery任意位置浮動固定層插件用法。分享給大家供大家參考。具體分析如下:

  說明:可以讓指定的層浮動到網頁上的任何位置,當滾動條滾動時它會保持在當前位置不變,不會產生閃動

  2009-06-10修改:重新修改插件實現固定浮動層的方式,使用一個大固定層來定位

  2009-07-16修改:修正IE6下無法固定在top上的問題

  09-11-5修改:當自定義層的絕對位置時,加上top為空值時的判斷

  這次的方法偷自天涯新版頁

  經多次測試,基本上沒bug~

  調用:

  1 無參數調用:默認浮動在右下角

  $("#id").floatdiv();

  2 內置固定位置浮動

  ?

1 2 3 4 5 6 7 8 9 10 11 12 //右下角 $("#id").floatdiv("rightbottom"); //左下角 $("#id").floatdiv("leftbottom"); //右下角 $("#id").floatdiv("rightbottom"); //左上角 $("#id").floatdiv("lefttop"); //右上角 $("#id").floatdiv("righttop"); //居中 $("#id").floatdiv("middle");

  另外新添加了四個新的固定位置方法

  middletop(居中置頂)、middlebottom(居中置低)、leftmiddle、rightmiddle

  3 自定義位置浮動

  $("#id").floatdiv({left:"10px",top:"10px"});

  以上參數,設置浮動層在left 10個像素,top 10個像素的位置

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 jQuery.fn.floatdiv=function(location){ //判斷浏覽器版本 var isIE6=false; var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua.match(/msie ([d.]+)/)) ? Sys.ie = s[1] : 0; if(Sys.ie && Sys.ie=="6.0"){ isIE6=true; } var windowWidth,windowHeight;//窗口的高和寬 //取得窗口的高和寬 if (self.innerHeight) { windowWidth=self.innerWidth; windowHeight=self.innerHeight; }else if (document.documentElement&&document.documentElement.clientHeight) { windowWidth=document.documentElement.clientWidth; windowHeight=document.documentElement.clientHeight; } else if (document.body) { windowWidth=document.body.clientWidth; windowHeight=document.body.clientHeight; } return this.each(function(){ var loc;//層的絕對定位位置 var wrap=$("<div></div>"); var top=-1; if(location==undefined || location.constructor == String){ switch(location){ case("rightbottom")://右下角 loc={right:"0px",bottom:"0px"}; break; case("leftbottom")://左下角 loc={left:"0px",bottom:"0px"}; break; case("lefttop")://左上角 loc={left:"0px",top:"0px"}; top=0; break; case("righttop")://右上角 loc={right:"0px",top:"0px"}; top=0; break; case("middletop")://居中置頂 loc={left:windowWidth/2-$(this).width()/2+"px",top:"0px"}; top=0; break; case("middlebottom")://居中置低 loc={left:windowWidth/2-$(this).width()/2+"px",bottom:"0px"}; break; case("leftmiddle")://左邊居中 loc={left:"0px",top:windowHeight/2-$(this).height()/2+"px"}; top=windowHeight/2-$(this).height()/2; break; case("rightmiddle")://右邊居中 loc={right:"0px",top:windowHeight/2-$(this).height()/2+"px"}; top=windowHeight/2-$(this).height()/2; break; case("middle")://居中 var l=0;//居左 var t=0;//居上 l=windowWidth/2-$(this).width()/2; t=windowHeight/2-$(this).height()/2; top=t; loc={left:l+"px",top:t+"px"}; break; default://默認為右下角 location="rightbottom"; loc={right:"0px",bottom:"0px"}; break; } }else{ loc=location; alert(loc.bottom); var str=loc.top; //09-11-5修改:加上top為空值時的判斷 if (typeof(str)!= 'undefined'){ str=str.replace("px",""); top=str; } } /*fied ie6 css hack*/ if(isIE6){ if (top>=0) { wrap=$("<div style="top:e­xpression(documentElement.scrollTop+"+top+");"></div>"); }else{ wrap=$("<div style="top:e­xpression(documentElement.scrollTop+documentElement.clientHeight-this.offsetHeight);"></div>"); } } $("body").append(wrap); wrap.css(loc).css({position:"fixed",z_index:"999"}); if (isIE6) { wrap.css("position","absolute"); //沒有加這個的話,ie6使用表達式時就會發現跳動現象 //至於為什麼要加這個,還有為什麼要加nothing.txt這個,偶也不知道,希望知道的同學可以告訴我 $("body").css("background-attachment","fixed").css("background-image","url(n1othing.txt)"); } //將要固定的層添加到固定層裡 $(this).appendTo(wrap); }); };

  完整實例代碼點擊此處本站下載。

  希望本文所述對大家的jQuery程序設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved