照片墙

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin:0;
                padding:0;
            }
            #ul1{
                400px;
                height:400px;
                background-color: skyblue;
                margin:20px auto;
                position:relative;
            }
            #ul1 li{
                100px;
                height:100px;
                background-color: #9e9e9e;
                float: left;
                list-style: none;
                text-align: center;
                line-height: 100px;
                margin-left: 25px;
                margin-top: 25px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <input type="button" id="input1" value="随机"/>
        <ul id="ul1">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
            <li>9</li>
        </ul>
    </body>
    <script src="js/tween.js"></script>
    <script>
        var oUl = document.getElementById('ul1');
        var oInput = document.getElementById('input1');
        var aLi = oUl.getElementsByTagName('li');
        var izIndex = 1;
        var arr2 = [];
        //布局转换,把浮动布局转换成定位布局
        var arr = [];
        for(var i=0;i<aLi.length;i++){
            //把每一项的top值和left值都存入数组中
            arr.push({'left': aLi[i].offsetLeft,'top': aLi[i].offsetTop});
        }
        for(var i=0;i<aLi.length;i++){
            aLi[i].style.position = 'absolute';
            aLi[i].style.top = arr[i].top + 'px';
            aLi[i].style.left = arr[i].left + '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){
                var ev = ev || window.event;
                
                var disX = ev.clientX - obj.offsetLeft;
                var disY = ev.clientY - obj.offsetTop;
                
                //确保被拖拽的元素的层级永远最高,不被覆盖
                obj.style.zIndex = izIndex++;
                document.onmousemove = function(ev){
                    var ev = ev || window.event;
                    
                    obj.style.left = ev.clientX - disX +'px';
                    obj.style.top = ev.clientY - disY +'px';
                    
                    for(var i=0;i<aLi.length;i++){
                        aLi[i].style.border = '';
                    }
                    var nL = nearby(obj);
                    if(nL){
                        nL.style.border = '1px solid red';
                    }
                }
                document.onmouseup = function(){
                    document.onmousemove = null;
                    document.onmouseup = null;
                    
                    var nL = nearby(obj);
                    //运动
                    if(nL){
                        starMove(nL,{left:arr[obj.index].left,top:arr[obj.index].top},500,'easeOut');
                        starMove(obj,{left:arr[nL.index].left,top:arr[nL.index].top},500,'easeOut');
                        nL.style.border = '';
                        
                        //索引转换
                        var changeIndex = nL.index;
                        nL.index = obj.index;
                        obj.index = changeIndex;
                    }else{
                        //如果没有碰撞还回到原始位置
                        starMove(obj,{left:arr[obj.index].left,top:arr[obj.index].top},500,'easeOut');
                        
                    }
                }
                return false;
            }
        }
        
        //碰撞
        function isCollision(obj,obj2){
            
            var T1 = obj.offsetTop;
            var L1 = obj.offsetLeft;
            var R1 = obj.offsetLeft + obj.offsetWidth;
            var B1 = obj.offsetTop + obj.offsetHeight;
            
            var T2 = obj2.offsetTop;
            var L2 = obj2.offsetLeft;
            var R2 = obj2.offsetLeft + obj2.offsetWidth;
            var B2 = obj2.offsetTop + obj2.offsetHeight;
            if(R1<L2 || L1>R2 || B1<T2 || T1>B2 ){            //碰不到的情况
                return false;
            }else{
                return true;
            }
        }
        
        function nearby(obj){
            var value = 9999;
            var index = -1;
            
            for(var i=0;i<aLi.length;i++){
                if(isCollision(obj,aLi[i]) && aLi[i] != obj){        //排除掉自身
                    
                    var c = j1(obj,aLi[i]);
                    if(c<value){
                        value = c;
                        index = i;
                    }
                }
            }
            if(index != -1){
                return aLi[index];
            }else{
                return false;
            }
        }
        
        function j1(obj1,obj2){
            var a = obj1.offsetLeft    - obj2.offsetLeft;
            var b = obj1.offsetTop - obj2.offsetTop;
            
            return Math.sqrt(a*a + b*b);
        }
        oInput.onclick = function(){
            var randomArr = [0,1,2,3,4,5,6,7,8];
            randomArr.sort(function(){
                return Math.random()-0.5;
            });
            for(var i=0;i<aLi.length;i++){
                starMove(aLi[i],{left:arr[randomArr[i]].left,top:arr[randomArr[i]].top},500,'easeOut');
                //调换位置之后,index也对应变化
                aLi[i].index = randomArr[i];
            }
        }
    </script>
</html>
tween.js
var Tween = {
            linear: function (t, b, c, d){  //匀速
                return c*t/d + b; 
            },
            easeIn: function(t, b, c, d){  //加速曲线
                return c*(t/=d)*t + b;
            },
            easeOut: function(t, b, c, d){  //减速曲线
                return -c *(t/=d)*(t-2) + b;
            },
            easeBoth: function(t, b, c, d){  //加速减速曲线
                if ((t/=d/2) < 1) {
                    return c/2*t*t + b;
                }
                return -c/2 * ((--t)*(t-2) - 1) + b;
            },
            easeInStrong: function(t, b, c, d){  //加加速曲线
                return c*(t/=d)*t*t*t + b;
            },
            easeOutStrong: function(t, b, c, d){  //减减速曲线
                return -c * ((t=t/d-1)*t*t*t - 1) + b;
            },
            easeBothStrong: function(t, b, c, d){  //加加速减减速曲线
                if ((t/=d/2) < 1) {
                    return c/2*t*t*t*t + b;
                }
                return -c/2 * ((t-=2)*t*t*t - 2) + b;
            },
            elasticIn: function(t, b, c, d, a, p){  //正弦衰减曲线(弹动渐入)
                if (t === 0) { 
                    return b; 
                }
                if ( (t /= d) == 1 ) {
                    return b+c; 
                }
                if (!p) {
                    p=d*0.3; 
                }
                if (!a || a < Math.abs(c)) {
                    a = c; 
                    var s = p/4;
                } else {
                    var s = p/(2*Math.PI) * Math.asin (c/a);
                }
                return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
            },
            elasticOut: function(t, b, c, d, a, p){    //正弦增强曲线(弹动渐出)
                if (t === 0) {
                    return b;
                }
                if ( (t /= d) == 1 ) {
                    return b+c;
                }
                if (!p) {
                    p=d*0.3;
                }
                if (!a || a < Math.abs(c)) {
                    a = c;
                    var s = p / 4;
                } else {
                    var s = p/(2*Math.PI) * Math.asin (c/a);
                }
                return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
            },    
            elasticBoth: function(t, b, c, d, a, p){
                if (t === 0) {
                    return b;
                }
                if ( (t /= d/2) == 2 ) {
                    return b+c;
                }
                if (!p) {
                    p = d*(0.3*1.5);
                }
                if ( !a || a < Math.abs(c) ) {
                    a = c; 
                    var s = p/4;
                }
                else {
                    var s = p/(2*Math.PI) * Math.asin (c/a);
                }
                if (t < 1) {
                    return - 0.5*(a*Math.pow(2,10*(t-=1)) * 
                            Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
                }
                return a*Math.pow(2,-10*(t-=1)) * 
                        Math.sin( (t*d-s)*(2*Math.PI)/p )*0.5 + c + b;
            },
            backIn: function(t, b, c, d, s){     //回退加速(回退渐入)
                if (typeof s == 'undefined') {
                   s = 1.70158;
                }
                return c*(t/=d)*t*((s+1)*t - s) + b;
            },
            backOut: function(t, b, c, d, s){
                if (typeof s == 'undefined') {
                    s = 3.70158;  //回缩的距离
                }
                return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
            }, 
            backBoth: function(t, b, c, d, s){
                if (typeof s == 'undefined') {
                    s = 1.70158; 
                }
                if ((t /= d/2 ) < 1) {
                    return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
                }
                return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
            },
            bounceIn: function(t, b, c, d){    //弹球减振(弹球渐出)
                return c - Tween['bounceOut'](d-t, 0, c, d) + b;
            },       
            bounceOut: function(t, b, c, d){
                if ((t/=d) < (1/2.75)) {
                    return c*(7.5625*t*t) + b;
                } else if (t < (2/2.75)) {
                    return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b;
                } else if (t < (2.5/2.75)) {
                    return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
                }
                return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
            },      
            bounceBoth: function(t, b, c, d){
                if (t < d/2) {
                    return Tween['bounceIn'](t*2, 0, c, d) * 0.5 + b;
                }
                return Tween['bounceOut'](t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
            }
        }
        function getStyle(obj,attr)
        {
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }
            else{
                return getComputedStyle(obj,false)[attr];
            }
        }
        function starMove(obj,json,time,fx,callBack)
        {
            
            var iNowTime=getNow();

            var iCur={};
            for(var attr in json)
            {
                if(attr=="opacity")
                {
                    iCur[attr]=Math.round(getStyle(obj,attr)*100);
                }
                else
                {
                    iCur[attr]=parseInt(getStyle(obj,attr));        
                }
            }
            clearInterval(obj.oTimer);
            obj.oTimer=setInterval(function(){
                var iTime=time-Math.max(0,iNowTime-getNow()+time);    
                for(var attr in json)
                {
                    var iVal=Tween[fx](iTime,iCur[attr],json[attr]-iCur[attr],time)
                    if(attr=="opacity")
                    {
                        obj.style.opacity=iVal/100;
                        obj.style.filter="alpha(opacity="+iVal+")"
                    }
                    else
                    {
                        obj.style[attr]=iVal+"px";
                    }
                }
                if(iTime==time)
                {
                    clearInterval(obj.oTimer);
                    if(callBack)
                    {
                        callBack.call(obj);
                    }
                }
            },14);
        
        }
        function getNow()
        {
            return new Date().getTime();
        }
原文地址:https://www.cnblogs.com/chenzhiyu/p/8192834.html