我希望有人可以帮助我。我认为这将是一个简单的事情,可以借助JQuery拖动,但也许我缺少一些东西。从本质上讲,我想将拖动元件放在鼠标光标的左上方或右上位置。
我希望我能做:
$(this).draggable("option", "cursorAt", {
left: 0,
top:0
});
顶部/左属性会更改,但似乎不想使用。
这是我的做法:
$( "#matches ul li:first-child" ).draggable({
addClasses: true,
snap: true,
cursor: "crosshair",
start: function(event, ui) {
$(this).draggable("option", "cursorAt", {
left: Math.floor(this.clientWidth / 2),
top: Math.floor(this.clientHeight / 2)
});
},
drag: function(event, ui) {
var dragged = ui.helper;
var dragged_data = dragged.data('draggable');
var direction = (dragged_data.originalPosition.left > dragged.position().left) ? 'left' : 'right';
$( "#matches ul li:first-child" ).removeClass('left right').addClass( direction );
if( direction == "left"){
$(this).draggable("option", "cursorAt", {
left: 0,
top:0
});
}else{
$(this).draggable("option", "cursorAt", {
right: 0,
top:0
});
}
},
stop: function(event, ui) {
$( "#matches ul li:first-child" ).removeClass('left right');
}
});
一如既往,谢谢。
ps我也只是尝试了Top&左0,使用CSS边距左左:/-元素的宽度。
您是否想要这样的东西?e
http://jsfiddle.net/deveshz/yvmff/2/
或将光标固定在左上方?