当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - combined drag and drop js examples

问题描述:

with the following code I tried to combine drag and drop examples for dragging restricted to a certain area and adding additional draggables. The inital three 'drag'-divs behave as expected and can't be dragged outside of the 'container'-div. The later on added 'drag'-divs on the other hand do not.

How can I add draggables which stay inside of 'container' like the first three 'drag'-divs?

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta http-equiv="X-UA-Compatible" content="IE=8" />

<link href="http://threedubmedia.com/inc/img/favicon.ico" rel="shortcut icon" />

<link href="http://threedubmedia.com/inc/css/base.css" rel="stylesheet" />

<script src="http://threedubmedia.com/inc/js/jquery-1.7.2.js"></script>

<script src="http://threedubmedia.com/inc/js/jquery.event.drag-2.2.js"></script>

<script src="http://threedubmedia.com/inc/js/jquery.event.drag.live-2.2.js"></script>

<script src="http://threedubmedia.com/inc/js/jquery.event.drop-2.2.js"></script>

<script src="http://threedubmedia.com/inc/js/jquery.event.drop.live-2.2.js"></script>

<script src="http://threedubmedia.com/inc/js/excanvas.min.js"></script>

<title>ThreeDubMedia &middot; jquery.event.drag</title>

</head>

<body>

<script type="text/javascript">

jQuery(function($){

var num = 1;

var $div = $('#container');

$('.drag')

.drag("start",function( ev, dd ){

dd.limit = $div.offset();

dd.limit.bottom = dd.limit.top + $div.outerHeight() - $( this ).outerHeight();

dd.limit.right = dd.limit.left + $div.outerWidth() - $( this ).outerWidth();

})

.drag(function( ev, dd ){

$( this ).css({

top: Math.min( dd.limit.bottom, Math.max( dd.limit.top, dd.offsetY ) ),

left: Math.min( dd.limit.right, Math.max( dd.limit.left, dd.offsetX ) )

});

});

$('#add').click(function(){

$('<div class="drag"/>')

.text( num++ )

.appendTo( document.body )

.css({

top: Math.random() * 241 + $div.offset().top,

left: Math.random() * ( $( window ).width() - 100 ) + 20,

});

});

});

</script>

<h1>Live Drag Demo</h1>

<p>

<input type="button" id="add" value="Add a Box" />

to the screen, drag it around, thanks to "live" delegation.

</p>

<div id="container"></div>

<div class="drag" style="left:40px;"></div>

<div class="drag" style="left:120px;"></div>

<div class="drag" style="left:200px;"></div>

<style type="text/css">

.drag {

position: absolute;

border: 1px solid #89B;

background: #BCE;

height: 58px;

width: 58px;

cursor: move;

top: 120px;

text-align: center;

line-height: 58px;

}

#container {

height: 299px;

border: 1px dashed #888;

}

</style></body>

</html>

网友答案:

meh, it works.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<link href="http://threedubmedia.com/inc/img/favicon.ico" rel="shortcut icon" />
<link href="http://threedubmedia.com/inc/css/base.css" rel="stylesheet" />
<script src="http://threedubmedia.com/inc/js/jquery-1.7.2.js"></script>
<script src="http://threedubmedia.com/inc/js/jquery.event.drag-2.2.js"></script>
<script src="http://threedubmedia.com/inc/js/jquery.event.drag.live-2.2.js"></script>
<script src="http://threedubmedia.com/inc/js/jquery.event.drop-2.2.js"></script>
<script src="http://threedubmedia.com/inc/js/jquery.event.drop.live-2.2.js"></script>
<script src="http://threedubmedia.com/inc/js/excanvas.min.js"></script>
<title>ThreeDubMedia &middot; jquery.event.drag</title>
</head>
<body>
<script type="text/javascript">
jQuery(function($){
    var num = 1;
    var $div = $('#container');
    $('.drag')
        .drag("start",function( ev, dd ){
            dd.limit = $div.offset();
            dd.limit.bottom = dd.limit.top + $div.outerHeight() - $( this ).outerHeight();
            dd.limit.right = dd.limit.left + $div.outerWidth() - $( this ).outerWidth();
        })
        .drag(function( ev, dd ){
            $( this ).css({
                top: Math.min( dd.limit.bottom, Math.max( dd.limit.top, dd.offsetY ) ),
                left: Math.min( dd.limit.right, Math.max( dd.limit.left, dd.offsetX ) )
            });   
        });
    $('#add').click(function(){
        $('<div class="drag"/>')
            .text( num++ )
            .appendTo( document.body )
            .css({ 
                top: Math.random() * 241 + $div.offset().top, 
                left: Math.random() * ( $( window ).width() - 100 ) + 20,
            })
            .drag("start",function( ev, dd ){
                dd.limit = $div.offset();
                dd.limit.bottom = dd.limit.top + $div.outerHeight() - $( this ).outerHeight();
                dd.limit.right = dd.limit.left + $div.outerWidth() - $( this ).outerWidth();
            })
            .drag(function( ev, dd ){
                $( this ).css({
                    top: Math.min( dd.limit.bottom, Math.max( dd.limit.top, dd.offsetY ) ),
                    left: Math.min( dd.limit.right, Math.max( dd.limit.left, dd.offsetX ) )
                });   
            });
    });

});
</script>

<h1>Live Drag Demo</h1>
<p>
    <input type="button" id="add" value="Add a Box" />
    to the screen, drag it around, thanks to "live" delegation.
</p>
<div id="container"></div>
<div class="drag" style="left:40px;"></div>
<div class="drag" style="left:120px;"></div>
<div class="drag" style="left:200px;"></div>
<style type="text/css">
.drag {
    position: absolute;
    border: 1px solid #89B;
    background: #BCE;
    height: 58px;
    width: 58px;
    cursor: move;
    top: 120px;
    text-align: center;
    line-height: 58px;
    }
#container {
    height: 299px;
    border: 1px dashed #888;
    }
</style></body>
</html>
分享给朋友:
您可能感兴趣的文章:
随机阅读: