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

按照普通的方法寫一個layout,一般是用一個table來實 " /> 黄色免费看网站,337p日本大胆欧美人术艺术精品,在线播放成人

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

寫了一個layout,拖動條連貫,內(nèi)容區(qū)可為iframe

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

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

按照普通的方法寫一個layout,一般是用一個table來實現(xiàn),用中間的td拖動來控制左右兩個td的大小,這個問題簡單,很快就搞定。代碼如下:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
5 <title>Untitled Document</title>
6 <style type="text/css">
7 *{margin:0px;padding:0px}
8 html{overflow:hidden}
9 #sideBar{width:200px;height:100%;overflow:auto}
10 #toggleBar,.div{
11 width:7px;height:100%;
12 overflow:hidden;background:#eee;
13 cursor:e-resize;border-left:1px solid #ccc;border-right:1px solid #ccc;
14 }
15 td{display:block;overflow:auto;word-break:break-all;}
16 </style>
17 <script type="text/Javascript" src="../Common/jquery.gif"></script>
18 <script type="text/Javascript">
19 $(document).ready(function(){
20 //及時調(diào)整頁面內(nèi)容的高度
21 setInterval(function(){
22 var winH=(document.documentElement||document.body).clientHeight;
23 $("#tbl,#sideBar,#toggleBar,#main").css("height",winH);
24 $("td").each(function(){$(this).html()||$(this).html(" ")});
25 },100)
26 }
27 );
28
29 var begin_x;
30 var drag_flag = false;
31 document.onmousemove = mouseDrag
32 document.onmouseup = mouseDragEnd
33 //半透明拖動條
34 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>";
35 function setDrag(){
36 drag_flag=true;
37 begin_x=event.x;
38 //添加半透明拖動條
39 $(alphaDiv).css("left",$("#toggleBar")[0].offsetLeft).appendTo("body");
40 }
41
42 //拖動時執(zhí)行的函數(shù)
43 function mouseDrag(){
44 if(drag_flag==true){
45 if (window.event.button==1){
46 var now_x=event.x;
47 var value=parseInt($("#alphaDiv")[0].style.left)+now_x-begin_x;
48 $("#alphaDiv")[0].style.left=value+"px";
49 begin_x=now_x;
50 }
51 $("body").css("cursor","e-resize"); //設(shè)定光標(biāo)類型
52 }else{
53 try{
54 $("#sideBar")[0].style.pixelWidth=$("#alphaDiv")[0].style.left;
55 $("#alphaDiv").remove();
56 }catch(e){}
57 }
58 }
59
60 function mouseDragEnd(){
61 //設(shè)置拖動條的位置
62 if(drag_flag==true){
63 //設(shè)定拖動條的位置(設(shè)定左側(cè)的寬度)
64 $("#sideBar")[0].style.pixelWidth=parseInt($("#alphaDiv")[0].style.left);
65 $("#alphaDiv").remove(); //刪除半透明拖動條
66 $("body").css("cursor","normal"); //恢復(fù)光標(biāo)類型
67 }
68 drag_flag=false;
69 }
70 </script>
71 </head>
72 <body>
73 <table id="tbl" border="0" bordercollASPe="collapse" cellpadding="2" cellspacing="0" width="100%" height="100%">
74 <tr>
75 <td width="1"><div id="sideBar" style="width:200px;"><div style="height:1200px">asdfasdf</div></div>
76 </td>
77 <td width="1" onmousedown="setDrag()" id="toggleBar"></td>
78 <td id="main">
79 right Panel
80 </td>
81 </tr>
82 </table>
83 </body>
84 </html>演示地址:http://www.ajaxbbs.NET/test/layout/JqSplit/noiframe.htm
上面的這種寫法也是大多數(shù)layout的寫法,著名框架dojo好像也是這么實現(xiàn)的,其他的沒試。

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

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

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

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

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

演示地址:
http://www.ajaxbbs.NET/test/layout/JqSplit/demo.htm
我們看一下完整的代碼:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
5 <title>Untitled Document</title>
6 <style type="text/css">
7 *{margin:0px;padding:0px}
8 html{overflow:hidden}
9 #sideBar{width:200px;height:100%;overflow:auto}
10 #toggleBar,.div{
11 width:7px;height:100%;
12 overflow:hidden;background:#eee;
13 cursor:e-resize;border-left:1px solid #ccc;border-right:1px solid #ccc;
14 }
15 td{display:block;overflow:auto;word-break:break-all;}
16 </style>
17 <script type="text/Javascript" src="../Common/jquery.js"></script>
18 <script type="text/Javascript">
19 $(document).ready(function(){
20 //及時調(diào)整頁面內(nèi)容的高度
21 setInterval(function(){
22 var winH=(document.documentElement||document.body).clientHeight;
23 $("#tbl,#sideBar,#toggleBar,#main").css("height",winH);
24 $("td").each(function(){$(this).html()||$(this).html(" ")});
25 },100)
26 }
27 );
28
29 var begin_x;
30 var drag_flag = false;
31 document.onmousemove = mouseDrag
32 document.onmouseup = mouseDragEnd
33 //半透明的拖動條(模擬)
34 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>";
35 function setDrag(){
36 drag_flag=true;
37 begin_x=event.x;
38 //添加蒙板
39 createMask();
40 //添加半透明拖動條
41 $(alphaDiv).css("left",$("#toggleBar")[0].offsetLeft).appendTo("body");
42 }
43
44 //關(guān)鍵部分
45 function createMask(){
46 //創(chuàng)建背景
47 var rootEl=document.documentElement||document.body;
48 var docHeight=((rootEl.clientHeight>rootEl.scrollHeight)?rootEl.clientHeight:rootEl.scrollHeight)+"px";
49 var docWidth=((rootEl.clientWidth>rootEl.scrollWidth)?rootEl.clientWidth:rootEl.scrollWidth)+"px";
50 var shieldStyle="position:absolute;top:0px;left:0px;width:"+docWidth+";height:"+docHeight+";background:#000;z-index:10000;filter:alpha(opacity=0);opacity:0";
51 $("<div id='shield' style=/""+shieldStyle+"/"></div>").appendTo("body");
52 }
53 //拖動時執(zhí)行的函數(shù)
54 function mouseDrag(){
55 if(drag_flag==true){
56 if (window.event.button==1){
57 var now_x=event.x;
58 var value=parseInt($("#alphaDiv")[0].style.left)+now_x-begin_x;
59 $("#alphaDiv")[0].style.left=value+"px";
60 begin_x=now_x;
61 }
62 $("body").css("cursor","e-resize"); //設(shè)定光標(biāo)類型
63 }else{
64 try{
65 $("#shield").remove();
66 $("#sideBar")[0].style.pixelWidth=$("#alphaDiv")[0].style.left;
67 $("#alphaDiv").remove();
68 }catch(e){}
69 }
70 }
71
72 function mouseDragEnd(){
73 //設(shè)置拖動條的位置
74 if(drag_flag==true){
75 //設(shè)定拖動條的位置(設(shè)定左側(cè)的寬度)
76 $("#sideBar")[0].style.pixelWidth=parseInt($("#alphaDiv")[0].style.left);
77 $("#shield").remove(); //刪除蒙板
78 $("#alphaDiv").remove(); //刪除半透明拖動條
79 $("body").css("cursor","normal"); //恢復(fù)光標(biāo)類型
80 }
81 drag_flag=false;
82 }
83 </script>
84 </head>
85 <body>
86 <table id="tbl" border="0" bordercollASPe="collapse" cellpadding="2" cellspacing="0" width="100%" height="100%">
87 <tr>
88 <td width="1"><div id="sideBar" style="width:200px;"><div style="height:1200px">asdfasdf</div></div>
89 </td>
90 <td width="1" onmousedown="setDrag()" id="toggleBar"></td>
91 <td id="main">
92 <iframe src="test.htm" id="frmMain" width="100%" height="100%"></iframe>
93 </td>
94 </tr>
95 </table>
96 </body>
97 </html>
算是自己的一點(diǎn)發(fā)現(xiàn),一點(diǎn)心得吧,不知對大家有沒有用處,只管拿出來獻(xiàn)丑了!(首發(fā)于藍(lán)色經(jīng)典)

JavaScript技術(shù)寫了一個layout,拖動條連貫,內(nèi)容區(qū)可為iframe,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 1区2区3区集团有限公司 | 日本加勒比系列 | 久久国产乱子伦精品免 | 成人永久免费 | 九九亚洲 | 黄免费观看 | 精品一区二区三区四区五区六区 | 一木道一二三区精品 | 综合久久久久久久综合网 | 色香蕉网| 国产亚洲精品国产福利在线观看 | 中文字幕第二一区 | 国内精品久久久久激情影院 | 中文字幕亚洲无线码在线一区 | 久久九九精品视频 | 日韩亚洲一区中文字幕在线 | 99久久久精品免费观看国产 | 亚洲一区小说区中文字幕 | 看全色黄大色黄大片免责看 | 怡红院在线播放 | 99国内视频 | my blow jober act| 日韩激情影院 | 久久精品成人免费网站 | 亚洲综合色婷婷久久 | 国产小视频福利 | 成人黄色免费网址 | 中文字幕在线网站 | 国产日韩欧美综合色视频在线 | 亚洲国产成人在线观看 | 国产夫妻av | 久久国产乱子伦精品免费一 | 色综合老色鬼狼天天 | 精品国产精品国产 | 亚洲一二区视频 | www.欧美在线 | 欧美一级高跟鞋鞋交 | 91精品国产91久久久久久 | 在线视频精品视频 | 久久这里精品青草免费 | 国产亚洲精品97在线观看 |