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

方法/步骤
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. Fortran并行计算的一些例子

    以下例子来自https://computing.llnl.gov/tutorials/openMP/exercise.html网站 一.打印线程(Hello world) C************* ...

  2. Finally 与 return

    网上有很多人探讨Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被执行?很多人都说不是,当然他们的回答是正确的,经过我试验,至少有两种情况下fina ...

  3. 【转】Unity3D开发之Http协议网络通信

    之前unity3d项目要做跟服务器通信的模块,然后服务器那边的协议是基于http的Jsonrpc通信方式一开始,用C#的本身类HttpWebRequest来提交请求,很快就在电脑上面成功了,代码也很简 ...

  4. java.util.concurrent.CopyOnWriteArrayList

    import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; impo ...

  5. wampserver

  6. 解决Eclipse启动报错Failed to create the Java Virtual Machine

    电脑:2G内存,WIN7 32位. 启动adt-bundle-windows-x86-20140702\eclipse\eclipse.exe时,报错[Failed to create the Jav ...

  7. Protocol and Delegate

    为什么使用委托? 答:比如,我上班的工作主要内容包括 (1)写代码(2)写文档(3)测试程序(4)接电话(5)会见客户 (1)(2)我自己全权负责,但是后面(3)(4)(5)我不想或者不方便自己做,所 ...

  8. 实验 snort安装配置与规则编写

    1 实验目的 在linux或windows任意一个平台下完成snort的安装,使snort工作在NIDS模式下,并编写符合相关情景要求的snort规则. 2 实验环境 物理机:windows 8.1 ...

  9. JMeter学习-005-JMeter 主要组件概要介绍及执行顺序

    本文将对 JMeter 主要组件(主要涉及 Threads(Users).Test Fragment.逻辑控制器.配置元件.定时器.前置处理器.Sampler.后置处理器.断言.监听器 十大组件)进行 ...

  10. ThreadLocal 多线程并发,数据隔离

    ThreadLocal:  创建一个线程本地变量. 本质:在ThreadLocal类中有一个Map,用于存储每一个线程的变量的副本. 优点:既实现多线程并发,游兼顾数据的安全性. 区别:Synchro ...