jQuery draggable appendTo不起作用

时间:2013-04-04 12:57:59

标签: jquery html css draggable

以下代码:

的jQuery

    $('.dragBox').draggable({ 
        axis: 'y',
        appendTo: 'parent',
        containment: [0,0,0,150],
        start: function() {
            $(this).addClass('grabbing');
        },
        stop: function() {
            $(this).removeClass('grabbing');
        }
    });

HTML

<div class="container">
    <div class="cropBox"><div class="dragBox"></div></div>
</div>

但.dragBox不会附加到.cropBox。 内盒的y轴不从0开始,从-117开始。 117是.cropBox和窗口顶边之间的像素距离。

修改

我修好了

startDrag();

$(window).resize(function(){
    startDrag();
});
function startDrag(){
    var xAxis = $('.dragBox').outerWidth() - $('.cropBox img').outerWidth();
    var yAxis = $('.dragBox').outerHeight() - $('.cropBox img').outerHeight();
    var x1 = $('.dragBox').position().left;
    var y1 = $('.dragBox').position().top;
    if( $('.dragBox img').outerWidth() > $('.dragBox img').outerHeight() ) {
        var axis = 'x';
        var containment = [xAxis+x1,0,x1,0];
    } else {
        var axis = 'y';
        var containment = [0,yAxis+y1,0,y1];
    }

    $('.dragBox img').draggable({ 
        axis: axis,
        appendTo: 'parent',
        containment: containment,
        start: function() {
            $(this).addClass('grabbing');
        },
        stop: function() {
            $(this).removeClass('grabbing');
        }
    }); 
}

感谢您的回复!

1 个答案:

答案 0 :(得分:0)

据我了解你的帖子,你正在寻找:

 $('.dragBox').draggable({ 
    axis: 'y',
    containment: 'parent',
    start: function() {
        $(this).addClass('grabbing');
    },
    stop: function() {
        $(this).removeClass('grabbing');
    }
});