[TimLinux] JavaScript 模态框可拖动功能实现——jQuery版

<!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>

原文地址:https://www.cnblogs.com/timlinux/p/11231499.html

时间: 2024-08-29 11:31:00

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

js实现模态框的拖曳功能

项目中需要对模态框进行拖曳,以便对比模态框和页面中的元素,现demo如下: 1 <!-- saved from url=(0066)https://guguji5.github.io/bs-modal-dragable/bs-modal-dragable.html --> 2 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8&qu

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

1. onfocus事件 input框获取到焦点时,触发了该事件,比如获取到焦点时,修改input框的背景色.这个功能其实可以使用css的伪类:focus来定义. 2. onblur事件 这个与onfocus事件想法,当input框失去焦点时,触发该事件,比如将输入的文本,统一修改为小写 3. onchange事件 这个事件的发生需要具有2个条件: 失去焦点 内容发生了变化 4. oninput事件 这个时HTML5新加入的事件,IE9之前是不支持的,在IE9之前使用的是: onproperty

实现搜索框自动提示功能(jquery+php)

客户端代码 <html> <head> <style> #search{font-size:14px;} #search .k{padding:2px 1px; width:320px;} #search_auto{border:1px solid #817FB2; position:absolute; display:none;} #search_auto li{background:#FFF; text-align:left;} #search_auto li.cl

模态框的弹出(移动版)

<!DOCTYPE html><html><head lang="zh-cn"> <meta charset="UTF-8"> <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" name="viewport"

Bootstrap 模态框强化

以下是基于bootstrap4版本做的模态框的强化功能,按需使用即可 1.ajax模态框 // ajax模态框 $('#ajaxModal').on('show.bs.modal', function (event) { var button = $(event.relatedTarget); var recipient = button.data('url'); var modal = $(this); modal.find('.modal-content').html(""); m

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

关键点: css的position定位为fixed或absolute css的z-index, 最顶层的值最大如遮罩层为0那么弹出框最好是大于等于1的整数,总之记住弹出层要比遮罩的z-index值大就行 如果弹出模态框的使用场景没有嵌套在iframe或frame中掌握以上两点加上点css功底就可以写出漂亮的模态框了 特殊场景会涉及到跨域问题,可以在父页面和子页面分别设置<script type="text/javascript">document.domain="

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

常常在我们的网页中需要倒计时来触发一些函数,例如遮罩等,在本项目中,通过使用jquery,bootstrap,实现了显示系统当前时间,并且实现了倒计时的功能,倒计时实现后将会弹出模态框(遮罩层).模态框界面友好,使用灵活,以弹出对话框的形式出现.具体见下图1: 项目源文件地址:https://github.com/zhangxy1035/Countdown 参考资料:http://v3.bootcss.com/javascript/#modals 图1: 关于倒计时函数如下: 1 var tim

Bootstrap3.0学习教程十七:JavaScript插件模态框

这篇文章中我们主要来学习一下JavaScipt插件模态框.在学习模态框之前,我们先来了解一下JavaScript插件吧. JavaScript插件概览 插件可以单个引入(使用Bootstrap提供的单个*.js文件),或一次性全部引入(使用bootstrap.js或压缩版的bootstrap.min.js). 不要将两份文件全部引入 bootstrap.js和bootstrap.min.js同样是包含了所有插件.区别是:一个没有压缩,一个进行了压缩. 插件之间的依赖 某些插件和CSS组件依赖于其

JavaScript:bootstrap 模态框的简单应用

最近用上了bootstrap这个强大的前端框架,有空来总结一下.这里记录下模态框的简单应用. 首先,要在页面中引入相应的js.css文件 1 <link href="css/bootstrap.css" rel="stylesheet" type="text/css" /> 2 <script type="text/javascript" src="js/jquery.min.js">