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:
- <scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"type="text/javascript"></script>
- <scriptsrc="javascript/jquery.gridly.js"type="text/javascript"></script>
- <linkhref="stylesheets/jquery.gridly.css"rel="stylesheet"type="text/css"/>
Example
Setting up a gridly is easy. The following snippet is a good start:
- <style type="text/css">
- .gridly {
- position: relative;
- width: 960px;
- }
- .brick.small {
- width: 140px;
- height: 140px;
- }
- .brick.large {
- width: 300px;
- height: 300px;
- }
- </style>
- <div class="gridly">
- <div class="brick small"></div>
- <div class="brick small"></div>
- <div class="brick large"></div>
- <div class="brick small"></div>
- <div class="brick small"></div>
- <div class="brick large"></div>
- </div>
- <script>
- $('.gridly').gridly({
- base: 60, // px
- gutter: 20, // px
- columns: 12
- });
- </script>
可以进行拖动、排序,动态效果。
怎么获取拖动的位置,进行排序保存呢?
之前有人说获取不到,实际上是可以的,通过两个回调函数实现,reordering拖动前回调,reordered拖动后回调。在初时化时需要这样指定:
- //初始化设置
- $('.gridly').gridly({
- callbacks:{ reordering: reordering , reordered: reordered }
- });
回调函数实例:
- //拖动前回调
- var reordering =function($elements){
- // Called before the drag and drop starts with the elements in their starting position.
- //alert('start');
- };
- //拖动后回调
- var reordered =function($elements){
- // Called after the drag and drop ends with the elements in their ending position.
- // 当前对象
- var currentObj =this.reordered.arguments[1];
- var currentId = currentObj[0].id;
- alert('拖动对象:'+ currentId);
- var arr = $elements;
- // 前一个对象
- var prevObj;
- // 后一个对象
- var afterObj;
- for(i =0; i < arr.length; i++){
- if(arr[i].id == currentId){
- if(i >0){
- prevObj = arr[i -1];
- }
- if(i +1< arr.length){
- afterObj = arr[i+1];
- }
- }
- }
- if(prevObj !=undefined){
- alert('前一个对象:'+ prevObj.id)
- }
- if(afterObj !=undefined){
- alert('后一个对象:'+ afterObj.id);
- }
- //执行保存排序,更新数据
- //sortData...
- };
- //初始化设置
- $('.gridly').gridly({
- callbacks: { reordering: reordering , reordered: reordered }
- });
在回调函数里得到了拖动的对象及前后位置的对象就可以知道位置了,再执行更新数据保存排序即可。
按官方文档做一个例子,可以下载附件 jquery-gridly-callback
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17