注:本文代碼皆基于jquery實(shí)現(xiàn)。

按照普通的方法寫一個(gè)layout,一般是用一個(gè)table來 " /> 亚洲三级色网,涩涩视频在线,456亚洲精品成人影院

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

一個(gè)剛完成的layout(拖動(dòng)流暢,不受iframe影響)

寫一個(gè)layout本來是一個(gè)很簡單的事情,可這次的一個(gè)layout問題確讓我為難了許久才做出來,下面來大概講解一下問題的出現(xiàn)與解決過程。

注:本文代碼皆基于jquery實(shí)現(xiàn)。

按照普通的方法寫一個(gè)layout,一般是用一個(gè)table來實(shí)現(xiàn),用中間的td拖動(dòng)來控制左右兩個(gè)td的大小,這個(gè)問題簡單,很快就搞定。代碼如下:

QUOTE:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        <title>Untitled Document</title>
    <style type="text/css">
        *{margin:0px;padding:0px}
        html{overflow:hidden}
        #sideBar{width:200px;height:100%;overflow:auto}
        #toggleBar,.div{
            width:7px;height:100%;
            overflow:hidden;background:#eee;
            cursor:e-resize;border-left:1px solid #ccc;border-right:1px solid #ccc;
        }
        td{display:block;overflow:auto;word-break:break-all;}
    </style>
    <script type="text/Javascript" src="../Common/jquery.gif"></script>
    <script type="text/Javascript">
        $(document).ready(function(){
                //及時(shí)調(diào)整頁面內(nèi)容的高度
                setInterval(function(){
                    var winH=(document.documentElement||document.body).clientHeight;
                    $("#tbl,#sideBar,#toggleBar,#main").css("height",winH);
                    $("td").each(function(){$(this).html()||$(this).html(" ")});
                },100)
            }
        );

        var begin_x; 
        var drag_flag = false; 
        document.onmousemove = mouseDrag
        document.onmouseup = mouseDragEnd
        //半透明拖動(dòng)條
        var alphaDiv="<div class='div' id='alphaDiv' style='position:absolute;height:2000px;top:0;z-index:10001;filter:alpha(opacity=50);opacity:0.5;left:200px'> </div>";
        function setDrag(){
            drag_flag=true; 
            begin_x=event.x;
            //添加半透明拖動(dòng)條
            $(alphaDiv).css("left",$("#toggleBar")[0].offsetLeft).appendTo("body");
        }

        //拖動(dòng)時(shí)執(zhí)行的函數(shù)
        function mouseDrag(){
            if(drag_flag==true){
                if (window.event.button==1){
                    var now_x=event.x;
                    var value=parseInt($("#alphaDiv")[0].style.left)+now_x-begin_x;
                    $("#alphaDiv")[0].style.left=value+"px";
                     begin_x=now_x;
                }    
                $("body").css("cursor","e-resize");    //設(shè)定光標(biāo)類型
            }else{
                try{
                    $("#sideBar")[0].style.pixelWidth=$("#alphaDiv")[0].style.left;
                    $("#alphaDiv").remove();
                }catch(e){}
            }
        }

        function mouseDragEnd(){
            //設(shè)置拖動(dòng)條的位置
            if(drag_flag==true){
                //設(shè)定拖動(dòng)條的位置(設(shè)定左側(cè)的寬度)
                $("#sideBar")[0].style.pixelWidth=parseInt($("#alphaDiv")[0].style.left);
                $("#alphaDiv").remove();    //刪除半透明拖動(dòng)條
                $("body").css("cursor","normal");    //恢復(fù)光標(biāo)類型
            }
            drag_flag=false;
        }
    </script>
    </head>
    <body>
        <table id="tbl" border="0" bordercollASPe="collapse" cellpadding="2" cellspacing="0" width="100%" height="100%">
            <tr>
                <td width="1"><div id="sideBar" style="width:200px;"><div style="height:1200px">asdfasdf</div></div>
                </td>
                <td width="1" onmousedown="setDrag()" id="toggleBar"></td>
                <td id="main">
                    right Panel
                </td>
            </tr>
        </table>
    </body>
</html>
演示地址:http://www.ajaxbbs.NET/test/layout/JqSplit/noiframe.htm上面的這種寫法也是大多數(shù)layout的寫法,著名框架dojo好像也是這么實(shí)現(xiàn)的,其他的沒試。

但現(xiàn)在的情況仍然不能滿足我們的需求,我們需要左側(cè)或右側(cè)是ifame,通過iframe調(diào)用相關(guān)的頁面,在前面的代碼中將右側(cè)改為iframe。
演示地址:http://www.ajaxbbs.NET/test/layout/JqSplit/iframeRight.htm

這時(shí)我們就發(fā)現(xiàn)問題了,只能向左邊拖動(dòng),但不能像右邊拖動(dòng),這是為什們呢?
經(jīng)過檢查,發(fā)現(xiàn)原來當(dāng)鼠標(biāo)移動(dòng)到iframe上就無法捕獲鼠標(biāo)的位置了,event對象也不存在。得不到鼠標(biāo)的位置我們的拖動(dòng)當(dāng)然會(huì)出現(xiàn)問題了。

這個(gè)問題著實(shí)讓我郁悶了許久,然后測試其他的一些layout(對iframe進(jìn)行了處理)發(fā)現(xiàn)凡是使用iframe的都有一個(gè)缺陷,當(dāng)鼠標(biāo)拖動(dòng)速度很快的時(shí)候,拉動(dòng)條速度跟不上(當(dāng)然這些并沒有那個(gè)模擬的半透明的拖動(dòng)條,直接拖動(dòng)真實(shí)的拖動(dòng)條的),感覺就是很不流暢很不同步。
我們看一下直接拖動(dòng)真是滾動(dòng)條的情況
演示地址:http://www.ajaxbbs.NET/test/layout/JqSplit/iframeRightNoAlpha.htm我們慢速度拖動(dòng)還是可以向右移動(dòng)的,但一但速度稍快便不能拖動(dòng)了。

對于這個(gè)問題始終沒有想到好的解決辦法,就在我悲傷的即將放棄時(shí),看到前幾天寫的一個(gè)模擬彈出框,因?yàn)楫?dāng)時(shí)測試彈出框應(yīng)該要遮住包括iframe在內(nèi)的select。所以頁面中使用了ifame。突然發(fā)現(xiàn)一個(gè)索引很高的層能夠遮住iframe,突然間就有了靈感,馬上實(shí)驗(yàn)。

思路如下:拖動(dòng)拉條時(shí)在頁面添加一個(gè)索引很大的層(如10000),將其透明度設(shè)為0(完全透明),這樣鼠標(biāo)就不會(huì)移動(dòng)到iframe中,但iframe仍然存在可以看到。當(dāng)拖動(dòng)結(jié)束(onmouseup)時(shí)去掉這個(gè)層即可,這樣就實(shí)現(xiàn)了比較完美的拖動(dòng)。

演示地址:http://www.ajaxbbs.NET/test/layout/JqSplit/demo.htm我們看一下完整的代碼:

QUOTE:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        <title>Untitled Document</title>
    <style type="text/css">
        *{margin:0px;padding:0px}
        html{overflow:hidden}
        #sideBar{width:200px;height:100%;overflow:auto}
        #toggleBar,.div{
            width:7px;height:100%;
            overflow:hidden;background:#eee;
            cursor:e-resize;border-left:1px solid #ccc;border-right:1px solid #ccc;
        }
        td{display:block;overflow:auto;word-break:break-all;}
    </style>
    <script type="text/Javascript" src="../Common/jquery.js"></script>
    <script type="text/Javascript">
        $(document).ready(function(){
                //及時(shí)調(diào)整頁面內(nèi)容的高度
                setInterval(function(){
                    var winH=(document.documentElement||document.body).clientHeight;
                    $("#tbl,#sideBar,#toggleBar,#main").css("height",winH);
                    $("td").each(function(){$(this).html()||$(this).html(" ")});
                },100)
            }
        );

        var begin_x; 
        var drag_flag = false; 
        document.onmousemove = mouseDrag
        document.onmouseup = mouseDragEnd
        //半透明的拖動(dòng)條(模擬)
        var alphaDiv="<div class='div' id='alphaDiv' style='position:absolute;height:2000px;top:0;z-index:10001;filter:alpha(opacity=50);opacity:0.5;left:200px'> </div>";
        function setDrag(){
            drag_flag=true; 
            begin_x=event.x;
            //添加蒙板
            createMask();
            //添加半透明拖動(dòng)條
            $(alphaDiv).css("left",$("#toggleBar")[0].offsetLeft).appendTo("body");
        }

        //關(guān)鍵部分
        function createMask(){
            //創(chuàng)建背景
            var rootEl=document.documentElement||document.body;
            var docHeight=((rootEl.clientHeight>rootEl.scrollHeight)?rootEl.clientHeight:rootEl.scrollHeight)+"px";
            var docWidth=((rootEl.clientWidth>rootEl.scrollWidth)?rootEl.clientWidth:rootEl.scrollWidth)+"px";
            var shieldStyle="position:absolute;top:0px;left:0px;width:"+docWidth+";height:"+docHeight+";background:#000;z-index:10000;filter:alpha(opacity=0);opacity:0";
            $("<div id='shield' style=/""+shieldStyle+"/"></div>").appendTo("body");
        }
        //拖動(dòng)時(shí)執(zhí)行的函數(shù)
        function mouseDrag(){
            if(drag_flag==true){
                if (window.event.button==1){
                    var now_x=event.x;
                    var value=parseInt($("#alphaDiv")[0].style.left)+now_x-begin_x;
                    $("#alphaDiv")[0].style.left=value+"px";
                     begin_x=now_x;
                }    
                $("body").css("cursor","e-resize");    //設(shè)定光標(biāo)類型
            }else{
                try{
                    $("#shield").remove();
                    $("#sideBar")[0].style.pixelWidth=$("#alphaDiv")[0].style.left;
                    $("#alphaDiv").remove();
                }catch(e){}
            }
        }

        function mouseDragEnd(){
            //設(shè)置拖動(dòng)條的位置
            if(drag_flag==true){
                //設(shè)定拖動(dòng)條的位置(設(shè)定左側(cè)的寬度)
                $("#sideBar")[0].style.pixelWidth=parseInt($("#alphaDiv")[0].style.left);
                $("#shield").remove();    //刪除蒙板
                $("#alphaDiv").remove();    //刪除半透明拖動(dòng)條
                $("body").css("cursor","normal");    //恢復(fù)光標(biāo)類型
            }
            drag_flag=false;
        }
    </script>
    </head>
    <body>
        <table id="tbl" border="0" bordercollASPe="collapse" cellpadding="2" cellspacing="0" width="100%" height="100%">
            <tr>
                <td width="1"><div id="sideBar" style="width:200px;"><div style="height:1200px">asdfasdf</div></div>
                </td>
                <td width="1" onmousedown="setDrag()" id="toggleBar"></td>
                <td id="main">
                    <iframe src="test.htm" id="frmMain" width="100%" height="100%"></iframe>
                </td>
            </tr>
        </table>
    </body>
</html>
自己的一點(diǎn)發(fā)現(xiàn),一點(diǎn)心得,不知對大家有沒有用處,只管拿出來獻(xiàn)丑了!

JavaScript技術(shù)一個(gè)剛完成的layout(拖動(dòng)流暢,不受iframe影響),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 白水县| 什邡市| 浠水县| 大渡口区| 双牌县| 金川县| 乌苏市| 晋宁县| 隆安县| 娄底市| 九江县| 承德市| 永川市| 安泽县| 富阳市| 南江县| 和静县| 宁强县| 哈尔滨市| 高要市| 扶沟县| 垦利县| 高邮市| 伊吾县| 夏津县| 三门县| 江都市| 泉州市| 永仁县| 神池县| 天柱县| 随州市| 广丰县| 叙永县| 鄂托克旗| 沛县| 象山县| 福鼎市| 克什克腾旗| 金乡县| 来宾市|