如何使用 jQuery 模仿带有动画的排序事件?
我有一个项目列表,每个项目都包含一个复选框。当选中一个复选框时,我希望该项目将自身排序到列表的开头,最好是动画,就像用鼠标拖动它一样(但没有鼠标光标)。
这可能吗?我过去在触发可拖动/可排序事件时遇到了麻烦。我愿意接受替代解决方案,只需清楚该项目已移至何处即可。
编辑:我最初想问如何在 jQuery sortable() 列表上实现这一点,但显然我没有很好地沟通,所以我更新了问题以匹配所有精彩的答案。
I've got a list of items, each containing a checkbox. When a checkbox is ticked I'd like that item to sort itself to the beginning of the list, preferably animating as if it were being dragged with the mouse (but without a mouse cursor).
Is this possible? I've had trouble triggering draggable/sortable events in the past. I'm open to alternate solutions, it just need to be obvious where the item has moved to.
Edit: I was originally trying to ask how to make this happen on a jQuery sortable() list, but I obviously didn't communicate that very well, so I've updated the question to match all the wonderful answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与 avetarman 的第二个例子类似,这个小提琴不会等待新选中的框到达顶部,然后其他框就会占据其应有的位置。相反,它们都一起动画化:
http://jsfiddle.net/ebiewener/Y5Mdt/1/
最终,它是所有这些都是为了给您要移动的行一个绝对位置,以便它可以向上滑动到其他行之上,同时为这些其他行提供必要的 CSS 属性,以便它们在删除所选行时不会立即跳出位置从文件中流量用绝对定位。完成此操作后,所选行将失去其绝对定位,但会附加到行的开头。这允许所有 css 本质上被“重置”,以便您可以继续选择新行。
Similar to avetarman's second example, this fiddle doesn't wait for the newly checked box to reach the top before the other ones take their rightful place. Instead, they all get animated together:
http://jsfiddle.net/ebiewener/Y5Mdt/1/
Ultimately, it's all about giving the row you're moving an absolute position so that it can slide up over the other rows, while giving the necessary CSS attributes to those other rows so that they don't immediately jump out of place when the selected row is removed from the document flow with the absolute positioning. Once that is done, the selected row loses it's absolute positioning, but is appended to the beginning of the rows. This allows all the css to essentially be "reset", so that you can proceed selecting new rows.
检查这个小提琴。
这里没有使用任何插件。
http://jsfiddle.net/bN8x6/
也检查此更新。它使用相对定位。因此,在动画过程中,占位符被保留。
http://jsfiddle.net/bN8x6/1/
Check this fiddle.
No plugin is used here.
http://jsfiddle.net/bN8x6/
Check this update as well. It uses relative positioning. So, during animation the placeholder is being kept.
http://jsfiddle.net/bN8x6/1/
jQuery ui 有一个 transfer 动画。
快速 jsfiddle 。
您可以使用它来模拟排序,然后在回调中移动实际的复选框。
当我回到家时,我将更新演示以使用复选框列表进行排序。
jQuery ui has a transfer animation.
Quick jsfiddle here.
You could use this to immitate the sort then on the callback move the actual checkbox.
I will update the demo when I get home to use a list of checkboxes to do the sort.
试试这个。
此处演示: http://jsfiddle.net/naveen/gLBjt/
免责声明:这本质上是一个端口@redsquare 解决方案。 :)
Try this.
Demo here: http://jsfiddle.net/naveen/gLBjt/
Disclaimer: This is essentially a port of @redsquare solution. :)