js拖拽换位置,使用数组方法
之前一直需要一个拖拽效果,网上找了些感觉不是不好用,就是写的有些地方让人不太满意,下面贡献一个自己写的。亲测可用,拖动后可互换位置!(带有注释) 方法/步骤
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拖拽换位置,使用数组方法的更多相关文章
- 关于 JS 拖拽功能的冲突问题及解决方法
前言 我在之前写过关于 JS 拖拽的文章,实现方式和网上能搜到的方法大致相同,别无二致,但是在一次偶然的测试中发现,这种绑定事件的方式可能会和其它的拖拽事件产生冲突,由此产生了对于事件绑定的思考.本文 ...
- html5 Sortable.js 拖拽排序源码分析
最近公司项目经常用到一个拖拽 Sortable.js插件,所以有空的时候看了 Sortable.js 源码,总共1300多行这样,写的挺完美的. 本帖属于原创,转载请出名出处. 官网http:// ...
- 再谈React.js实现原生js拖拽效果
前几天写的那个拖拽,自己留下的疑问...这次在热心博友的提示下又修正了一些小小的bug,也加了拖拽的边缘检测部分...就再聊聊拖拽吧 一.不要直接操作dom元素 react中使用了虚拟dom的概念,目 ...
- React.js实现原生js拖拽效果及思考
一.起因&思路 不知不觉,已经好几天没写博客了...近来除了研究React,还做了公司官网... 一直想写一个原生js拖拽效果,又加上近来学react学得比较嗨.所以就用react来实现这个拖 ...
- js拖拽效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 浅谈js拖拽
本文来自网易云社区 作者:刘凌阳 前言 本文依据半年前本人的分享<浅谈js拖拽>撰写,算是一篇迟到的文章. 基本思路 虽然现在关于拖拽的组件库到处都是,HTML5也把拖放纳入了标准.但考虑 ...
- 一步一步实现JS拖拽插件
js拖拽是常见的网页效果,本文将从零开始实现一个简单的js插件. 一.js拖拽插件的原理 常见的拖拽操作是什么样的呢?整过过程大概有下面几个步骤: 1.用鼠标点击被拖拽的元素 2.按住鼠标不放,移动鼠 ...
- js拖拽分析
js拖拽分析 思路 1.三个鼠标事件,mousedown,mousemove,mouseup 2.可移动性absolute 3.边界限制 得到鼠标点击处和div边界的距离,然后得出top 和 left ...
- 原生js拖拽、jQuery拖拽、vue自定义指令拖拽
原生js拖拽: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
随机推荐
- IIS7 .NET Runtime version 2.0.50727.5420 - 执行引擎错误(000007FEE77AAF0E) (80131506)
装完系统,配置完IIS,发现.NET程序报503错误,出错后连接池自动关闭 这个程序是需要连接access数据库的,打开系统日志发现错误如下: 错误应用程序名称: w3wp.exe,版本: 7.5.7 ...
- 在fortran下进行openmp并行计算编程
最近写水动力的程序,体系太大,必须用并行才能算的动,无奈只好找了并行编程的资料学习了.我想我没有必要在博客里开一个什么并行编程的教程之类,因为网上到处都是,我就随手记点重要的笔记吧.这里主要是open ...
- 运行EFDC出现这样的错误:forrt1:severe<157>:Program Exception-access violation
经过检查是由于TQSER出现读写错误,原来我的数据输入文件的时间是427天,后来延长到639,但其中有一个点的时间仍然维持在427.故此出现这个错误.EFDC是用Fortran编译的,通过debug才 ...
- [have_fun] 好玩哒小游戏又来啦
联机贪吃蛇,相互厮杀,试一下吧! http://splix.io/
- opencv提取截获图像(总结摘来)
opencv提取截获图像(总结摘来) http://blog.csdn.net/wuxiaoyao12/article/details/7305865 版权声明:本文为博主原创文章,未经博主允许不得转 ...
- Java中重点关键词的区分
1.final, finally, finalize的区别final-修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承. 因此一个类不能既被声明为 abs ...
- php--纯静态和伪静态的区别与关系
先前说了什么是纯静态和伪静态,现在介绍一下他们的区别? 首先肯定的是纯静态和伪静态都是SEO的产物,但纯静态和伪静态还是有很大区别的.纯静态是生成真实的HTML页面保存到服务器端,用户访问时直接访问这 ...
- qt 屏幕旋转
qt屏幕旋转的方法 参考链接 http://mikenoodle.blog.163.com/blog/static/11333522010102754154616/ http://blog.csdn. ...
- 玩转HTML5移动页面
(1) 动画雪碧图涉及的动画十分多,用的元素也十分多,请务必使用雪碧图(Sprite)!网上的工具有一些可以帮助你生成雪碧图的工具,例如CssGaga,GoPng等等,自动化构建工具Grunt和Gul ...
- 设置tomcat的编码为utf-8
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" ...