gpt4 book ai didi

JQuery 可轻松拖动

转载 作者:行者123 更新时间:2023-12-03 22:06:38 25 4
gpt4 key购买 nike

我想用Jquery的draggable来实现ease效果。但我在这个插件中没有找到这个选项。所以我想知道是否有其他插件有这个选项 - 或者一个简单的解决方案。

我想要达到的效果是这样的:http://www.fileden.com/files/2009/6/4/2466215/dragease.swf

正如您所看到的,拖动时,由于缓动,图像移动感觉更加平滑。我还想将拖动限制在一个轴上,还需要使其恢复到原来的位置。 JQuery 的可拖动确实有最后两个选项,所以这很好。

示例代码已经为我提供了我想要的(除缓动之外):http://jsfiddle.net/dandoen/NJwER/1/

如有任何建议,我们将不胜感激。

干杯,丹东园

最佳答案

您可以使用原始的可拖动项,但需要几行额外的代码。我们创建一个不可见的助手,并手动为原始对象设置动画,以使用自定义缓动来跟随它。您可以使用动画持续时间和 easing function自定义效果。

如果您要使用任何可拖动对象,当鼠标悬停在它们上方时,它们应该正确激活,而不必等待对象到达那里。

唯一的缺点是,当我们手动更改原始元素的位置时,无法使用 revert 参数,但可以通过保存起始位置并在拖动停止时将对象动画回来来轻松解决这个问题。

HTML:

<div id="draggable" class="ui-widget-content">
<p>Revert the original</p>
</div>

CSS:

#draggable {
position: relative;
width: 100px;
height: 100px;
padding: 0.5em;
float: left;
margin: 0 10px 10px 0;
background-color: red;
border: 2px solid gray;
}

Javascript:

$(function() {
$("#draggable").draggable({
// Can't use revert, as we animate the original object
//revert: true,

axis: "y",
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.
var $this = $(this);
$this.data('starttop',$this.position().top);
},
stop: function(){
// When dragging stops, revert the draggable to its
// original starting position.
var $this = $(this);
$this.stop().animate({
top: $this.data('starttop')
},1000,'easeOutCirc');
},
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
},1000,'easeOutCirc');
}
});
});

演示:http://jsfiddle.net/NJwER/4/

更新:约束轴可拖动

根据要求,这是来自 this thread 的修改后的代码。原始版本是brianpeiris出色的轴约束可拖动扩展。

更改它非常简单,只需将上述位添加到代码中并使恢复成为可选即可。我将其重命名为draggableXYE(E 表示缓动)。它可能不是最优雅的解决方案,将其编写为 draggableXY 的小扩展可能很容易,但它可以完成这项工作。

当您打开动态模式时,拖动感觉非常有趣,当可拖动对象从一个轴捕捉到另一个轴时,它可以简化移动。

Javascript:

$.fn.draggableXYE = function(options) {
var defaultOptions = {
distance: 5,
dynamic: false
};
options = $.extend(defaultOptions, options);

// ADDED: Store startPosition for reverting
var startPosition = this.position();

// ADDED: Function to apply easing to passed element
function AnimateElement(element, newpos) {
$(element).stop().animate({
top: newpos.top,
left: newpos.left
}, 1000, 'easeOutCirc');
}

this.draggable({
distance: options.distance,
// ADDED: Helper function to create invisible helper
helper: function(){
return $('<div></div>').css('opacity',0);
},
start: function(event, ui) {
ui.helper.data('draggableXY.originalPosition', ui.position || {
top: 0,
left: 0
});
ui.helper.data('draggableXY.newDrag', true);
},
drag: function(event, ui) {
var originalPosition = ui.helper.data('draggableXY.originalPosition');
var deltaX = Math.abs(originalPosition.left - ui.position.left);
var deltaY = Math.abs(originalPosition.top - ui.position.top);

var newDrag = options.dynamic || ui.helper.data('draggableXY.newDrag');
ui.helper.data('draggableXY.newDrag', false);

var xMax = newDrag ? Math.max(deltaX, deltaY) === deltaX : ui.helper.data('draggableXY.xMax');
ui.helper.data('draggableXY.xMax', xMax);

var newPosition = ui.position;
if (xMax) {
newPosition.top = originalPosition.top;
}
if (!xMax) {
newPosition.left = originalPosition.left;
}

// ADDED: Animate original object with easing to new position
AnimateElement(this, newPosition);

return newPosition;
},
// ADDED: Stop event to support reverting
stop: function() {
if (options.revert) {
AnimateElement(this, startPosition);
}
}
});
};

用法:

$('.drag').draggableXYE({
revert: true,
dynamic: true
});

演示:http://jsfiddle.net/4C9p2/

关于JQuery 可轻松拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6398854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com