我使用的是Jquery UI的draggable,并且我打开了恢复功能。
默认情况下,div会以“摆动”的方式恢复,
我想知道如何才能使div恢复为直线运动。
发布于 2012-04-26 22:12:48
你不能在draggable上使用常规的还原方法来获得自定义的返回缓动,因为它只支持更改持续时间。如果要使还原具有自定义效果,则需要如下所示的少量自定义代码,并插入JQueryUI showcase http://jqueryui.com/demos/effect/#easing中的任何自定义缓动效果。
请注意,在stop方法中,我将缓和效果设为easeInElastic以突出显示差异,但您可以将其更改为您想要的任何值(在本例中为线性)。
请注意,您需要包含JQuery UI才能获得这些效果。
http://jsfiddle.net/gregjuva/Hjf8p/
$(function() {
$("#draggable").draggable({
// We Can't use revert, as we animate the original object so
//revert: true, <- don't use this
helper: function(){
// Create an invisible div as the helper. It will move and
// follow the cursor as usual.
return $('<div></div>').css('opacity',0);
},
create: function(){
// When the draggable is created, save its starting
// position into a data attribute, so we know where we
// need to revert to.
// cache $this to keep from having to make
// lots of DOM calls w jquery
var $this = $(this);
$this.data('starttop',$this.position().top)
.data('startleft',$this.position().left);
},
stop: function(){
// When dragging stops, revert the draggable to its
// original starting position.
var $this = $(this);
$this.stop().animate({
top: $this.data('starttop'),
left:$this.data('startleft')
},1000,'easeInElastic');
// replace with whatever jQueryUI easing you want
},
drag: function(event, ui){
// During dragging, animate the original object to
// follow the invisible helper with custom easing.
$(this).stop().animate({
top: ui.helper.position().top,
left: ui.helper.position().left
},0,'linear');
}
});
});https://stackoverflow.com/questions/10324811
复制相似问题