JQuery拖拽移动

/**
 * zzh_2022032101_拖拽移动
 * @param obj 目标对象 / #id / .class
 * @param moveOut 是否可以移出边界
 */
function dragMove(dom, moveOut) {
    var obj = $(dom);
    moveOut = moveOut || false;
    obj.bind('mousedown', start);
    var deltaX = 0, deltaY = 0;
    function start(e) {
        var ol = obj.offset().left;
        var ot = obj.offset().top;
        deltaX = e.pageX - ol;
        deltaY = e.pageY - ot;
        $(document).bind({
            'mousemove': move,
            'mouseup': stop
        });
        return false;
    }
    function move(e) {
        var tpX = 0, tpY = 0;
        if ((e.pageX - deltaX > 0 && e.pageY - deltaY > 0) || moveOut) {
            tpX = e.pageX - deltaX;
            tpY = e.pageY - deltaY;
        } else if (e.pageX - deltaX <= 0 && e.pageY - deltaY > 0) {
            tpX = 0;
            tpY = e.pageY - deltaY;
        } else if (e.pageX - deltaX > 0 && e.pageY - deltaY <= 0) {
            tpX = e.pageX - deltaX;
            tpY = 0;
        }
        obj.css({
            "left": (tpX),
            "top": (tpY)
        });
        return false;
    }
    function stop() {
        $(document).unbind({
            'mousemove': move,
            'mouseup': stop
        });
    }
}

 

posted @ 2022-03-21 10:08  散人长情  阅读(230)  评论(0编辑  收藏  举报