基于js鼠标拖动图片排序

分享一款基于js的图片排序效果。鼠标拖动图片,重新排列图片的排列顺序。该插件适用浏览器:IE8、360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗。效果图如下:

在线预览   源码下载

实现的代码。

html代码:

  <ul id="ul1">
        <li>
            <img src="images/1.jpg" width="200" height="150" /></li>
        <li>
            <img src="images/2.jpg" width="200" height="150" /></li>
        <li>
            <img src="images/3.jpg" width="200" height="150" /></li>
        <li>
            <img src="images/4.jpg" width="200" height="150" /></li>
        <li>
            <img src="images/5.jpg" width="200" height="150" /></li>
        <li>
            <img src="images/6.jpg" width="200" height="150" /></li>
    </ul>

css代码:

  *
        {
            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;
        }

js代码:

  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事件,清空内存
                        //检测是否普碰上,在交换位置
                        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; //低版本出现禁止符号
                }
            }
            //碰撞检测
            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];
                }
            }
        }

via:http://www.w2bc.com/Article/23686

时间: 2024-12-20 04:54:57

基于js鼠标拖动图片排序的相关文章

js鼠标拖动图片360度平面旋转

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

avalon js拖动图片排序

转载请注明: TheViper http://www.cnblogs.com/TheViper   什么是拖动图片排序? 就像微博这种,上传后允许用户通过拖动图片,调整几张图片的顺序. 可以看到微博在这里把每张图片固定了尺寸,稍微严谨点的话,就需要像上一篇文章那样,外面是响应式的等高等宽的若干div容器,里面则是等比例缩放的响应式图片. 下面说下要求. 1.当然首先图片要可以拖动. 2.图片移出其原本的位置,拖动到目标位置并且未松开鼠标完成拖动前,需要在目标位置设置占位符,让用户预览拖动完成后的

鼠标拖动图片移动

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>鼠标拖动图片移动</title> <script type="text/javascript" src="js/jquery-1.11.3.js&q

JS随意拖动图片

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>JS随意拖动图片丨河北冷风机</title> </head> <body onLoad="remove()"> <div id="div1" onMouseOv

js鼠标控制图片的特效,滚轮控制放大、缩小、鼠标拖动、聚焦。。。

项目需要做一个js控制图片的特效,滚轮控制放大.缩小.鼠标拖动等效果,网上找方法,各种报错.不兼容...最终自己研究出一套方案如下: 代码直接从项目中拷了,就不整理格式了 <script type="text/javascript"> //图片特效 by jifei_mei //图片大小,记录放大或缩小图片前的大小 var pic_size = { width:0, height:0 }; //绑定滚轮滚动事件 if (window.addEventListener) {

JS鼠标拖动

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>鼠标拖动</title> &l

js鼠标滑动图片显示隐藏效果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <!--声明当前页面的编码集charset=gbk中文编码gb2312,charset=utf-8国际编码--> <meta http-equi

js 鼠标上移 图片放大

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="

禁止鼠标拖动图片在新窗口打开

<script language="javascript">  function imgdragstart(){return false;}  </script> <script language="javascript">  for(i in document.images)document.images[i].ondragstart=imgdragstart;  </script> 或直接在body写就行了<