如何让弹出窗口可以拖动呢?

如何做出可以拖动的对话框呢?

实际上弹出窗口就是一个div,范例:

Js代码  可以拖动的弹出窗
  1. <!--  弹出窗口层 -->  
  2.     <div id="subPagePanel"  style="display:none;"  >  
  3.     <h2 style="color: #fff;font-weight: bold;" class="ui-icon-close" ><label>发帖</label>  
  4.         <a title="关闭" onclick="closeSubPagePanel();" style="margin-top: 4px;margin-right: 4px;  " class="close" ></a>  
  5.         </h2>  
  6.         <div id="subContent" ><!-- <img style="margin:500px;width:50px" src="<%=path%>/static/images/loading/progress.gif"> -->  
  7.           
  8.   
  9.     </div>  
  10.   
  11. </div>  
  12.     <!-- / 弹出窗口层 -->  

对应的css:

Css代码  可以拖动的弹出窗
  1. #subPagePanel {  
  2.     overflow-y: auto;  
  3.     overflow-x: auto;  
  4.     width: 640px;  
  5.     /* top: 10px !important; */  
  6.     left: 10px;  
  7.     height: auto/* 320px */;  
  8.     position: absolute;  
  9.     background-color: rgba(2552552550.9);  
  10.     z-index: 99996;  
  11.     background-repeat: no-repeat;  
  12.     background-position: center;  
  13.     display: none;  
  14.     border:1px solid #ea4748;  
  15.     border-radius: 5px;  
  16. }  
  17. #subPagePanel h2{  
  18.     background-color: #ea4748;  
  19.     height: 40px;  
  20.     line-height: 40px;  
  21.     padding-left: 5px;  
  22.     cursor: move;  
  23. }  

 js 方法:

Js代码  可以拖动的弹出窗
  1. drag = function ($obj) {  
  2.     if (arguments.length == 0) {  
  3.         return;  
  4.     }  
  5.     if ($obj == null || $obj == undefined) {  
  6.         return;  
  7.     }  
  8.     if (typeof  $obj == 'string') {//when $obj is a string  
  9.         $obj = $($obj);  
  10.     }  
  11.     $obj.on({  
  12.         mousedown: function (e) {  
  13.             e.preventDefault();  
  14.             var t = $obj.offset(),  
  15.                 o = e.pageX - t.left,  
  16.                 i = e.pageY - t.top;  
  17.             $(document).on("mousemove.drag"function (e) {  
  18.                 $obj.offset({  
  19.                     top: e.pageY - i,  
  20.                     left: e.pageX - o  
  21.                 })  
  22.             })  
  23.         },  
  24.         mouseup: function () {  
  25.             $(document).unbind("mousemove.drag")  
  26.         }  
  27.     });  
  28. };//drag  

  

在页面加载完时就执行drag操作:

Js代码  可以拖动的弹出窗
  1. $(function(){  
  2.   
  3.     drag("#subPagePanel");  
  4.   
  5. });  

看看效果:

 
可以拖动的弹出窗
 

 今天又进行了优化:

Js代码  可以拖动的弹出窗
  1. drag = function ($obj, hn) {  
  2.     if (arguments.length == 0) {  
  3.         return;  
  4.     }  
  5.     if ($obj == null || $obj == undefined) {  
  6.         return;  
  7.     }  
  8.     if (typeof  $obj == 'string') {//when $obj is a string  
  9.         $obj = $($obj);  
  10.     }  
  11.     var $hn = null;  
  12.     if (arguments.length > 1) {  
  13.         $hn = $obj.find(hn);//div h1,h2...  
  14.     } else {  
  15.         $hn = $obj.find("h2");  
  16.     }  
  17.     $hn.on({  
  18.         mousedown: function (e) {  
  19.             e.preventDefault();  
  20.             var t = $obj.offset(),  
  21.                 o = e.pageX - t.left,  
  22.                 i = e.pageY - t.top;  
  23.             //$obj.css("position", 'fixed');  
  24.             $(document).on("mousemove.drag"function (e) {  
  25.                 $obj.offset({  
  26.                     top: e.pageY - i,  
  27.                     left: e.pageX - o  
  28.                 })  
  29.             })  
  30.         },  
  31.         mouseup: function () {  
  32.             $(document).unbind("mousemove.drag");  
  33.             $obj.css("position"'fixed');  
  34.         }  
  35.     });  
  36. };//drag  

 下面的方法是让对话框显示出来:

Js代码  可以拖动的弹出窗
  1. var ajaxSubPanel=function(url)  
  2. {  
  3.     $subContent=$("#subContent");  
  4.     showLoadPanel(server_url_prefix+"static/img/loading/progress.gif");  
  5.     ajaxHtml(url+"&recordsPerPage=5&random22="+Math.random(),$subContent);//page.js  
  6.     $subPagePanel = $('#subPagePanel');  
  7.     //$subPagePanel.css("position", 'absolute');//保证下面的语句生效  
  8.     $subPagePanel.css("top", (/*com.whuang.hsj.getScroll().top+*/10) + "px");//弹出panel兼容滚动条  
  9.     $cboxOverlay.height($(document).height());  
  10.     $cboxOverlay.show();  
  11.     $subPagePanel.show('normal');  
  12.     $subPagePanel.css("position"'fixed');//保证固定在可视范围内  
  13. };  

 
可以拖动的弹出窗
 

 

相关文章: