3

我正在尝试构建一个类似于 Twitter 的图像裁剪器 - http://jsfiddle.net/yarKr/1/。我坚持的是拖动图像的能力。不求助于 jquery ui的最佳方法是什么?

<div class="canvas">
    <span class="window"></span>
    <img src="http://www.dailystab.com/blog/wp-content/uploads/2009/03/katy-perry-esquire-4.jpg" class="draggable" />
</div>

我希望能够在 .canvas div 内移动拖动图像。

4

4 回答 4

4

像这样的东西会起作用:jsFiddle

var TheDraggable = null;

$(document).ready(function () {

    $('.draggable').on({
        mousedown: function () { TheDraggable = $(this); },
        mouseup: function () { TheDraggable = null; }
    });

    $(document).mousemove(function (e) {

        if (TheDraggable) {

            TheDraggable.css({'top': e.pageY, 'left': e.pageX});
        }
    });
});

然后对于 CSS,您添加以下内容:.draggable { position:absolute; }

您可以重写它并在重新定位时添加某种形式的缓动,更改光标或根据图片上初始点击发生的位置添加更精确的起点,但总体而言,这应该让您开始。

于 2013-07-21T19:58:38.793 回答
1

如何在拖动时将位置设为绝对位置并将其设置为鼠标位置或鼠标位置附近?

于 2013-07-21T19:45:44.043 回答
0

这是我的。 http://jsfiddle.net/pd1vojsL/

一个 div 中的 3 个可拖动按钮,拖动受 div 约束。

<div id="parent" class="parent">
    <button id="button1" class="button">Drag me</button>
    <button id="button2" class="button">Drag me</button>
    <button id="button3" class="button">Drag me</button>
</div>
<div id="log1"></div>
<div id="log2"></div>

需要 JQuery(仅):

$(function() {
    $('.button').mousedown(function(e) {
        if(e.which===1) {
            var button = $(this);
            var parent_height = button.parent().innerHeight();
            var top = parseInt(button.css('top')); //current top position
            var original_ypos = button.css('top','').position().top; //original ypos (without top)
            button.css({top:top+'px'}); //restore top pos
            var drag_min_ypos = 0-original_ypos;
            var drag_max_ypos = parent_height-original_ypos-button.outerHeight();
            var drag_start_ypos = e.clientY;
            $('#log1').text('mousedown top: '+top+', original_ypos: '+original_ypos);
            $(window).on('mousemove',function(e) {
                //Drag started
                button.addClass('drag');
                var new_top = top+(e.clientY-drag_start_ypos);
                button.css({top:new_top+'px'});
                if(new_top<drag_min_ypos) { button.css({top:drag_min_ypos+'px'}); }
                if(new_top>drag_max_ypos) { button.css({top:drag_max_ypos+'px'}); }
                $('#log2').text('mousemove min: '+drag_min_ypos+', max: '+drag_max_ypos+', new_top: '+new_top);
                //Outdated code below (reason: drag contrains too early)
                /*if(new_top>=drag_min_ypos&&new_top<=drag_max_ypos) {
                    button.css({top:new_top+'px'});
                }*/
            });
            $(window).on('mouseup',function(e) {
                 if(e.which===1) {
                    //Drag finished
                    $('.button').removeClass('drag');
                    $(window).off('mouseup mousemove');
                    $('#log1').text('mouseup');
                    $('#log2').text('');
                 }
            });
        }
    });
});
于 2015-02-27T01:30:34.520 回答
0

拖动图标 - 少跳

您可以使用的移动图标 借用 frenchie 的回答,我必须即时创建一个可移动的弹出窗口。必须在创建对象后为其添加可拖动性。这并非完美无瑕 - 比缓慢拖动更快,光标离开后面直到您松开,此时<div>然后捕捉并坚持光标移动,直到您再次单击。

<!-- language: lang-html -->
<input type="button" value="pop" onclick="pop('NN')" id="btnNN"><!-- NN is the query rownumber -->
<b style="display:none;" id="protoContent">
<div id="divdrag~">
    <img style="float:left;" id="drag~">
    my form <input id="inp1_~">...
</div>
</b>

<!-- language: lang-js -->
var TheDraggable = null;
$(document).ready(function () {
    $(document).mousemove(function (e) {
        if (TheDraggable) {
            TheDraggable.css({'top': e.pageY-15, 'left': e.pageX-15});
        }//"-15" gets the cursor back on img inside the <div>
    });
});
var gOpenPop="";
function pop(NN){
    if(gOpenPop!=""){
        document.getElementById("divdrag"+gOpenPop).style.display="none";//hide opened
        if(gOpenPop==NN){
            gOpenPop="";
            return;
        }
    }
    gOpenPop=NN;
    //add div after the button
    $("#btn"+NN).after(
        //get div w/ form, replace of any "~"s with argument NN
        $("#protoContent").html().replace(/~/g,NN)
    );
    //ojb created, now bind click for dragability to the img
    $('#drag'+NN).on(
        {mousedown: function () {TheDraggable = $("#divdrag"+NN); },//
        mouseup: function () { TheDraggable = null; }
    });
    ...
}
于 2019-10-31T20:57:13.527 回答