Jquery 多行拖拽图片排序 jq优化

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery图片拖动排序代码</title><style type="text/css">.item_container{position:relative;height:auto;overflow:hidden;}
.item_content ul{list-style:none;padding:0;margin:0;}
.item_content ul li{width:200px;height:160px;float:left;margin:10px }
.item_content{width:50%;height:auto;border:1px solid #ccc;float:left;}
.item_content .item{width:200px;height:120px;line-height:120px;text-align:center;cursor:pointer;background:#ccc;}
.item_content .item img{width:200px;height:120px;border-radius:6px;}
.close{display:block;width:20px;height:20px;top:0;right:0;z-index:9999;position:absolute;text-align:center;font-size:16px;cursor:pointer;color:aliceblue;}
</style></head>
<body>
<div class="item_container"><div class="item_content" id="imageChange"><ul><li><div class="item"> <img src="img/500x500-1.png" width="150" height="150"> </div></li><li><div class="item"> <img src="img/500x500-2.png" width="150" height="150"> </div></li><li><div class="item"> <img src="img/500x500-3.png" width="150" height="150"> </div></li><li><div class="item"> <img src="img/500x500-4.png" width="150" height="150"> </div></li><li><div class="item"> <img src="img/500x500-5.png" width="150" height="150"> </div></li><li><div class="item"> <img src="img/500x500-6.png" width="150" height="150"> </div></li><li><div class="item"> <img src="img/500x500-7.png" width="150" height="150"> </div></li></ul></div>
</div>
<script src="js/jquery-1.8.3.min.js"></script>
<script>
$(function () {function Pointer(x, y) {this.x = x;this.y = y;}function Position(left, top) {this.left = left;this.top = top;}$(".item_container .item").each(function (i) {  this.init = function () { // 初始化this.box = $(this).parent();$(this).attr("index", i).css({position: "absolute",left: this.box.position().left,top: this.box.position().top,cursor: "move"}).appendTo(".item_container");this.drag();},this.move = function (callback) {  // 移动
                $(this).stop(true).animate({left: this.box.position().left,//相对父级的距离
                    top: this.box.position().top}, 500, function () {if (callback) {callback.call(this);}});},this.collisionCheck = function () {var currentItem = this;var direction = null;$(this).siblings(".item").each(function () {if (currentItem.pointer.x > this.box.offset().left &&currentItem.pointer.y > this.box.offset().top &&(currentItem.pointer.x < this.box.offset().left + this.box.width()) &&(currentItem.pointer.y < this.box.offset().top + this.box.height())) {// 返回对象和方向if (currentItem.box.position().top < this.box.position().top) {direction = "down";} else if (currentItem.box.position().top > this.box.position().top) {direction = "up";} else {direction = "normal";}this.swap(currentItem, direction);}});},this.swap = function (currentItem, direction) { // 交换位置if (this.moveing) return false;var directions = {normal: function () {var saveBox = this.box;this.box = currentItem.box;currentItem.box = saveBox;this.move();$(this).attr("index", this.box.index());$(currentItem).attr("index", currentItem.box.index());},down: function () {// 移到上方var box = this.box;var node = this;var startIndex = currentItem.box.index();var endIndex = node.box.index();;for (var i = endIndex; i > startIndex; i--) {var prevNode = $(".item_container .item[index=" + (i - 1) + "]")[0];node.box = prevNode.box;$(node).attr("index", node.box.index());node.move();node = prevNode;}currentItem.box = box;$(currentItem).attr("index", box.index());},up: function () {// 移到上方var box = this.box;var node = this;var startIndex = node.box.index();var endIndex = currentItem.box.index();;for (var i = startIndex; i < endIndex; i++) {var nextNode = $(".item_container .item[index=" + (i + 1) + "]")[0];node.box = nextNode.box;$(node).attr("index", node.box.index());node.move();node = nextNode;}currentItem.box = box;$(currentItem).attr("index", box.index());}}directions[direction].call(this);},this.drag = function () { // 拖拽var oldPosition = new Position();var oldPointer = new Pointer();var isDrag = false;var currentItem = null;$(this).mousedown(function (e) {e.preventDefault();oldPosition.left = $(this).position().left;oldPosition.top = $(this).position().top;oldPointer.x = e.clientX;oldPointer.y = e.clientY;isDrag = true;currentItem = this;});$(document).mousemove(function (e) {var currentPointer = new Pointer(e.clientX, e.clientY);if (!isDrag) return false;$(currentItem).css({"opacity": "0.8","z-index": 999});var left = currentPointer.x - oldPointer.x + oldPosition.left;var top = currentPointer.y - oldPointer.y + oldPosition.top;$(currentItem).css({left: left,top: top});currentItem.pointer = currentPointer;// 开始交换位置
currentItem.collisionCheck();});$(document).mouseup(function () {if (!isDrag) return false;isDrag = false;currentItem.move(function () {$(this).css({"opacity": "1","z-index": 0});});});}this.init();});
});</script></body>
</html>

 

转载于:https://www.cnblogs.com/yi-miao/p/9269776.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/452088.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

应该把script标签放在哪里

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 概述&#xff1a; 如果在页面中写JS的话&#xff0c;那必然会用到script标签&#xff0c;理论上script标签放在哪里都是可以的&#xff…

网络爬虫--3.str和bytes的区别

文章目录一.bytes二.str和bytes相互转换三.bytearray一.bytes bytes对象只负责以二进制字节序列的形式记录所需记录的对象&#xff0c;至于该对象到底表示什么&#xff08;比如到底是什么字符&#xff09;则由相应的编码格式解码所决定。 bytes是Python 3中特有的&#xff0c…

git使用问题

1、错误&#xff1a;The following untracked working tree files would be overwritten by checkout 。后面跟了几个文件 场景&#xff1a;需要从一个分支切换到另一个分支时报错 方法&#xff1a;git clean -d -fx "" 原因&#xff1a;之前修改了.gitignore文件&am…

jdbc写入和读取过程

[jdbc写操作] Class.forName("com.mysql.jdbc.Driver"); Connection conn DriverManager.getConnection("jdbc:mysql://localhost:3306/big","root","root"); ppst conn.preparedStatement("insert into test(id,name,age) val…

分享11款主流的开源编程工具

导读&#xff1a;有了开源编程工具&#xff0c;在基于开源许可证的情况下您可以轻松学习、修改、提高代码的质量&#xff0c;本文收集了11款最主流的且有价值的开源编程工具。或许会给您带来一丝惊喜。一起来看下吧。 NO.1 Rhomobile Rhodes Ruby或许是Github上第二大流行语言…

数据库触发器

触发器是一种特殊类型的存储过程&#xff0c;它在指定的表中的数据发生变化时自动生效。唤醒调用触发器以响应 INSERT、UPDATE 或 DELETE 语句。触发器可以查询其它表&#xff0c;并可以包含复杂的Transact-SQL语句。将触发器和触发它的语句作为可在触发器内回滚的单个事务对待…

jQuery中ready与load事件的区别

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 jQuery中ready与load事件的区别 概述&#xff1a; 大家在工作中用jQuery的时候一定会在使用之前这样&#xff1a;12345678//document rea…

网络爬虫--4.requests的简单实用

文章目录一.简介二.基本GET请求1.最基本的GET请求--直接用get方法2.添加 headers 和查询参数parmas3.通过requests获取网络上图片的大小三.基本POST请求1.传入data数据四.代理&#xff08;proxies参数&#xff09;五.私密代理六.web客户端验证七.Cookies 和 Sission1.Cookies2.…

编程各类资源大放送

小编的公众号[编程软文]一直在运营着&#xff0c;但是我的公众号只是发一些技术类文章&#xff0c;没有特地为自己的公众号如何添粉去操作&#xff0c;目前公众号关注度比较低。看着其他人的公众号粉丝越来越多&#xff0c;小编也需要为自己做些事。为了提高自己公众号的关注度…

使用Spring框架能带来那些好处?

1、Dependency Injection(DI)方法使得构造器和JavaBean properties文件中的依赖关系一目了然。2、与EJB容器相比较&#xff0c;Ioc容器更加趋向于轻量级。这样一来Ioc容器在邮箱的内存和CPU资源的情况下进行应用程序的开发和发布就变得十分有利。3、Spring并没有闭门造车&#…

谁在告谁?移动专利混战图

移动领域激战正酣&#xff0c;同样是没有永远的朋友&#xff0c;只有永远的利益。 苹果刚刚起诉三星的Galaxy手机和平板电脑山寨了苹果的产品&#xff0c;而此前两家并没有过节。再比如微软和亚马逊以及HTC之间的授权协议争端。移动领域的争端如此之多&#xff0c;以至于看客无…

java中list()和listfile()

File fnew File("c:\\");String[] f1f.list();File[] f2f.listFiles();① list() 返回一个字符串数组&#xff0c;这些字符串指定此抽象路径名表示的目录中的文件和目录。 以C盘为例&#xff0c;返回的是c盘下文件夹名字的字符串数组,如[TEMP, Windows]②listFile…

光棍节程序员闯关秀过关全攻略

maven/java/web/bootstrapQQ群&#xff1a;566862629。希望更多人一起帮助我学习。 光棍节程序员闯关秀过关全攻略。程序员的寂寞谁能懂?"SF光棍节程序员闯关秀"智力挑战小游戏火热上线&#xff0c;看看你能闯到第几关&#xff1f; 游戏地址: http://segmentfault…

jekins搭建

2019独角兽企业重金招聘Python工程师标准>>> 转自 https://www.cnblogs.com/hdwang/p/6081994.html &#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xf…

网络爬虫--5.urllib库的基本使用(1)

文章目录一. 前言二. urlopen三. Request四. User-Agent五. 添加更多的Header信息1. 添加一个特定的header2. 随机添加/修改User-Agent一. 前言 所谓网页抓取&#xff0c;就是把URL地址中指定的网络资源从网络流中读取出来&#xff0c;保存到本地。 在Python中有很多库可以用来…

高性能计算

信息时代的硬件芯片和存储器价格以摩尔定律的形式下降&#xff0c;可是现在处理的数据量也越来越大。我们先以cocoa编程为例&#xff0c;然后再结合网格计算、云计算&#xff0c;综合对最新的高性能计算技术作介绍。 使用 runloop 在cocoa编程如果用NSThread开线程 [NSThread …

混合型面向对象语言和纯面向对象语言

20世纪80年代以来&#xff0c;面向对象语言像雨后春笋一样大量涌现&#xff0c;形成了两大类面向对象语言。 一类是纯面向对象语言&#xff0c;如Smalltalk和Eiffel等语言 另一类是混合型面向对象语言&#xff0c;也就是在过程语言的基础上增加面向对象机制&#xff0c;如C等…

块级元素的margin-left和margin-right的用法注意

此时是有效果显示的因为html文档流默认是从上往下&#xff0c;从左往右进行显示的&#xff0c;所以此时是有效果的。那如果此时把#son的块元素的margin-right:20px; 是没有效果的此时是没有效果的&#xff0c;如图所示&#xff1a;如果此时想要margin-right有效果的话&#xf…

Apache Tiles的基本使用

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 1、概述 对于一个新的技术&#xff0c;了解其基本的概念和和原理是学好该技术的基础。 2、Tiles的概念 Tiles 是复合视图模式&#xff0…

js--------1.时间

1 //获取当前时间 yyyy-MM-dd2 function getNowFormatDate() {3 var date new Date();4 var seperator1 "-";5 var seperator2 ":";6 var month date.getMonth() 1;7 var strDate date.getDate();8 if (month > 1 &&…