Fork me on GitHub

实现一个JQ拖拽插件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Just test</title>
    <style type="text/css">
        .drag
        {
            position: absolute;
            width: 100px;
            height: 100px;
            border: 1px solid #ddd;
            background: #FBF2BD;
            text-align: center;
            padding: 5px;
            top: 100px;
            left: 20px;
            cursor: move;
        }
    </style>
    <script src="jquery-1.7.2.js" type="text/javascript"></script>
    <script src="jquery.drag.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var _move = false; //移动标记
            var _x, _y; //鼠标离控件左上角的相对位置
            $(".drag").click(function () {
                //alert("click");//点击(松开后触发)
            }).mousedown(function (e) {
                _move = true;
                _x = e.pageX - parseInt($(".drag").css("left"));
                _y = e.pageY - parseInt($(".drag").css("top"));
                $(".drag").fadeTo(20, 0.5); //点击后开始拖动并透明显示
            });
            $(document).mousemove(function (e) {
                if (_move) {
                    var x = e.pageX - _x; //移动时根据鼠标位置计算控件左上角的绝对位置
                    var y = e.pageY - _y;
                    $(".drag").css({ top: y, left: x }); //控件新位置
                }
            }).mouseup(function () {
                _move = false;
                $(".drag").fadeTo("fast", 1); //松开鼠标后停止移动并恢复成不透明
            });
        });

        $(function () {
            //   $(".drag").drag();
        });
    </script>
</head>
<body>
    <div class="drag">
        亲,我是可以拖动哦!</div>
</body>
</html>

 

posted @ 2013-01-06 16:58  花儿笑弯了腰  阅读(360)  评论(0编辑  收藏  举报