JQuery 可轻松拖动
我想用Jquery的draggable来实现ease效果。但我在这个插件中没有找到这个选项。所以我想知道是否有其他插件有这个选项 - 或者一个简单的解决方案。
我想要达到的效果是这样的: http:// www.fileden.com/files/2009/6/4/2466215/dragease.swf
如您所见,拖动时,图像移动感觉更加平滑,因为缓和。我还想将拖动限制在一个轴上,还需要使其恢复到原来的位置。 JQuery 的可拖动确实有最后两个选项,所以这很好。
示例代码已经为我提供了我想要的(除缓动之外): http://jsfiddle.net/ dandoen/NJwER/1/
如有任何建议,我们将不胜感激。
干杯, 丹东园
I would like to achieve the ease effect with Jquery's draggable. But I did not find the option in this plugin. So I was wondering if there are other plugins that have this option - or an easy solution.
The effect I am trying to achieve is something this: http://www.fileden.com/files/2009/6/4/2466215/dragease.swf
As you can see, upon dragging, the image movement is feeling much smoother because of the easing. I would also like to constraint the dragging to one axis, also need to make it revert back to its place. JQuery's draggable does have this last two options, so thats nice.
The example code already provides me with what I want (except the easing): http://jsfiddle.net/dandoen/NJwER/1/
Any advise would be appreciated.
Cheers,
Dandoen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用原始的可拖动项,但需要几行额外的代码。我们创建一个不可见的助手,并手动为原始对象设置动画,以使用自定义缓动来跟随它。您可以使用动画持续时间和缓动函数来自定义效果。
如果您要使用任何可拖动对象,当鼠标悬停在它们上方时,它们应该正确激活,而不必等待对象到达那里。
唯一的缺点是,当我们手动更改原始元素的位置时,无法使用 revert 参数,但可以通过保存起始位置并在拖动停止时将对象动画回来来轻松解决这个问题。
HTML:
CSS:
Javascript:
演示: http://jsfiddle.net/NJwER/4/
更新:约束轴可拖动
根据要求,这里是来自 此线程。原始版本是brianpeiris出色的轴约束可拖动扩展。
更改它非常简单,只需将上述位添加到代码中并使恢复成为可选即可。我将其重命名为draggableXYE(E 表示缓动)。它可能不是最优雅的解决方案,将其编写为draggableXY 的小扩展可能很容易,但它可以完成这项工作。
当您打开动态模式时,拖动感觉非常有趣,当可拖动从一个轴捕捉到另一轴时,它可以简化移动。
JavaScript:
用法:
演示:http://jsfiddle.net/4C9p2/
You can use the original draggable, but you will need a few lines of extra code. We create an invisible helper and manually animate the original object to follow it with custom easing. You can play with the animation duration and the easing function to customise the effect.
In case you would use any draggables, they should be properly activating when the mouse hovers over them without having to wait for the object to get there.
The only drawback is that as we're changing the original element's position manually, the revert parameter can't be used, but it can be easily sorted out by saving the starting position and animating the object back when dragging stops.
HTML:
CSS:
Javascript:
Demo: http://jsfiddle.net/NJwER/4/
Update: Constrained-axis draggable
As requested, here's the modified code from this thread. The original is brianpeiris' brilliant axis-constrained draggables extension.
Changing it was very simple, just added the above bits to the code and made reverting optional. I renamed it to
draggableXYE
(E for easing that is). It might not be the most elegant solution, it would probably be easy to write it as a small extension to thedraggableXY
, but it will do the job.Dragging feels quite interesting when you switch dynamic mode on, it eases movement when the draggable snaps from one axis to the other.
Javascript:
Usage:
DEMO: http://jsfiddle.net/4C9p2/
我见过很多关于阻力缓和/势头的问题。我终于开始从我的解决方案中制作一个插件。在这里尝试一下:
http://jsfiddle.net/mattsahr/bKs7w/
基本使用很简单。
它整理了这个问题的一些早期工作。
注释——
CONTAINMENT——.dragMomentum 与“containment”选项配合得很好,当你放手时,用一个漂亮的弹回动作代替正常行为。如果没有容器 div,它会对浏览器窗口执行相同的从边缘弹回操作。
兼容性——适用于 ie9、chrome12、firefox5。过去了,我不知道。
I've seen a fair number of questions about drag easing/momentum. I finally got around to making a plugin from my solution. Try it here:
http://jsfiddle.net/mattsahr/bKs7w/
Basic use is straightforward.
It tidies up some earlier work from this question.
Notes --
CONTAINMENT -- .dragMomentum works pretty well with the "containment" option, replaces the normal behavior with a nice snap-back action when you let go. And it does the same snap-back-from-edge with the browser window if there's no container div.
COMPATIBILIEY -- It works on ie9, chrome12, firefox5. Past that, I dunno.
我不认为 Draggable 有这样的选择。您可能需要自己动手。如果您选择这样做,您可能需要执行以下操作:
http://jsfiddle.net/NJwER/2 /
这真的很粗糙(但有点有趣)。您可能希望根据元素距光标的距离使动画持续时间动态化,并使用默认值以外的缓动。
I don't think draggable has an option for that. You may need to roll your own. If you choose to, you may want to do something like this:
http://jsfiddle.net/NJwER/2/
this is really rough (but was sort of interesting). You would probably want to make the animation duration dynamic based on how far the element was from your cursor and use an easing other than the default.