/*
* HTML5 Sortable jQuery Plugin
* http://farhadi.ir/projects/html5sortable
*
* Copyright 2012, Ali Farhadi
* Released under the MIT license.
*/
(function($) {
var dragging, placeholders = $();
$.fn.sortable = function(options) {
var method = String(options);
options = $.extend({
connectWith: false
}, options);
return this.each(function() {
// 参数里有enable或disable或destroy
if (/^enable|disable|destroy$/.test(method)) {
// 有enable,设置后代元素的draggable属性为true,否则为false
var items = $(this).children($(this).data('items')).attr('draggable', method == 'enable');
// 有destroy属性,则是移除相关数据和事件
if (method == 'destroy') {
items.add(this).removeData('connectWith items')
.off('dragstart.h5s dragend.h5s selectstart.h5s dragover.h5s dragenter.h5s drop.h5s');
}
return;
}
// items是后代元素(依据参数的items属性来定)
var isHandle, index, items = $(this).children(options.items);
// placeholder是,<ul class="sortable-placeholder"></ul>,或<div class="sortable-placeholder"></div>
var placeholder = $('<' + (/^ul|ol$/i.test(this.tagName) ? 'li' : 'div') + ' class="sortable-placeholder">');
// 依据参数的handler属性,来绑定更后代元素的isHandle
items.find(options.handle).mousedown(function() {
isHandle = true;
}).mouseup(function() {
isHandle = false;
});
// 给元素的数据属性items赋值
$(this).data('items', options.items);
// placeholders放入placeholder
placeholders = placeholders.add(placeholder);
if (options.connectWith) {
$(options.connectWith).add(this).data('connectWith', options.connectWith);
} // 上面代码是做数据环境准备,下面代码开始绑定事件 items.attr('draggable', 'true').on('dragstart.h5s', function(e) {
if (options.handle && !isHandle) {
return false;
}
isHandle = false;
// dataTransfer 是 拖拽元素的数据接口
var dt = e.originalEvent.dataTransfer;
// effectAllowed 拖拽效果
dt.effectAllowed = 'move';
// 为拖拽元素添加指定数据
dt.setData('Text', 'dummy');
// dragging是正在拖拽的元素,index是该元素所在数组的位置
index = (dragging = $(this)).addClass('sortable-dragging').index();
}).on('dragend.h5s', function() {
dragging.removeClass('sortable-dragging').show();
// 移除 placeholders,但保留事件
placeholders.detach();
if (index != dragging.index()) {
items.parent().trigger('sortupdate', {item: dragging});
}
// 释放引用
dragging = null;
}).not('a[href], img').on('selectstart.h5s', function() {
// 当元素选中时,阻止元素背景色边蓝
this.dragDrop && this.dragDrop();
return false;
}).end().add([this, placeholder]).on('dragover.h5s dragenter.h5s drop.h5s', function(e) {
// 注意dragenter,dragover,drop的this是目标元素 // 拖拽元素,不是items集合内,不给拖拽
if (!items.is(dragging) && options.connectWith !== $(dragging).parent().data('connectWith')) {
return true;
}
if (e.type == 'drop') {
e.stopPropagation();
// 当拖拽的对象,被放下。则在坑后面填入多拽的对象
placeholders.filter(':visible').after(dragging);
return false;
}
e.preventDefault();
e.originalEvent.dataTransfer.dropEffect = 'move';
if (items.is(this)) { if (options.forcePlaceholderSize) {
// 大多数情况下,这一步不会实现
placeholder.height(dragging.outerHeight());
}
// 隐藏被拖动的元素
dragging.hide();
$(this)[placeholder.index() < $(this).index() ? 'after' : 'before'](placeholder);
placeholders.not(placeholder).detach();
console.log(this); } else if (!placeholders.is(this) && !$(this).children(options.items).length) {
placeholders.detach();
$(this).append(placeholder);
}
return false;
});
});
};
})(jQuery);

jquery.sortable.js源代码解读的更多相关文章

  1. 分享:json2.js源代码解读笔记

    1. 怎样理解"json" 首先应该意识到,json是一种数据转换格式,既然是个"格式",就是个抽象的东西.它不是js对象,也不是字符串,它仅仅是一种格式,一种 ...

  2. 修改 jquery.validate.js 支持非form标签

    尝试使用markdown来写一篇blog,啦啦啦 源代码传送门:github 在特殊情况下我们使用jquery.validate.js对用户输入的内容做验证的时候,表单并不是一定包含在form之中,有 ...

  3. 懒加载插件- jquery.lazyload.js

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  4. jquery.datatable.js与CI整合 异步加载(大数据量处理)

    http://blog.csdn.net/kingsix7/article/details/38928685 1.CI 控制器添加方法 $this->show_fields_array=arra ...

  5. 当 jquery.unobtrusive-ajax.js 遇上Web API

    最近在熟悉Abp框架,其基于DDD领域驱动设计...前段可以绕过mvc直接调用根据app层动态生成的webapi,有点神奇~,Web API之前有简单接触过,WCF的轻量级版,一般用于做一写开发性的服 ...

  6. MVC - 11(下)jquery.tmpl.js +ajax分页

    继续 mvc-11(上).dto:http://www.cnblogs.com/tangge/p/3840060.html jquery.tmpl.js 下载:http://pan.baidu.com ...

  7. 【jquery】jquery.cookie.js 的使用指南

    之前有写过一篇原生 js 的 cookie 介绍,并且最后封装成 cookie.js 具体内容点击传送门. jquery.cookie.js 是一款轻量级的 cookie 插件,可以读取,写入和删除 ...

  8. easyloader.js源代码分析

    http://www.cnblogs.com/jasonoiu/p/easyloader_source_code_analysis.html Jquery easyui是一个javascript UI ...

  9. aspx中的表单验证 jquery.validate.js 的使用 以及 jquery.validate相关扩展验证(Jquery表单提交验证插件)

    这一期我们先讲在aspx中使用 jquery.validate插件进行表单的验证, 关于MVC中使用 validate我们在下一期中再讲     上面是效果,下面来说使用步骤 jQuery.Valid ...

随机推荐

  1. MVC之实体框架(数据持久化框架)EntityFrameWork(EF)

    EF - EntityFrameWork 中文名:实体框架(数据持久化框架) 1.使用EF查询(Linq to EF) 1.1使用标准查询运算符来查询 OumindBlogEntities db = ...

  2. poj3261 -- Milk Patterns

                                                                        Milk Patterns Time Limit: 5000MS ...

  3. cocos2d-x特效之CCControlPotentiometer

    在test示例下面,有一个关于此功能的代码,实现的效果如下: 通过拉动可旋转的按钮,从而改变所代表的值,这个效果的确是很棒的,但,和我的需求有一些差别,先贴上我实现的效果吧               ...

  4. java设计模式--行为型模式--模板方法

    什么是模板方法,这个有待考虑,看下面: 模板方法 概述 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中. TemplateMethod使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步 ...

  5. /etc/fstab 文件详解 及 /etc/mtab

    /etc/fstab 文件解释 文件fstab包含了你的电脑上的存储设备及其文件系统的信息.它是决定一个硬盘(分区)被怎样使用或者说整合到整个系统中的唯一文件. 这个文件的全路径是/etc/fstab ...

  6. Java 安装配置

    1.下载 进入官方网站,点击下载链接进入下载页面,选择合适的版本(如,jdk-6u31-windows-i586.exe)下载. 2.安装 双击jdk-6u31-windows-i586.exe文件, ...

  7. Duff策略

    Tom Duff首先在C语言中提出了展开循环的构想,所以这种模式被称之为Duff策略.Duff策略背后的思想是每一次循环完成标准循环的1-8次.首先通过数组值得总数除以8来取定循环次数.Duff发现对 ...

  8. Qt creator 搭配 valgrind 检测内存泄漏

    继上次重载operator new检测内存泄漏失败之后,妥协了.决定不管是否是准确指明哪一行代码出现内存泄漏,只要告诉我是否有泄漏就行了,这样就没有new替换的问题.在开发中,总是一个个小功能的开发. ...

  9. 字符(汉子)转换为ASCII

    一般在jdk里面都会自包含一个官方提供的转换工具:native2ascii.exe 调用方法: 打开cmd界面,使用cd  C:\Program Files\Java\jdk1.6.0_39\bin命 ...

  10. SICP 习题 (1.9) 解题总结

    SICP 习题 1.9 开始针对“迭代计算过程”和“递归计算过程”,有关迭代计算过程和递归计算过程的内容在书中的1.2.1节有详细讨论,要完成习题1.9,必须完全吃透1.2.1节的内容,不然的话,即使 ...