jQuery实现弹出框拖拽

主要应用jQuery的on和off方法实现在 mousedown 的情况下进行mousemove。

其他:$(window).width() 、$(window).height() ---- 获取浏览页面的宽高。

   pageX、pagerY(ie8不兼容)或 clientX、clientY --------- 获取鼠标当前位置的横轴纵轴。

   offset().left、offset().top------------------------- div距离左边界的长度和上边界的长度。

下面是简易实现方法:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>移动式弹出框</title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
                overflow: hidden;
                background-color: rgba(215,215,215,0.2);
            }
            .eject{
                height: 200px;
                width: 400px;
                background-color: blue;
                position: absolute;
                top: 100px;
                left: 30%;
            }
            .eject_move{
                width: 400px;
                height: 50px;
                background-color: red;
                cursor: move;
                line-height: 50px;
                text-align: center;
                font-size: 2em;
            }
        </style>
    </head>
    <body>
        <div class="eject">
            <div class="eject_move">拖动位置</div>
        </div>
        <script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript">
        //    可移动弹出框
            var eject_move = $(".eject_move");
            var eject = $(".eject");
            eject_move.mousedown(function(e){
                var max_x = $(window).width() - 380;            //获取浏览页面的宽度
                var max_y = $(window).height() -200;
                var ev = window.event || e;
                var old_mouse_x = ev.clientX;                        //获取鼠标开始的横轴位置
                var old_mouse_y = ev.clientY;                        //获取鼠标开始的纵轴位置
                var position_l = eject.offset().left;            //弹出框距离的横轴位置
                var position_t = eject.offset().top;            //弹出框距离的纵轴位置
                eject_move.on(‘mousemove‘,function(n){
                    var nv = window.event || n;
                    var new_mouse_x = nv.clientX;                    //获取鼠标现在的横轴位置
                    var new_mouse_y = nv.clientY;                    //获取鼠标现在的纵轴位置
                    var new_x = new_mouse_x - old_mouse_x +position_l;    //弹出框现在的横轴位置
                    var new_y = new_mouse_y - old_mouse_y +position_t;    //弹出框现在的纵轴位置
                    //三元表达式 判断有没有超出边界,有的话置于相应的值
                    new_x = (new_x < 0 )?0:new_x;
                    new_y = (new_y < 0 )?0:new_y;
                    new_x = (new_x > max_x)?max_x:new_x;
                    new_y = (new_y > max_y)?max_y:new_y;
                    eject.css({‘left‘:new_x,‘top‘:new_y});
                });

            });
        //    鼠标抬起
            eject.mouseup(function(){
                eject_move.off(‘mousemove‘);
            });
        </script>
    </body>
</html>

      jQuery兼容ie7的前提:

chrome firefox ie7 ie8+

 

虽然ie兼容,但是这种写法存在弊端。

导致ie拖动时会出现卡顿现象。而火狐谷歌卡顿现象就不明显。

时间: 2024-08-03 11:19:33

jQuery实现弹出框拖拽的相关文章

jquery实战——弹出框拖拽效果

今天主要记录一下弹出框拖拽效果: 一.移动弹出窗口的步骤是:按下鼠标左键——移动鼠标——松开鼠标左键停止移动 二.主要思想: 按下鼠标左键:$('div').mousedown(function(e){}) 移动鼠标,获取鼠标当前坐标值:$(document).mousemove(function(e){}) 松开鼠标左键停止移动:$('div').mouseup(function(e){ $(document).unbind('mousemove');  //即当鼠标左键被释放时解除mouse

js简单显示和隐藏div,触发超链接,动态更改button值,setInterval()简单使用,jquery easyui弹出框简单使用 .

js简单显示和隐藏div 01.<!DOCTYPE html> 02.<html> 03.<head> 04.<meta charset="UTF-8"> 05.<title>Insert title here</title> 06.<script type="text/javascript"> 07. window.onload=function(){ 08. document.g

jQuery+css3弹出框插件

先来看DEMO:https://codepen.io/jonechen/pen/regjGG 插件的开发很简单,运用了CSS3的动画效果,并且弹出框的内容可以自定义.插件的默认配置参数有三个: var defaults = { event: 'click', // 默认点击事件 dialogBox: 'selector', // 弹出框容器 hover: false, // 默认滑动事件关闭 } CSS部分: *{padding: 0; margin: 0; list-style: none;f

弹出层拖拽案例

style代码 <style> * { margin: 0; padding: 0; } .login-header { width: 100%; text-align: center; height: 30px; font-size: 24px; line-height: 30px; } ul, li, ol, dl, dt, dd, div, p, span, h1, h2, h3, h4, h5, h6, a { padding: 0px; margin: 0px; } .login {

jquery弹出层拖拽

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css">

jquery UI 弹出框

2015-07-17 11:04:38 1 <div id="reg"></div> 2 <script type="text/javascript"> 3 var dg = $("#reg").dialog( 4 { 5 autoOpen:false, 6 // modal:true, 7 show:"blind", // clip,drop,explode,fold,puff,slide

jquery Bootstrap 弹出框(Popover)显示html内容,URL,div等

实现效果: 其实有个关键点: data-container="body" data-html="true" 注意下就可以了 <button type="button" class="btn btn-success" title="Popover title" data-container="body" data-html="true" data-toggle=&

显示遮罩层和弹出框

因为最近用到了遮罩层和弹出框,上网查资料后自己完成了一个,在此做一下记录做保存. 因为用到了jq,所以需要引入jq文件,比如jquery-1.11.3.js 以下是jsp的代码: <body> <div> <input id="button" type="button" value="点击我"> <div class="shareDialog"> <div class=&q

弹出框组件,可拖拽

/** * 弹出框组件 */ (function($) { var Utils = { showMask: function() { var $mask = $("#mask"); if( $mask.length === 0 ) { $('body').prepend("<div id='mask' class='mask'></div>"); } $("#mask").css({ width: Math.max(doc