bootstrap modal模态窗口拖拽功能


/** 拖拽模态框*/ 
        var dragModal={
            mouseStartPoint:{"left":0,"top":  0},
            mouseEndPoint : {"left":0,"top":  0},
            mouseDragDown : false,
            basePoint : {"left":0,"top":  0},
            moveTarget:null,
            topleng:0
        }
        $(document).on("mousedown",".modal-header",function(e){
            //webkit内核和火狐禁止文字被选中
            $('body').addClass('select')
            //ie浏览器禁止文字选中
            document.body.onselectstart=document.body.οndrag=function(){
                return false;
                }
            if($(e.target).hasClass("close"))//点关闭按钮不能移动对话框  
                return;  
            dragModal.mouseDragDown = true;  
            dragModal.moveTarget = $(this).parent().parent();         
            dragModal.mouseStartPoint = {"left":e.clientX,"top":  e.pageY};  
            dragModal.basePoint = dragModal.moveTarget.offset();  
            dragModal.topLeng=e.pageY-e.clientY;
        });  
        $(document).on("mouseup",function(e){       
            dragModal.mouseDragDown = false;  
            dragModal.moveTarget = undefined;  
            dragModal.mouseStartPoint = {"left":0,"top":  0};  
            dragModal.basePoint = {"left":0,"top":  0};  
        });  
        $(document).on("mousemove",function(e){  
            if(!dragModal.mouseDragDown || dragModal.moveTarget == undefined)return;          
            var mousX = e.clientX;  
            var mousY = e.pageY;  
            if(mousX < 0)mousX = 0;  
            if(mousY < 0)mousY = 25;  
            dragModal.mouseEndPoint = {"left":mousX,"top": mousY};  
            var width = dragModal.moveTarget.width();  
            var height = dragModal.moveTarget.height();
            var clientWidth=document.body.clientWidth
            var clientHeight=document.body.clientHeight;
            if(dragModal.mouseEndPoint.left<dragModal.mouseStartPoint.left - dragModal.basePoint.left){
                dragModal.mouseEndPoint.left=0;
            }
            else if(dragModal.mouseEndPoint.left>=clientWidth-width+dragModal.mouseStartPoint.left - dragModal.basePoint.left){
                dragModal.mouseEndPoint.left=clientWidth-width-38;
            }else{
                dragModal.mouseEndPoint.left =dragModal.mouseEndPoint.left-(dragModal.mouseStartPoint.left - dragModal.basePoint.left);//移动修正,更平滑  
                
            }
            if(dragModal.mouseEndPoint.top-(dragModal.mouseStartPoint.top - dragModal.basePoint.top)<dragModal.topLeng){
                dragModal.mouseEndPoint.top=dragModal.topLeng;
            }else if(dragModal.mouseEndPoint.top-dragModal.topLeng>clientHeight-height+dragModal.mouseStartPoint.top - dragModal.basePoint.top){
                dragModal.mouseEndPoint.top=clientHeight-height-38+dragModal.topLeng;
            }
            else{
                dragModal.mouseEndPoint.top = dragModal.mouseEndPoint.top - (dragModal.mouseStartPoint.top - dragModal.basePoint.top);           
            }
            dragModal.moveTarget.offset(dragModal.mouseEndPoint);  
        });   
        $(document).on('hidden.bs.modal','.modal',function(e){
            $('.modal-dialog').css({'top': '0px','left': '0px'})
            $('body').removeClass('select')
            document.body.onselectstart=document.body.οndrag=null;
        })

演示案例:

https://guguji5.github.io/bs-modal-dragable/bs-modal-dragable.html


bootstrap模态框拖拽效果,bootstrap拖拽模态框,bootstrap模态框

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智能体格

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值