jquery.sortable.js源代码解读
/*
* 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源代码解读的更多相关文章
- 分享:json2.js源代码解读笔记
1. 怎样理解"json" 首先应该意识到,json是一种数据转换格式,既然是个"格式",就是个抽象的东西.它不是js对象,也不是字符串,它仅仅是一种格式,一种 ...
- 修改 jquery.validate.js 支持非form标签
尝试使用markdown来写一篇blog,啦啦啦 源代码传送门:github 在特殊情况下我们使用jquery.validate.js对用户输入的内容做验证的时候,表单并不是一定包含在form之中,有 ...
- 懒加载插件- jquery.lazyload.js
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
- jquery.datatable.js与CI整合 异步加载(大数据量处理)
http://blog.csdn.net/kingsix7/article/details/38928685 1.CI 控制器添加方法 $this->show_fields_array=arra ...
- 当 jquery.unobtrusive-ajax.js 遇上Web API
最近在熟悉Abp框架,其基于DDD领域驱动设计...前段可以绕过mvc直接调用根据app层动态生成的webapi,有点神奇~,Web API之前有简单接触过,WCF的轻量级版,一般用于做一写开发性的服 ...
- MVC - 11(下)jquery.tmpl.js +ajax分页
继续 mvc-11(上).dto:http://www.cnblogs.com/tangge/p/3840060.html jquery.tmpl.js 下载:http://pan.baidu.com ...
- 【jquery】jquery.cookie.js 的使用指南
之前有写过一篇原生 js 的 cookie 介绍,并且最后封装成 cookie.js 具体内容点击传送门. jquery.cookie.js 是一款轻量级的 cookie 插件,可以读取,写入和删除 ...
- easyloader.js源代码分析
http://www.cnblogs.com/jasonoiu/p/easyloader_source_code_analysis.html Jquery easyui是一个javascript UI ...
- aspx中的表单验证 jquery.validate.js 的使用 以及 jquery.validate相关扩展验证(Jquery表单提交验证插件)
这一期我们先讲在aspx中使用 jquery.validate插件进行表单的验证, 关于MVC中使用 validate我们在下一期中再讲 上面是效果,下面来说使用步骤 jQuery.Valid ...
随机推荐
- 单片机 认识HEX文件
看过几篇常用指令的用法后,我们换换口味,介绍一下Intel 原厂所公布的HEX文件标准格式,相信经过本文的介绍,一定可以让您对8051的操作有更进一步的认识.以下是一个程序经编译器编译后所得到的HEX ...
- 开发反模式 - SQL注入
一.目标:编写SQL动态查询 SQL常常和程序代码一起使用.我们通常所说的SQL动态查询,是指将程序中的变量和基本SQL语句拼接成一个完整的查询语句. string sql = SELECT * FR ...
- kibana 统计每天注册数
- 【转】android 兼容性测试 CTS 测试过程(实践测试验证通过)
原文网址:http://blog.csdn.net/jianguo_liao19840726/article/details/7222814 写这个博客的时候是为了记忆,建议大家还是看官方的说明,官方 ...
- Linux下core文件产生的一些注意问题
前面转载了一篇文章关于core文件的产生和调试使用的设置,但在使用有一些需要注意的问题,如 在什么情况 才会正确地产生core文件. 列出一些常见问题: 一,如何使用core文件 1. 使用core文 ...
- 理解 Linux 配置文件分类和使用
理解 Linux 配置文件分类和使用 本文说明了 Linux 系统的配置文件,在多用户.多任务环境中,配置文件控制用户权限.系统应用程序.守护进程.服务和其它管理任务.这些任务包括管理用户帐号.分配磁 ...
- XShell连接CentOS 7.2显示中文乱码问题的解决方法
背景 使用U盘往Windows主机.Linux主机传文件还是经常的事,但有时候文件名有中文, 传到Linux机器会有乱码,选择起来也很麻烦,最近刚好遇到,写下解决方法. 环境 Linux [root@ ...
- LabView 下载与安装
labview2014是目前labview软件的最新版本,新版本增加了多个VI服务器对象,增加了多个vi脚本对象,增加了labview第三方许可和激活工具包,同时针对程序框图.编辑环境.应用程序生成器 ...
- 获取客户端IP地址定位城市信息
获取客户端IP地址定位城市信息 1.首先获取客户端的IP地址 function getIPaddress(){ $IPaddress=''; if (isset($_SERVER)){ if (iss ...
- "a newer version of unity web player is required but the auto-update failed"
问题背景描述: 项目采用winform调用unity web player作为播放器在客户端使用. 在有些环境会出现标题所示错误. 经过一翻研究后发现是插件在向服务器请求更新以下文件时报http 30 ...