jQuery Gridly 拖拽排序插件获得拖动的位置

本文介绍如何使用 jQuery Gridly 插件实现拖拽排序功能,并提供了详细的示例代码来说明如何获取拖动元素的位置信息,以便进行排序保存。
jQuery Gridly 拖拽排序插件获得拖动的位置
Installation

To install download one of these packages and include the jquery.gridly.js and jquery.gridly.css files in your head with the following:

 
 github:  https://github.com/ksylvest/jquery-gridly
 
  1. <scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"type="text/javascript"></script>
  2. <scriptsrc="javascript/jquery.gridly.js"type="text/javascript"></script>
  3. <linkhref="stylesheets/jquery.gridly.css"rel="stylesheet"type="text/css"/>

Example

Setting up a gridly is easy. The following snippet is a good start:

  1. <style type="text/css">
  2.   .gridly {
  3.     position: relative;
  4.     width: 960px;
  5.   }
  6.   .brick.small {
  7.     width: 140px;
  8.     height: 140px;
  9.   }
  10.   .brick.large {
  11.     width: 300px;
  12.     height: 300px;
  13.   }
  14. </style>
  15. <div class="gridly">
  16.   <div class="brick small"></div>
  17.   <div class="brick small"></div>
  18.   <div class="brick large"></div>
  19.   <div class="brick small"></div>
  20.   <div class="brick small"></div>
  21.   <div class="brick large"></div>
  22. </div>
  23. <script>
  24.   $('.gridly').gridly({
  25.     base: 60, // px 
  26.     gutter: 20, // px
  27.     columns: 12
  28.   });
  29. </script>

 

可以进行拖动、排序,动态效果。

 

怎么获取拖动的位置,进行排序保存呢?

之前有人说获取不到,实际上是可以的,通过两个回调函数实现,reordering拖动前回调,reordered拖动后回调。在初时化时需要这样指定:

  1. //初始化设置
  2. $('.gridly').gridly({
  3. callbacks:{ reordering: reordering , reordered: reordered }
  4. });​​

 

回调函数实例:

  1. //拖动前回调
  2. var reordering =function($elements){
  3. // Called before the drag and drop starts with the elements in their starting position.
  4. //alert('start');
  5. };
  6.  
  7. //拖动后回调
  8. var reordered =function($elements){
  9. // Called after the drag and drop ends with the elements in their ending position.
  10. // 当前对象
  11. var currentObj =this.reordered.arguments[1];
  12. var currentId = currentObj[0].id;
  13. alert('拖动对象:'+ currentId);
  14.  
  15. var arr = $elements;
  16. // 前一个对象
  17. var prevObj;
  18. // 后一个对象
  19. var afterObj;
  20. for(i =0; i < arr.length; i++){
  21. if(arr[i].id == currentId){
  22. if(i >0){
  23. prevObj = arr[i -1];
  24. }
  25. if(i +1< arr.length){
  26. afterObj = arr[i+1];
  27. }
  28. }
  29. }
  30. if(prevObj !=undefined){
  31. alert('前一个对象:'+ prevObj.id)
  32. }
  33. if(afterObj !=undefined){
  34. alert('后一个对象:'+ afterObj.id);
  35. }
  36. //执行保存排序,更新数据
  37. //sortData...
  38. };
  39.  
  40.  
  41.  //初始化设置
  42.  $('.gridly').gridly({
  43.  callbacks: { reordering: reordering , reordered: reordered }
  44.  });​

 

在回调函数里得到了拖动的对象及前后位置的对象就可以知道位置了,再执行更新数据保存排序即可。

 

按官方文档做一个例子,可以下载附件 jquery-gridly-callback 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值