时间:2021-05-26
JS实现图片拖拽交换效果,供大家参考,具体内容如下
听 WEB前端javascript企业实战班 公开课,用JS实现了图片拖拽交换的目的;感谢老师的讲解。
实现要点
注意点
小技巧
实现
HTML
CSS
JS
var photo = document.getElementById("photo"); var oUl = photo.getElementsByTagName("ul")[0]; var aLi = oUl.getElementsByTagName("li"); var z = 2; var arr = []; for (var i = 0; i < aLi.length; i++) { arr.push([aLi[i].offsetLeft, aLi[i].offsetTop]); } for (var i = 0; i < aLi.length; i++) { aLi[i].style.position = "absolute"; aLi[i].style.left = arr[i][0] + "px"; aLi[i].style.top = arr[i][1] + "px"; aLi[i].style.margin = 0; } for (var i = 0; i < aLi.length; i++) { aLi[i].index = i; drag(aLi[i]); } function drag(obj) { obj.onmousedown = function(ev) { ev = ev || window.ev; var x = ev.clientX; var y = ev.clientY; var l = obj.offsetLeft; var t = obj.offsetTop; this.style.zIndex = z++; document.onmousemove = function(ev) { ev = ev || window.ev; var _left = ev.clientX - x + l; var _top = ev.clientY - y + t; obj.style.left = _left + "px"; obj.style.top = _top + "px"; var li = near(obj); for (var i = 0; i < aLi.length; i++) { aLi[i].style.background = ""; } if (li) { li.style.background = "#DF971F"; } } document.onmouseup = function() { document.onmousemove = null; document.onmousedown = null; var nearLi = near(obj); var tmp = 0; if (nearLi) { move(nearLi, {left:arr[obj.index][0], top:arr[obj.index][1]}); move(obj, {left:arr[nearLi.index][0], top:arr[nearLi.index][1]}); nearLi.style.background = ""; tmp = obj.index; obj.index = nearLi.index; nearLi.index = tmp; } else { move(obj, {left:arr[obj.index][0], top:arr[obj.index][1]}); } } return false; } } function impact(obj1, obj2) { var L1 = obj1.offsetLeft; var R1 = obj1.offsetLeft + obj1.offsetWidth; var T1 = obj1.offsetTop; var B1 = obj1.offsetTop + obj1.offsetHeight; var L2 = obj2.offsetLeft; var R2 = obj2.offsetLeft + obj2.offsetWidth; var T2 = obj2.offsetTop; var B2 = obj2.offsetTop + obj2.offsetHeight; if (L2 > R1 || T2 > B1 || R2 < L1 || B2 < T1) { return false; } else { return true; } } function near(obj) { var tmp = 5000; var oLi = ''; for (var i = 0; i < aLi.length; i++) { if (impact(obj, aLi[i]) && obj != aLi[i]) { var c = disCalc(obj, aLi[i]); if (tmp > c) { tmp = c; oLi = aLi[i]; } } } return oLi; } function disCalc(obj1, obj2) { var x = obj1.offsetLeft - obj2.offsetLeft; var y = obj1.offsetTop - obj2.offsetTop; return Math.sqrt(x * x + y * y); }move.js
function move(obj, json, endFn) { clearInterval(obj.timer); obj.timer = setInterval(function() { var bBtn = true; for (var attr in json) { var iCur = 0; if (attr == 'opacity') { if (Math.round(parseFloat(getStyle(obj,attr)) * 100) == 0) { iCur = Math.round(parseFloat(getStyle(obj,attr)) * 100); } else { iCur = Math.round(parseFloat(getStyle(obj,attr)) * 100) || 100; } } else { iCur = parseInt(getStyle(obj,attr)) || 0; } var iSpeed = (json[attr] - iCur) / 8; iSpeed = iSpeed >0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); if (iCur != json[attr]) { bBtn = false; } if (attr == 'opacity') { obj.style.filter = 'alpha(opacity=' +(iCur + iSpeed)+ ')'; obj.style.opacity = (iCur + iSpeed) / 100; } else { obj.style[attr] = iCur + iSpeed + 'px'; } } if (bBtn) { clearInterval(obj.timer); if (endFn) { endFn.call(obj); } } }, 30);}function getStyle(obj, attr) { if (obj.currentStyle) { return obj.currentStyle[attr]; } else { return getComputedStyle(obj, false)[attr]; }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家讲解了JS实现拖拽图片效果的详细代码,分享给大家供大家参考,具体内容如下javascriptevent对象的clientX,offsetX,scr
本文实例为大家分享了js实现简单图片拖拽效果的具体代码,供大家参考,具体内容如下//图片需要自己导入在当前显示区范围内实现点不到的小方块div{position
本文实例为大家分享了Vue实现移动端拖拽交换位置的具体代码,供大家参考,具体内容如下点击图片删除,拖拽可更改顺序,共4张删除该图片?删除取消exportdefa
本文实例讲述了JS实现漂亮的窗口拖拽效果。分享给大家供大家参考。具体如下:这是一款漂亮的JS窗口拖拽效果(改变大小/最小化/最大化/还原/关闭)特点:①窗口可以
本文实例为大家分享了js实现拖拽移动与缩放效果的具体代码,供大家参考,具体内容如下效果图如下-实现了简单的拖拽和缩放功能第一步—简单的拖拽功能//创建一个Mov