富贵资源网 Design By www.hznty.com

本文实例讲述了js实现照片墙功能的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:html5="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8">
        <title>js照片墙</title>
        <meta http-equiv="X-UA-Compatible" content="edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="Keywords" content="js照片墙">
        <meta name="description" content="js照片墙">
        <link href="css/style.css" rel="stylesheet">
        <script src="/UploadFiles/2021-04-02/move.js">         <style>
            *{padding: 0; margin: 0;}
            li{list-style: none;}
            ul{width: 660px; height:510px; background:#ccc;margin: 20px auto;}
            li{width:200px; height: 150px; margin: 10px; float: left; }
        </style>
        <script>
        /*
            var arr=['a','b','c','d','e','d','f'];
            var pos = arr.lastIndexOf('d');
            alert(pos);
            */
            window.onload=function(){
                var aLi = document.querySelectorAll('li');
                var oInput =  document.querySelector('#btn');
                var pos = [];
                var len= aLi.length;
                var izIndex= 2;
                //布局转换

                for(var i=0;i<len;i++){
                    pos.push([aLi[i].offsetLeft,aLi[i].offsetTop]);
                }
                for(var i=0;i<len;i++){
                    aLi[i].style.left=pos[i][0]+'px';
                    aLi[i].style.top=pos[i][1]+'px';
                    aLi[i].style.position = 'absolute';
                    aLi[i].style.margin = '0px';
                }
                for(var i=0;i<len;i++){
                     aLi[i].index = i;
                     setDrag(aLi[i]);
                }
                oInput.onclick=function(){
                    var randomArr = [0,1,2,3,4,5,6,7,8];
                    randomArr.sort(function(num1,num2){
                        return Math.random()-0.5;
                    })
                    for(var i=0;i<len;i++){
                        //增加随机位置的情况
                       startMove(aLi[i],{left:pos[randomArr[i]][0],top:pos[randomArr[i]][1]});
                       //修正索引
                       aLi[i].index = randomArr[i];
                    }
                }
                //拖拽
                function setDrag(obj){
                    obj.onmousedown =function(ev){
                        izIndex++;
                        obj.style.zIndex= izIndex;
                        var ev = ev || event;
                        var disX = ev.clientX - obj.offsetLeft;
                        var disY = ev.clientY - obj.offsetTop;
                        document.onmousemove=function(ev){
                            var ev = ev || event;
                            obj.style.left =  ev.clientX- disX +'px';
                            obj.style.top =  ev.clientY - disY +'px';
                            for(var i=0; i<len; i++){
                                aLi[i].style.border='none';
                            }
                            var nL = nearLi(obj);
                            if(nL){
                                nL.style.border='2px solid red';
                            }                         
                        }
                        document.onmouseup= function(){
                            document.onmousemove = null;
                            document.onmouseup = null;
                            var nL = nearLi(obj);
                            if(nL){
                                nL.style.border='2px solid red';
                                startMove(obj,{left:pos[nL.index][0],top:pos[nL.index][1]});
                                startMove(nL,{left:pos[obj.index][0],top:pos[obj.index][1]});
                                var tmp =nL.index;
                                nL.index = obj.index;
                                obj.index = tmp;
                                nL.style.border='';
                            }else{
                                startMove(obj,{left:pos[obj.index][0],top:pos[obj.index][1]});
                            }
                        }
                        return false;
                    }
                }
                //检测是否有碰撞
                function isDump(obj1,obj2){
                    var l1= obj1.offsetLeft;
                    var r1= l1+obj1.offsetWidth;
                    var t1= obj1.offsetTop;
                    var b1 =obj1.offsetHeight +t1;

                    var l2= obj2.offsetLeft;
                    var r2= l2+obj2.offsetWidth;
                    var t2= obj2.offsetTop;
                    var b2 =obj2.offsetHeight +t2;
                    if(b2<t1 || l2>r1 || r2<l1 || t2>b1){
                        return false;
                    }else{
                        return true;
                    }
                }
                //寻找最近的节点
                function nearLi(obj){
                    var index= -1;
                    var value =9999;
                   for(var i=0; i<len; i++){
                        if(isDump(obj,aLi[i]) && obj !=aLi[i]){
                            var c = getDis(obj,aLi[i]);
                            if(c <value){
                                value = c;
                                index = i;
                            }
                        }
                    }
                    if(index !=-1){
                        return aLi[index];
                    }else{
                        return false;
                    }
                }
                function getDis(obj1,obj2){
                    var x = obj1.offsetLeft - obj2.offsetLeft;
                    var y = obj1.offsetTop - obj2.offsetTop;
                    return  Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
                }
            }
        </script>
    </head>
    <body>
    <button id="btn">click</button>
        <ul>
            <li><img src="/UploadFiles/2021-04-02/1l.jpg">             <li><img src="2l.jpg">             <li><img src="3l.jpg">             <li><img src="4l.jpg">             <li><img src="5l.jpg">             <li><img src="6l.jpg">             <li><img src="1l.jpg">             <li><img src="2l.jpg">             <li><img src="4l.jpg">         </ul>
    </body> 
</html>

希望本文所述对大家的javascript程序设计有所帮助。

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。