wdDragWidth.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. function initWdDragWidth(offsetheight, topNum) {
  2. linewidth(offsetheight);
  3. }
  4. //初始化拖动线
  5. function linewidth(offsetheight) {
  6. function getAttr(wdDragWidth, name) {
  7. /* 改,增加 {} 后,导致 第一个(min) 拿不到 -- "{min:300,max:1490,right:true}"。Lin
  8. var reg = new RegExp("(^|,)" + name + ":([^,]*)");
  9. */ var reg = new RegExp("(^{|,)" + name + ":([^,]*)");
  10. var r = wdDragWidth.match(reg);
  11. if(r != null) return unescape(r[2]);
  12. return null;
  13. }
  14. var dw = {};
  15. var wdDragWidthAry = $("[ssDragw]"); // = $("[wdDragWidth]")。Lin
  16. if(wdDragWidthAry.length!=2)return;1
  17. var leftSide=true;//拖动手柄的方向
  18. for(var i = 0; i < wdDragWidthAry.length; i++) {
  19. var wdDragWidth = $(wdDragWidthAry[i]).attr("ssDragw"); // ("wdDragWidth")。Lin
  20. /* 改,side:right 改为 right:true -- 只写 right:true,不写 right:false。Lin
  21. if(wdDragWidth.indexOf("side")>0){
  22. leftSide=("left"==getAttr(wdDragWidth, "side"))?true:false;
  23. }
  24. */ leftSide = wdDragWidth.indexOf("right") < 0;
  25. if(i==1) {
  26. dw.domB = $(wdDragWidthAry[i]);
  27. }else {
  28. dw.domAWidth = $(wdDragWidthAry[i]).width();
  29. dw.domA = $(wdDragWidthAry[i]);
  30. var max =wdDragWidth.indexOf("max")>-1? getAttr(wdDragWidth, "max").replace("px"):0;
  31. var min =wdDragWidth.indexOf("min")>-1? getAttr(wdDragWidth, "min").replace("px"):0;
  32. dw.maxWidth = parseInt(max);
  33. dw.minWidth = parseInt(min);
  34. }
  35. }
  36. if(dw.domA){
  37. $("#dragIcon").remove();
  38. dw.dragDom=$('<div id="dragIcon" class="'+(leftSide?'content-vSeperator-left':'content-vSeperator')+'"><span></span></div>');
  39. //dw.dragDom=$("<div id=\"dragIcon\" style='width: 12px;height: 518px;background: red;float: left;cursor: w-resize;'></div>");
  40. dw.domA.after(dw.dragDom);
  41. $("<div id=\"dragwidthmask\" style=\"left:0px;top:0px;z-index: 999;display: none;position: absolute;\"></div>").css({width:$(window).width(),height:$(window).height()}).appendTo("body");
  42. }
  43. // dw.countWidth = $(dw.domA).width() + $(dw.domB).width();
  44. dw.x = 0; // 鼠标按下时光标的X值
  45. dw.w = 0; // 拖拽前div的宽
  46. $(dw.dragDom)[0].onmousedown = function(ev) {
  47. if(!dw.countWidth){
  48. dw.countWidth = $(dw.domA).width() + $(dw.domB).width();
  49. }
  50. var ev = ev || window.event;
  51. dw.x = ev.clientX; // 获取鼠标按下时光标x的值
  52. dw.w = $(dw.domA).width(); // 拖动前的宽度
  53. $("#dragwidthmask").show();
  54. function dragmoveEnd(){
  55. document.onmousemove = null;
  56. document.onmouseup = null;
  57. $("#dragwidthmask").hide();
  58. }
  59. var mousemoveid=null;
  60. document.onmousemove = function(ev) {
  61. if(mousemoveid){
  62. clearTimeout(mousemoveid)
  63. mousemoveid=null;
  64. }
  65. mousemoveid=setTimeout(dragmoveEnd,3000);
  66. var ev = ev || window.event;
  67. var W = ev.clientX - dw.x + dw.w;
  68. if(W < dw.minWidth) {
  69. W = dw.minWidth;
  70. }else if(W > dw.maxWidth){
  71. W = dw.maxWidth;
  72. }
  73. $(dw.domA).width(W + 'px');
  74. $(dw.domB).width((dw.countWidth - W) + 'px');
  75. }
  76. document.onmouseup = function() {
  77. dragmoveEnd()
  78. }
  79. return false;
  80. }
  81. $(dw.dragDom)[0].ondblclick = function() {
  82. // 如果当前是最小状态,则直接恢复最大宽度
  83. // if($(dw.domA).width() <= dw.minWidth) {
  84. // $(dw.domA).width(dw.maxWidth);
  85. // $(dw.domB).width(dw.countWidth - dw.maxWidth);
  86. // return;
  87. //}
  88. $(dw.domA).width(dw.domAWidth + "px");
  89. $(dw.domB).width((dw.countWidth - dw.domAWidth) + "px");
  90. }
  91. }