DIV CSS 佈局教程網

jQuery floatdiv 外掛:任意位置浮動固定層完整實作
編輯:JQuery特效代碼     

作者:沒劍(https://regedit.cnblogs.com) 08-03-11
2008-04-01 修改:修正自訂 right 位置時無效的問題,並修正層位置在 IE6 下的偏移。

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

呼叫方式

// 1. 無參數呼叫:預設浮動在右下角
$("#id").floatdiv();

// 2. 內建固定位置浮動
$("#id").floatdiv("rightbottom");   // 右下角
$("#id").floatdiv("leftbottom");    // 左下角
$("#id").floatdiv("lefttop");       // 左上角
$("#id").floatdiv("righttop");      // 右上角
$("#id").floatdiv("middle");        // 居中

// 3. 自訂位置浮動
$("#id").floatdiv({left:"10px", top:"10px"});
// 以上參數會把浮動層設在 left 10 像素、top 10 像素的位置

外掛程式碼

jQuery.fn.floatdiv = function(location){
    // IE6 要隱藏縱向捲軸
    var isIE6 = false;
    if($.browser.msie && $.browser.version == "6.0"){
        $("html").css("overflow-x","auto").css("overflow-y","hidden");
        isIE6 = true;
    }

    $("body").css({
        margin: "0px",
        padding: "0 10px 0 10px",
        border: "0px",
        height: "100%",
        overflow: "auto"
    });

    return this.each(function(){
        var loc;   // 層的絕對定位位置

        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"};
                    break;
                case("righttop"):      // 右上角
                    loc = {right:"0px", top:"0px"};
                    break;
                case("middle"):        // 居中
                    var l = 0;   // 居左
                    var t = 0;   // 居上
                    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;
                    }
                    l = windowWidth  / 2 - $(this).width()  / 2;
                    t = windowHeight / 2 - $(this).height() / 2;
                    loc = {left: l + "px", top: t + "px"};
                    break;
                default:               // 預設為右下角
                    loc = {right:"0px", bottom:"0px"};
                    break;
            }
        }else{
            loc = location;
        }

        $(this).css("z-index","9999").css(loc).css("position","fixed");

        if(isIE6){
            if(loc.right != undefined){
                // 2008-4-1 修改:自訂 right 位置時若無值,補上 18px 以修正 IE6 下的層位置
                if($(this).css("right") == null || $(this).css("right") == ""){
                    $(this).css("right","18px");
                }
            }
            $(this).css("position","absolute");
        }
    });
};

使用範例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" >
<head>
<title>任意位置浮動視窗外掛</title>
<script type="text/javascript" src="../js/jquery-1.2.6.js"></script>
<script type="text/javascript" src="../js/jquery.floatDiv.js"></script>
<script type="text/javascript">
$(function(){
    $("#test").floatdiv({top:"200px", right:"200px"});
    $("#rtop").floatdiv("righttop");
    $("#floatAd").floatdiv({top:"50px", left:"50px"});
});
</script>
</head>
<body>
<div id="rtop" style="background-color: #666633; width: 300px; height: 100px;padding:2px;color:white;">右上角</div>
<div id="floatAd" style="background-color: #667733; width: 300px; height: 100px;padding:2px;color:white;">左下角</div>
<div id="test" style="background-color: #665533; width: 300px; height: 100px;padding:2px;color:white;">
<p>
/*任意位置浮動固定層*/<br />
/*說明:可以讓指定的層浮動到網頁上的任何位置,當捲軸捲動時它會保持在目前位置不變,不會產生閃動*/<br />
/*調用:<br />
1 無參數調用:預設浮動在右下角<br />
$("#id").floatdiv();</p>
<p>
2 內建固定位置浮動<br />
//右下角<br />
$("#id").floatdiv("rightbottom");<br />
//左下角<br />
$("#id").floatdiv("leftbottom");<br />
//左上角<br />
$("#id").floatdiv("lefttop");<br />
//右上角<br />
$("#id").floatdiv("righttop");<br />
//居中<br />
$("#id").floatdiv("middle");</p>
<p>
3 自訂位置浮動<br />
$("#id").floatdiv({left:"10px",top:"10px"});<br />
以上參數,設置浮動層在left 10個像素,top 10個像素的位置<br />
*/</p>
</div>
<div>hello</div>
</body>
</html>

重點解析

  • position:fixed 是關鍵:它讓元素固定在視窗座標系上,捲動頁面時不會跑掉,這也是「不會閃動」的原因。
  • IE6 的例外處理:IE6 不支援 position:fixed,所以外掛偵測到 IE6 時會改用 absolute,並把 right 補上 18px(IE6 經典的捲軸寬度偏移問題)。
  • 居中的算法:視窗寬高減去元素寬高再除以二,並用三段 if 相容不同的瀏覽器取值方式。
  • z-index:9999:確保浮動層壓在一般內容之上。

提醒:程式中使用的 $.browser 在 jQuery 1.9 之後已被移除,若要在新版 jQuery 使用,需自行改用 navigator.userAgent 判斷。

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