博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
可以拖动的弹出窗
阅读量:7065 次
发布时间:2019-06-28

本文共 2747 字,大约阅读时间需要 9 分钟。

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

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

实际上弹出窗口就是一个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="关闭" οnclick="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. };  

 

 

 

转载地址:http://dksll.baihongyu.com/

你可能感兴趣的文章
java中常见的异常类型
查看>>
python中list和tuple的区别
查看>>
利用 docker 配置 pinbot-crawl 环境
查看>>
Tigase XMPP Server源码部署
查看>>
Intellij IDEA创建Maven Web项目
查看>>
java 7 入门书籍
查看>>
Android Pdf文档的生成、显示与打印
查看>>
SpringMVC三种异常处理方式
查看>>
w命令
查看>>
golang使用oracle碰到go/lib/time/zoneinfo.zip: no such file or directory
查看>>
quartz定时任务时间设置描
查看>>
ES6常用语法
查看>>
https://www.jianshu.com/p/dbffae16ba0b
查看>>
微信,QQ这类IM app怎么做——谈谈Websocket
查看>>
在Ubuntu 11.04中安装Openresty
查看>>
JAVA常见的面试题
查看>>
《Python高效开发实战》实战演练——建立应用2
查看>>
导出容器快照,并导入为镜像
查看>>
撰写合格的REST API
查看>>
infoq 七牛云CTO
查看>>