0

付箋システムを作成していますが、付箋をjavascriptでドラッグできるようにしたいです。このサイトで非常に便利なドラッグ アンド ドロップ スクリプトを見つけました。しかし、特定の付箋をドラッグできる唯一の場所であるはずの付箋 div 内に「ドラッグ バー」があります。私が持っているスクリプトは、カーソルがクリックしているものだけをドラッグし、「.dragbar」div の親要素である「.parent」をドラッグしたいと考えています。

現在、スクリプトは、.parent をクリックしたときに .parent をドラッグするように設定されています。このコードで、「.parent」である「.dragbar」の親 div を選択するにはどうすればよいですか?

スクリプトで .dragbar を選択すると、.parent 内でドラッグバーが移動するだけです。注: スクリプトは元のコードですが、87 行目と 95 行目のセレクター名が「ドラッグ」から「親」に変更されています。

<!DOCTYPE html>
<html>

    <head>
        <style type="text/css">
            .pagecontent {
                width:98%;
                height:96%;
                overflow:hidden;
                background:lightyellow;
                border:1px solid black;
                padding:10px;
            }
            .parent {
                position:relative;
                height:300px;
                width:300px;
                background-color:gray;
                border:1px solid black;
                float:left;
                margin:0 10px 0 0;
                overflow:auto;
            }
            .dragbar {
                height:50px;
                width:300px;
                background:lightgray;
                cursor:move;
                position:relative;
            }
        </style>
    </head>

    <body>
        <div class="pagecontent">
            <h1>Dragging Practice</h1>
            <h3>Drag and drop the parent div by using the child dragbar</h3>
            <pre id="debug">mouse up</pre>
            <div class="parent">
                <div class="dragbar">I am the Child, my class is .dragbar</div>I am the Parent, my class is
                .parent</div>
            <div class="parent">
                <div class="dragbar">I am the Child, my class is .dragbar</div>I am the Parent, my class is
                .parent</div>
            <script language="JavaScript" type="text/javascript">
                <!--
                // this is simply a shortcut for the eyes and fingers
                function $(id) {
                    return document.getElementById(id);
                }
                var _startX = 0; // mouse starting positions
                var _startY = 0;
                var _offsetX = 0; // current element offset
                var _offsetY = 0;
                var _dragElement; // needs to be passed from OnMouseDown to OnMouseMove
                var _oldZIndex = 0; // we temporarily increase the z-index during drag
                var _debug = $('debug'); // makes life easier
                InitDragDrop();

                function InitDragDrop() {
                    document.onmousedown = OnMouseDown;
                    document.onmouseup = OnMouseUp;
                }

                function OnMouseDown(e) {
                    // IE is retarded and doesn't pass the event object
                    if (e == null) e = window.event;
                    // IE uses srcElement, others use target
                    var target = e.target != null ? e.target : e.srcElement;
                    _debug.innerHTML = target.className == 'parent' //Selector
                    ?
                    'draggable element clicked' : 'NON-draggable element clicked';
                    // for IE, left click == 1
                    // for Firefox, left click == 0
                    if ((e.button == 1 && window.event != null || e.button == 0) && target.className == 'parent') //Selector
                    {
                        // grab the mouse position
                        _startX = e.clientX;
                        _startY = e.clientY;
                        // grab the clicked element's position
                        _offsetX = ExtractNumber(target.style.left);
                        _offsetY = ExtractNumber(target.style.top);
                        // bring the clicked element to the front while it is being dragged
                        _oldZIndex = target.style.zIndex;
                        target.style.zIndex = 10000;
                        // we need to access the element in OnMouseMove
                        _dragElement = target;
                        // tell our code to start moving the element with the mouse
                        document.onmousemove = OnMouseMove;
                        // cancel out any text selections
                        document.body.focus();
                        // prevent text selection in IE
                        document.onselectstart = function () {
                            return false;
                        };
                        // prevent IE from trying to drag an image
                        target.ondragstart = function () {
                            return false;
                        };
                        // prevent text selection (except IE)
                        return false;
                    }
                }

                function ExtractNumber(value) {
                    var n = parseInt(value);
                    return n == null || isNaN(n) ? 0 : n;
                }

                function OnMouseMove(e) {
                    if (e == null) var e = window.event;
                    // this is the actual "drag code"
                    _dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px';
                    _dragElement.style.top = (_offsetY + e.clientY - _startY) + 'px';
                    _debug.innerHTML = '(' + _dragElement.style.left + ', ' + _dragElement.style.top + ')';
                }

                function OnMouseUp(e) {
                    if (_dragElement != null) {
                        _dragElement.style.zIndex = _oldZIndex;
                        // we're done with these events until the next OnMouseDown
                        document.onmousemove = null;
                        document.onselectstart = null;
                        _dragElement.ondragstart = null;
                        // this is how we know we're not dragging
                        _dragElement = null;
                        _debug.innerHTML = 'mouse up';
                    }
                }
                //-->
            </script>
        </div>
    </body>

</html>
4

1 に答える 1

3

jQuery UI のドラッグ可能およびドロップ可能を調べることをお勧めします。これらは間違いなく、リンクしたスクリプトよりも強力であり、要素を jQuery の強力なセレクターでドラッグできるように設定し、特定の要素内でのみドロップできるようにすることができます。

于 2012-05-15T14:56:20.633 に答える