<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery - Drag</title>
<style>
.modal {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(100, 100, 100, 0.5);
z-index: 2001;
} .modal-content {
position: absolute;
width: 500px;
height: 300px;
left: 50%;
margin-left: -250px;
margin-top: 100px;
border: 10px solid rgba(150, 150, 150, 0.5);
background-color: white;
text-align: center;
} .modal-title {
height: 38px;
border-bottom: 1px solid blue;
text-align: left;
padding: 0 10px;
font-weight: bold;
cursor: move;
} .modal-title div {
display: inline-block;
font-size: larger;
line-height: 2;
padding: 0 8px;
} .modal-title .modal-close {
float: right;
cursor: pointer;
} .modal-title .modal-close:hover {
background-color: rgb(198, 236, 236);
}
</style>
</head>
<body>
<div>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
<p>This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. This is backend content. </p>
</div>
<div id="idModal" class="modal">
<div id="idModalDetail" class="modal-content">
<div id="idModalTitle" class="modal-title">
<div>可拖动</div>
<div class="modal-close">X</div>
</div>
<div class="modal-body">This is drag modal</div>
</div>
</div>
<script type="text/javascript" src="./jquery-2.2.4.js"></script>
<script>
// 拖动时,偶发选中文本,将该选择功能禁用
function cancelUserSelect(ele) {
if (!ele) return false;
document.body.style.userSelect = 'none';
document.body.style.webkitUserSelect = 'none';
document.body.style.msUserSelect = 'none';
document.body.style.mozUserSelect = 'none'; ele.style.userSelect = 'none';
ele.style.webkitUserSelect = 'none';
ele.style.msUserSelect = 'none';
ele.style.mozUserSelect = 'none';
} // 恢复禁用的文本选择功能
function restoreUserSelect(ele) {
if (!ele) return false;
document.body.style.userSelect = 'text';
document.body.style.webkitUserSelect = 'text';
document.body.style.msUserSelect = 'text';
document.body.style.mozUserSelect = 'text'; ele.style.userSelect = 'text';
ele.style.webkitUserSelect = 'text';
ele.style.msUserSelect = 'text';
ele.style.mozUserSelect = 'text';
} function drag() {
var $idModalTitle = $('#idModalTitle');
var $idDragModal = $idModalTitle.parent(); var mouseStartPoint = {left: 0, top: 0};
var mouseEndPoint = {left: 0, top: 0};
var oldPoint = {left: 0, top: 0};
var isDraging = false; $(document).on('mousedown', '.modal-title', function(e) {
// 关闭按钮不进行移动
if ($(e.target).hasClass('modal-close')) return false;
isDraging = true; mouseStartPoint = {left: e.clientX, top: e.clientY};
oldPoint = $idDragModal.offset(); // 鼠标离模态框边距值
cancelUserSelect($idDragModal.get(0));
}); $(document).on('mousemove', function(e) {
if (!isDraging) return false; if ($idDragModal.css('margin-left') !== 0) {
$idDragModal.css('margin-left', 0);
} if ($idDragModal.css('margin-top') !== 0) {
$idDragModal.css('margin-top', 0);
} var mouseX = e.clientX;
var mouseY = e.clientY; if (mouseX < 0) mouseX = 0;
if (mouseY < 0) mouseY = 25; mouseEndPoint = {left: mouseX, top: mouseY}; var newPoint = {
left: mouseEndPoint.left - (mouseStartPoint.left - oldPoint.left),
top: mouseEndPoint.top - (mouseStartPoint.top - oldPoint.top)
}; $idDragModal.offset(newPoint);
}); $(document).on('mouseup', function(e) {
if (!isDraging) return false;
isDraging = false;
restoreUserSelect($idDragModal.get(0));
});
} drag();
</script>
</body>
</html>

[TimLinux] JavaScript 模态框可拖动功能实现——jQuery版的更多相关文章

  1. [TimLinux] JavaScript 模态框可拖动功能实现——节流版

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. [TimLinux] JavaScript 模态框可拖动功能实现——原始版

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 完美解决bootstrap模态框允许拖动后拖出边界的问题

    使用bootstrap3版本 在网上看了很多方法,我觉得jquery-ui的实现方法是最简单有效的,具体实现方法 1.下载并引入jquery-ui插件 2.全局添加模态框允许拖动事件 $(docume ...

  4. [TimLinux] JavaScript input框的onfocus/onblur/oninput/onchange事件介绍

    1. onfocus事件 input框获取到焦点时,触发了该事件,比如获取到焦点时,修改input框的背景色.这个功能其实可以使用css的伪类:focus来定义. 2. onblur事件 这个与onf ...

  5. BootStrap 模态框禁用空白处点击关闭问题

    模态框为信息编辑窗口,涉及好多内容,填了半天,若一不小心点了空白处,那就前功尽弃了..... 所以我们很有必要禁用鼠标点击空白处模态框关闭的功能. $('#myModal').modal({backd ...

  6. iframe中的模态框遮罩父窗口原理

    关键点: css的position定位为fixed或absolute css的z-index, 最顶层的值最大如遮罩层为0那么弹出框最好是大于等于1的整数,总之记住弹出层要比遮罩的z-index值大就 ...

  7. bootstrap模态框关闭后清除模态框的数据

    https://segmentfault.com/q/1010000008789123 bootstrap模态框第二次打开时如何清除之前的数据? 我用了bootstrap模态框的remote功能,在弹 ...

  8. Bootstrap——可拖动模态框(Model)

    还是上一个小项目,o(╥﹏╥)o,要实现点击一个div或者button或者一个东西然后可以弹出一个浮在最上面的弹框.网上找了找,发现Bootstrap的Model弹出框可以实现该功能,因此学习了一下, ...

  9. 使用js实现显示系统当前时间并实现倒计时功能并触发模态框(遮罩)功能

    常常在我们的网页中需要倒计时来触发一些函数,例如遮罩等,在本项目中,通过使用jquery,bootstrap,实现了显示系统当前时间,并且实现了倒计时的功能,倒计时实现后将会弹出模态框(遮罩层).模态 ...

随机推荐

  1. 数据结构之队列and栈总结分析

    一.前言: 数据结构中队列和栈也是常见的两个数据结构,队列和栈在实际使用场景上也是相辅相成的,下面简单总结一下,如有不对之处,多多指点交流,谢谢. 二.队列简介 队列顾名思义就是排队的意思,根据我们的 ...

  2. Java多态——代码示例

    刚开始看多态的文字定义时,总是不明白说的啥意思,看了一些示例代码后,总算知道了,其实也就是“多态”的字面意思. 如下: class A{ public void Out() { System.out. ...

  3. VS 使用 :新建项目

    1.文件位置不放C盘

  4. nyoj 773-开方数 (pow)

    773-开方数 内存限制:64MB 时间限制:1000ms 特判: No 通过数:3 提交数:8 难度:3 题目描述: 现在给你两个数 n 和 p ,让你求出 p 的开 n 次方. 输入描述: 每组数 ...

  5. opencv 6 图像轮廓与图像分割修复 1 查找并绘制轮廓 寻找物体的凸包

    查找并绘制轮廓 寻找轮廓(findContours)函数 绘制轮廓(drawContours()函数) 基础实例程序:轮廓查找 #include <opencv2/opencv.hpp> ...

  6. 动手造轮子:实现简单的 EventQueue

    动手造轮子:实现简单的 EventQueue Intro 最近项目里有遇到一些并发的问题,想实现一个队列来将并发的请求一个一个串行处理,可以理解为使用消息队列处理并发问题,之前实现过一个简单的 Eve ...

  7. HBase 基本入门

    目录 一.简介 有什么特性 与RDBMS的区别 二.数据模型 三.安装HBase 四.基本使用 表操作 五.FAQ 参考文档 无论是 NoSQL,还是大数据领域,HBase 都是非常"炙热& ...

  8. LoadRunner中的90%响应时间

    LoadRunner中的90%响应时间是什么意思?这个值在进行性能分析时有什么作用? 为什么要有90%用户响应时间? 这个跟超女.舞林大会等比赛那样在比赛后都要去掉一个最高分一个最低分在取平均值有点类 ...

  9. linux工作调度(计划任务)

    linux工作调度有两种:at,cron · at:at是一个可以处理仅执行一次就结束调度的命令.说白了就是在某个时间需要干某一件事,例如在2018年10月12日下午一点要执行一个数据库矫正脚本. · ...

  10. Kotlin协程通信机制: Channel

    Coroutines Channels Java中的多线程通信, 总会涉及到共享状态(shared mutable state)的读写, 有同步, 死锁等问题要处理. 协程中的Channel用于协程间 ...