之前一直需要一个拖拽效果,网上找了些感觉不是不好用,就是写的有些地方让人不太满意,下面贡献一个自己写的。亲测可用,拖动后可互换位置!(带有注释)

方法/步骤
CSS代码部分
<style>
* { padding:0;margin:0;list-style: none }
html,body { height:100%; overflow:hidden; }
ul { margin:50px auto;position:relative; width:1100px; height:500px;}
ul li { float:left; width:25%; cursor:pointer; padding:10px; box-sizing:border-box; height:33%}
ul img { height:100%;width:100%; box-shadow:0 3px 4px rgba(102,102,102,0.5) }
</style>
JS代码部分
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function(){ var ps={L:[],R:[],T:[],B:[]};
for(var i=11; i>=0; i--){
var oLi = $('li').eq(i),
gLi = oLi.get(0); oLi.css({'left':gLi.offsetLeft+'px','top':gLi.offsetTop+'px','position':'absolute','margin':0});
ps.L.push(gLi.offsetLeft);
ps.T.push(gLi.offsetTop);
ps.R.push(gLi.offsetLeft + gLi.offsetWidth);
ps.B.push(gLi.offsetTop + gLi.offsetHeight);
};
ps.L = ps.L.reverse();
ps.R = ps.R.reverse();
ps.T = ps.T.reverse();
ps.B = ps.B.reverse(); //储存位置 //初始化
var b = 0; $(document).on('mousedown','li',function(e){
e.preventDefault();
var _this = this;
if(_this.setCapture){_this.setCapture()};
var X = e.clientX - _this.offsetLeft,
Y = e.clientY - _this.offsetTop,
oList = $('li'),
attr = [];
my_index = $( _this ).attr('index'); //初始保存一个原来的Index,回到原来的数组(位置)
$(_this).css({'opacity':0.9,'zIndex':1});
document.index = my_index; //目的是为了脱离变量作用域 $('li').each(function() {
attr.push( $(this).attr('index') )
}); $(document).on('mousemove',function(e){
var lt = e.clientX - X,
tp = e.clientY - Y,
screen_l = e.clientX - _this.parentNode.offsetLeft,
screen_t = e.clientY - _this.parentNode.offsetTop;
$(_this).css({'left':lt+'px','top':tp+'px'}); for(var i=0;i<12;i++){ var he_index = parseInt(oList.eq(i).attr('index'));
if(screen_l>ps.L[he_index]&&screen_l<ps.R[he_index]&&screen_t>ps.T[he_index]&&screen_t<ps.B[he_index]){
var i_index = parseInt($(_this).attr('index'));
if(he_index == i_index)continue;
document.index = he_index; //当找到元素保存要抵达的位置的index
document.flag = false;
var test = function (num,j){
var he_Li = $('li[index='+j+']');
$(he_Li).stop();
he_Li.animate({
left:ps.L[j+num],
top:ps.T[j+num]
},'fast');
he_Li.attr('index',j+num);
};
//利用属性选择器找到对应index(也就是找到数组相应位置)的元素;并且变换属性index到相应的数组索引; if(i_index>he_index){
for(var j=i_index-1; j>=he_index; j--){
test(1,j);
}
}else{
for(var j=i_index+1; j<he_index+1; j++){
test(-1,j);
}
};
$(_this).attr('index',he_index); //变换_this的index
document.flag = true; }else{
if(document.flag){
var parent = _this.parentNode,
parent_X = e.clientX - parent.offsetLeft,
parent_Y = e.clientY - parent.offsetTop;
if( parent_X<0||parent_X>parent.offsetWidth||parent_Y<0||parent_Y>parent.offsetHeight ){
oList.not(_this).each(function(index, element) {
var a = $(element).index();
$(element).animate({
left:ps.L[attr[a]],
top:ps.T[attr[a]]
},'fast').attr('index',attr[a])
});
document.index = my_index;
$(_this).attr('index',my_index);
document.flag = false;
//当移出父节点还原
} }
}; };
});
$(document).on('mouseup',function(){
if(_this.releaseCapture){
_this.releaseCapture();
};
$(this).off('mousemove');
$(this).off('mouseup'); $(_this).animate({
left:ps.L[document.index],
top:ps.T[document.index]
},'fast',function(){
$(_this).css({'opacity':1,'zIndex':0})
});
delete document.index;
delete document.flag; });
}); })
</script>
上面的是网页 head区域, 下面的是 body区域
<ul>
<li index="0"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="1"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="2"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="3"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="4"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="5"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="6"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="7"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="8"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="9"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="10"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li>
<li index="11"><img src="http://www.baidu.com/img/bd_logo1.png" title="百度" /></li> </ul>

  

js拖拽换位置,使用数组方法的更多相关文章

  1. 关于 JS 拖拽功能的冲突问题及解决方法

    前言 我在之前写过关于 JS 拖拽的文章,实现方式和网上能搜到的方法大致相同,别无二致,但是在一次偶然的测试中发现,这种绑定事件的方式可能会和其它的拖拽事件产生冲突,由此产生了对于事件绑定的思考.本文 ...

  2. html5 Sortable.js 拖拽排序源码分析

    最近公司项目经常用到一个拖拽 Sortable.js插件,所以有空的时候看了 Sortable.js 源码,总共1300多行这样,写的挺完美的.   本帖属于原创,转载请出名出处. 官网http:// ...

  3. 再谈React.js实现原生js拖拽效果

    前几天写的那个拖拽,自己留下的疑问...这次在热心博友的提示下又修正了一些小小的bug,也加了拖拽的边缘检测部分...就再聊聊拖拽吧 一.不要直接操作dom元素 react中使用了虚拟dom的概念,目 ...

  4. React.js实现原生js拖拽效果及思考

    一.起因&思路 不知不觉,已经好几天没写博客了...近来除了研究React,还做了公司官网... 一直想写一个原生js拖拽效果,又加上近来学react学得比较嗨.所以就用react来实现这个拖 ...

  5. js拖拽效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 浅谈js拖拽

    本文来自网易云社区 作者:刘凌阳 前言 本文依据半年前本人的分享<浅谈js拖拽>撰写,算是一篇迟到的文章. 基本思路 虽然现在关于拖拽的组件库到处都是,HTML5也把拖放纳入了标准.但考虑 ...

  7. 一步一步实现JS拖拽插件

    js拖拽是常见的网页效果,本文将从零开始实现一个简单的js插件. 一.js拖拽插件的原理 常见的拖拽操作是什么样的呢?整过过程大概有下面几个步骤: 1.用鼠标点击被拖拽的元素 2.按住鼠标不放,移动鼠 ...

  8. js拖拽分析

    js拖拽分析 思路 1.三个鼠标事件,mousedown,mousemove,mouseup 2.可移动性absolute 3.边界限制 得到鼠标点击处和div边界的距离,然后得出top 和 left ...

  9. 原生js拖拽、jQuery拖拽、vue自定义指令拖拽

    原生js拖拽: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

随机推荐

  1. Siddhi CEP Window机制

    https://docs.wso2.com/display/CEP400/SiddhiQL+Guide+3.0#SiddhiQLGuide3.0-Window https://docs.wso2.co ...

  2. mysql 存储过程 php版本

    <?php /** * PHP操作Mysql存储过程示例 * * @author flyer0126 * @date 2011-12-23 * */ //配置数据库连接信息 $hostname ...

  3. MySQL安装图解教程

    安装文件存放路径:不能有中文和空格! 校验 1 安装MySQL 2 校验MySQL 登录MySQL:mysql -uroot -p123 退出MySQL:exit | quit 查看数据库:show ...

  4. (转)js一道比较考验的题目

    转载下别人曾经出过的一道面试题,此题是他出的一套前端面试题中的最后一题,用来考核面试者的JavaScript的综合能力,很可惜到目前为止的将近两年中,几乎没有人能够完全答对,并非多难只是因为大多面试者 ...

  5. vim编辑器配置修改

    刚上手的vim,黑底白字,看起来笨死了,于是一顿狂找,终于找到了配置方法. 配置当然要去etc目录下. cd /etc/vim ls -l                        //找到vim ...

  6. Mixing Delphi and C++(相互调用)

    Mixing Delphi and C++ You have a TStringList and <algorithm>. What can you do? Quite a lot, ac ...

  7. mvc EF 数据保存时,报错:”对一个或多个实体的验证失败……“之解决

    在EF5.0添加实体数据到数据库的时候,出现“对一个或多个实体的验证失败.有关详细信息,请参见“EntityValidationErrors”属性这个错误 解决: SaveChanges前先关闭验证实 ...

  8. C++ 自动指针 共享指针

    #include <iostream> #include <string> #include <memory> class Item { public: Item( ...

  9. php--如何解决网站分页导致的SEO问题

    如何解决网站分页导致的SEO问题 分页(pagination)是一种自动分页机制,可以将移动Web窗体中的内容分割成一组组较小的页进行呈现,以适合于特定的设备,该机制还呈现可用于浏览到其他页的用户界面 ...

  10. windows下安装和配置mongoDB

    上次在mac下安装和配置了mongodb,这次在windows下也尝试安装和配置mongodb. 1.首先下载mongodb压缩包,下载后解压到D盘或E盘.如下: 2.配置环境变量:桌面—计算机右键— ...