jquery中可调整大小的可拖动对象。可能?

时间:2011-02-04 23:16:55

标签: jquery jquery-ui draggable resizable

我希望有一个可调整大小和可拖动的对象。我需要:

  • X
  • ý
  • 尺寸

的对象。

这可能吗?

http://www.jsfiddle.net/davidThomas/DGbT3/1/上有一个示例,它获取可拖动对象的x和y。我怎样才能让它可以调整大小?

由于


值得补充一点,这个问题与之前的问题有关,并建立在此问题之上:How to get the position of a draggable object?

2 个答案:

答案 0 :(得分:8)

当然...... jQuery UI适用于拖放,调整大小,选择和排序等复杂行为。

使用jQuery UI,您可以:

  • 掉落
  • 调整大小
  • 排序

你可以将所有事物连在一起。

重要功能包括jquery-ui.css文件。

JSFIDDLE:http://jsfiddle.net/uQWRk/

以下是存档的完整代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<html lang="en">
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css"/>

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {

        $('#dragThis').resizable({
            stop: function(event, ui) {
                var w = $(this).width();
                var h = $(this).height();
                console.log('StopEvent fired')
                console.log('Width:'+w);
                console.log('Height:'+h)    
            }
        }).draggable(
            {
                containment: $('body'),
                drag: function(){
                    var offset = $(this).offset();
                    var xPos = offset.left;
                    var yPos = offset.top;
                    $('#posX').text('x: ' + xPos);
                    $('#posY').text('y: ' + yPos);
                },
                stop: function(){
                    var finalOffset = $(this).offset();
                    var finalxPos = finalOffset.left;
                    var finalyPos = finalOffset.top;

            $('#finalX').text('Final X: ' + finalxPos);
            $('#finalY').text('Final X: ' + finalyPos);
                }
            });

        $('#dropHere').droppable(
            {
                accept: '#dragThis',
                over : function(){
                    $(this).animate({'border-width' : '5px',
                                     'border-color' : '#0f0'
                                    }, 500);
                    $('#dragThis').draggable('option','containment',$(this));
                }
            });
    });

</script>   
<style type="text/css">
    #dragThis {
        width: 6em;
        height: 6em;
        padding: 0.5em;
        border: 3px solid #ccc;
        border-radius: 0 1em 1em 1em;
        background-color: #fff;
        background-color: rgba(255,255,255,0.5);
    }

    #dropHere {
        width: 12em;
        height: 12em;
        padding: 0.5em;
        border: 3px solid #f90;
        border-radius: 1em;
        margin: 0 auto;
    }
</style>
</head>
<body>
<div id="dragThis">
<ul>
    <li id="posX"></li>
    <li id="posY"></li>
    <li id="finalX"></li>
    <li id="finalY"></li>
</ul>
</div>
<div id="dropHere"></div>
</body>
</html>

见评论: enter image description here

答案 1 :(得分:0)

如果你的意思是拖延,那就是这样:

$( "#elem" ).resizable().draggable();

您可以使用css指定的大小和位置(第一次)。 你可以用jQuery改变它们并获得(当你调整大小或移动时)。

您可以向draggable()和resizable()方法添加选项。参考在这里: http://jqueryui.com/demos/resizable/http://jqueryui.com/demos/draggable/

P.S。这段代码需要jquery-ui.js文件和jquery-ui.css(因为它绘制了一个标记来调整大小)

P.P.S。

使用您的链接: 在JavaScript框架中,我添加了一行:

$('#dropHere').resizable();

我在托管资源中添加了css风格“http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/themes/base/jquery-ui.css”

它有效!