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.pswp.cn/news/452088.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/452088.shtml
英文地址,請注明出處:http://en.pswp.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 &&…