基于js實現(xiàn)的圖片拖拽排序源碼實例
更新時間:2020年11月04日 11:27:48 作者:Take your time_
這篇文章主要給大家介紹了關于如何基于js實現(xiàn)的圖片拖拽排序的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
效果圖:

直接上代碼
<script>
window.onload = function() {
var oUl = document.getElementById("ul1");
var aLi = oUl.getElementsByTagName("li");
var disX = 0;
var disY = 0;
var minZindex = 1;
var aPos = [];
for (var i = 0; i < aLi.length; i++) {
var t = aLi[i].offsetTop;
var l = aLi[i].offsetLeft;
aLi[i].style.top = t + "px";
aLi[i].style.left = l + "px";
aPos[i] = {
left: l,
top: t
};
aLi[i].index = i;
}
for (var i = 0; i < aLi.length; i++) {
aLi[i].style.position = "absolute";
aLi[i].style.margin = 0;
setDrag(aLi[i]);
}
//拖拽
function setDrag(obj) {
obj.onmouseover = function() {
obj.style.cursor = "move";
}
obj.onmousedown = function(event) {
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
obj.style.zIndex = minZindex++;
//當鼠標按下時計算鼠標與拖拽對象的距離
disX = event.clientX + scrollLeft - obj.offsetLeft;
disY = event.clientY + scrollTop - obj.offsetTop;
document.onmousemove = function(event) {
//當鼠標拖動時計算div的位置
var l = event.clientX - disX + scrollLeft;
var t = event.clientY - disY + scrollTop;
obj.style.left = l + "px";
obj.style.top = t + "px";
/*for(var i=0;i<aLi.length;i++){
aLi[i].className = "";
if(obj==aLi[i])continue;//如果是自己則跳過自己不加紅色虛線
if(colTest(obj,aLi[i])){
aLi[i].className = "active";
}
}*/
for (var i = 0; i < aLi.length; i++) {
aLi[i].className = "";
}
var oNear = findMin(obj);
if (oNear) {
oNear.className = "active";
}
}
document.onmouseup = function() {
document.onmousemove = null; //當鼠標彈起時移出移動事件
document.onmouseup = null; //移出up事件,清空內(nèi)存
//檢測是否普碰上,在交換位置
var oNear = findMin(obj);
if (oNear) {
oNear.className = "";
oNear.style.zIndex = minZindex++;
obj.style.zIndex = minZindex++;
startMove(oNear, aPos[obj.index]);
startMove(obj, aPos[oNear.index]);
//交換index
oNear.index += obj.index;
obj.index = oNear.index - obj.index;
oNear.index = oNear.index - obj.index;
} else {
startMove(obj, aPos[obj.index]);
}
}
clearInterval(obj.timer);
return false; //低版本出現(xiàn)禁止符號
}
}
//碰撞檢測
function colTest(obj1, obj2) {
var t1 = obj1.offsetTop;
var r1 = obj1.offsetWidth + obj1.offsetLeft;
var b1 = obj1.offsetHeight + obj1.offsetTop;
var l1 = obj1.offsetLeft;
var t2 = obj2.offsetTop;
var r2 = obj2.offsetWidth + obj2.offsetLeft;
var b2 = obj2.offsetHeight + obj2.offsetTop;
var l2 = obj2.offsetLeft;
if (t1 > b2 || r1 < l2 || b1 < t2 || l1 > r2) {
return false;
} else {
return true;
}
}
//勾股定理求距離
function getDis(obj1, obj2) {
var a = obj1.offsetLeft - obj2.offsetLeft;
var b = obj1.offsetTop - obj2.offsetTop;
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
//找到距離最近的
function findMin(obj) {
var minDis = 999999999;
var minIndex = -1;
for (var i = 0; i < aLi.length; i++) {
if (obj == aLi[i]) continue;
if (colTest(obj, aLi[i])) {
var dis = getDis(obj, aLi[i]);
if (dis < minDis) {
minDis = dis;
minIndex = i;
}
}
}
if (minIndex == -1) {
return null;
} else {
return aLi[minIndex];
}
}
}
</script>
<ul id="ul1">
<li><img src="https://www.jq22.com/img/cs/500x500-1.png" width="200" height="150 "></li>
<li><img src="https://www.jq22.com/img/cs/500x500-2.png " width="200 " height="150 "></li>
<li><img src="https://www.jq22.com/img/cs/500x500-3.png " width="200 " height="150 "></li>
<li><img src="https://www.jq22.com/img/cs/500x500-4.png " width="200 " height="150 "></li>
<li><img src="https://www.jq22.com/img/cs/500x500-5.png " width="200 " height="150 "></li>
<li><img src="https://www.jq22.com/img/cs/500x500-6.png " width="200 " height="150 "></li>
</ul>
* {
margin:0;
padding:0;
list-style:none
}
#ul1 {
width:660px;
position:relative;
margin:10px auto;
}
#ul1 li {
width:200px;
height:150px;
float:left;
list-style:none;
margin:10px;
}
#ul1 li:hover {
border-color:#9a9fa4;
box-shadow:0 0 6px 0 rgba(0,0,0,0.85);
}
#ul1 .active {
border:1px dashed red;
}
//通過class獲取元素
function getClass(cls) {
var ret = [];
var els = document.getElementsByTagName("*");
for (var i = 0; i < els.length; i++) {
//判斷els[i]中是否存在cls這個className;.indexOf("cls")判斷cls存在的下標,如果下標>=0則存在;
if (els[i].className === cls || els[i].className.indexOf("cls") >= 0 || els[i].className.indexOf(" cls") >= 0 || els[i].className.indexOf(" cls ") > 0) {
ret.push(els[i]);
}
}
return ret;
}
function getStyle(obj, attr) { //解決JS兼容問題獲取正確的屬性值
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, false)[attr];
}
function startMove(obj, json, fun) {
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var isStop = true;
for (var attr in json) {
var iCur = 0;
//判斷運動的是不是透明度值
if (attr == "opacity") {
iCur = parseInt(parseFloat(getStyle(obj, attr)) * 100);
} else {
iCur = parseInt(getStyle(obj, attr));
}
var ispeed = (json[attr] - iCur) / 8;
//運動速度如果大于0則向下取整,如果小于0想上取整;
ispeed = ispeed > 0 ? Math.ceil(ispeed) : Math.floor(ispeed);
//判斷所有運動是否全部完成
if (iCur != json[attr]) {
isStop = false;
}
//運動開始
if (attr == "opacity") {
obj.style.filter = "alpha:(opacity:" + (json[attr] + ispeed) + ")";
obj.style.opacity = (json[attr] + ispeed) / 100;
} else {
obj.style[attr] = iCur + ispeed + "px";
}
}
//判斷是否全部完成
if (isStop) {
clearInterval(obj.timer);
if (fun) {
fun();
}
}
}, 30);
}
總結
到此這篇基于js實現(xiàn)的圖片拖拽排序源碼的文章就介紹到這了,更多相關js圖片拖拽排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JS獲得鼠標位置(兼容多瀏覽器ie,firefox)腳本之家修正版
這段代碼經(jīng)過測試,支持ie和ff是個不錯的代碼,并修正了錯誤,希望大家先運行測試下2008-11-11
layui點擊數(shù)據(jù)表格添加或刪除一行的例子
今天小編就為大家分享一篇layui點擊數(shù)據(jù)表格添加或刪除一行的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
innertext , insertadjacentelement , insertadjacenthtml , ins
innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等區(qū)別...2007-06-06
javascript實現(xiàn)輸出指定行數(shù)正方形圖案的方法
這篇文章主要介紹了javascript實現(xiàn)輸出指定行數(shù)正方形圖案的方法,可實現(xiàn)javascript獲取用戶輸入及根據(jù)輸入?yún)?shù)打印圖形的功能,需要的朋友可以參考下2015-08-08

