以下代码:
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');
}
});
}
感谢您的回复!