本文为大家分享了jquery拖拽自动排序插件,供大家参考,具体内容如下
该插件并不是原生js写的,是基于jquery的,想看原生的话,请绕道而行。
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="drag.js"></script>
<style type="text/css">
div{
height: 200px;
overflow-y: auto;
}
ul{
margin: 0;
padding: 0;
list-style: none;
box-shadow: rgba(0, 0, 0, 0.2) 5px 5px 10px;
display: inline-block;
}
.drag-item{
width: 100px;
padding: 0 10px;
line-height: 38px;
cursor: move;
}
.draging{
background-color: #ccc !important;
}
.no-draging{
background-color: #fff !important;
}
</style>
</head>
<body>
<ul class="drag-box"> </ul>
</body>
<script type="text/javascript">
new Drag({
container: '.drag-box',
data: ['应用1','应用2','应用3','应用4','应用5','应用6','应用7','应用8','应用9'] });
</script>
</html>
js:
(function(win){ function Drag(opts){
this.init(opts);
};
Drag.prototype = {
constructor: Drag,
options: {
container: '',
data: [], //可以是数据,也可以是html标签
className: 'item'
},
//初始化
init: function(opts){
$.extend(this.options, opts);
this.$el = $(this.get('container'));
this._render();
this._bindEvent();
},
get: function(key){
return this.options[key];
},
set: function(key, value){
this.options[key]=value;
},
//渲染列表
_render: function(){
var me = this, lis = '',
data = me.get('data') || [];
for(var i=0,len=data.length;i<len;i++) lis+='<li class="drag-item" id="drag-item-'+i+'">'+ data[i]+'</li>';
me.$el.append(lis)
.find('li').attr('draggable',true)
.addClass(this.get('className'));
},
//绑定事件
_bindEvent: function(){
var me = this,
$lis = $('li', me.$el),
events = ['dragstart', 'dragenter', 'dragover', 'drop', 'dragend'];
$.each(events, function(index, item){
$lis.on(item, function(e){
me['_'+item+'Handle'] && me['_'+item+'Handle'](e, me);
});
})
$lis.hover(function(){
$(this).css('background-color','#eee');
},function(){
$(this).css('background-color','#fff');
});
},
//开始拖动










