【问题标题】:combined drag and drop js examples结合拖放js示例
【发布时间】:2020-02-25 13:52:16
【问题描述】:

通过以下代码,我尝试结合拖放示例来限制特定区域的拖动并添加其他可拖动对象。最初的三个 'drag'-div 的行为与预期一样,不能被拖到 'container'-div 之外。另一方面,后来添加的“拖动”-div 则没有。

如何添加像前三个“拖动”-div 一样留在“容器”内的可拖动对象?

<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>

【问题讨论】:

    标签: javascript jquery css


    【解决方案1】:

    嗯,它有效。

    <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>
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多