js实现照片墙功能实例


Posted in Javascript onFebruary 05, 2015

本文实例讲述了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="move.js"></script>

        <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="1l.jpg" width="200px" height="150px"></li>

            <li><img src="2l.jpg" width="200px" height="150px"></li>

            <li><img src="3l.jpg" width="200px" height="150px"></li>

            <li><img src="4l.jpg" width="200px" height="150px"></li>

            <li><img src="5l.jpg" width="200px" height="150px"></li>

            <li><img src="6l.jpg" width="200px" height="150px"></li>

            <li><img src="1l.jpg" width="200px" height="150px"></li>

            <li><img src="2l.jpg" width="200px" height="150px"></li>

            <li><img src="4l.jpg" width="200px" height="150px"></li>

        </ul>

    </body>  

</html>

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

Javascript 相关文章推荐
JavaScript Event学习第三章 早期的事件处理程序
Feb 07 Javascript
Jquery Change与bind事件代码
Sep 29 Javascript
中文字符串截取的js函数代码
Apr 17 Javascript
jQuery制作仿Mac Lion OS滚动条效果
Feb 10 Javascript
JS返回iframe中frameBorder属性值的方法
Apr 01 Javascript
JavaScript实现点击按钮就复制当前网址
Dec 14 Javascript
简单的JS时钟实例讲解
Jan 13 Javascript
微信小程序 页面跳转传参详解
Oct 28 Javascript
jQuery Ajax File Upload实例源码
Dec 12 Javascript
jQuery获取复选框选中的当前行的某个字段的值
Sep 15 jQuery
通过vue手动封装on、emit、off的代码详解
May 29 Javascript
vue实现百度搜索功能
Dec 28 Javascript
js实现仿百度瀑布流的方法
Feb 05 #Javascript
JS函数this的用法实例分析
Feb 05 #Javascript
IE下使用jQuery重置iframe地址时内存泄露问题解决办法
Feb 05 #Javascript
js关于命名空间的函数实例
Feb 05 #Javascript
js实现数字每三位加逗号的方法
Feb 05 #Javascript
javascript实现字符串反转的方法
Feb 05 #Javascript
Javascript中拼接大量字符串的方法
Feb 05 #Javascript
You might like
网友原创的PHP模板类代码
2008/09/07 PHP
php中ftp_chdir与ftp_cdup函数用法
2014/11/18 PHP
PHP parse_ini_file函数的应用与扩展操作示例
2019/01/07 PHP
PHP正则表达式处理函数(PCRE 函数)实例小结
2019/05/09 PHP
php curl发送请求实例方法
2019/08/01 PHP
php数组和链表的区别总结
2019/09/20 PHP
Extjs改变树节点的勾选状态点击按钮将复选框去掉
2013/11/14 Javascript
jquery根据name属性查找的小例子
2013/11/21 Javascript
在Node.js中实现文件复制的方法和实例
2014/06/05 Javascript
node.js操作mongoDB数据库示例分享
2014/11/26 Javascript
javascript实现平滑无缝滚动
2020/08/09 Javascript
解决jQuery ajax请求在IE6中莫名中断的问题
2016/06/20 Javascript
AngularJS基础 ng-switch 指令简单示例
2016/08/03 Javascript
js实现炫酷的左右轮播图
2017/01/18 Javascript
vue实现添加标签demo示例代码
2017/01/21 Javascript
原生JS与jQuery编写简单选项卡
2017/10/30 jQuery
微信小程序获取手机系统信息的方法【附源码下载】
2017/12/07 Javascript
vue利用axios来完成数据的交互
2018/03/23 Javascript
Vue.js实现数据响应的方法
2018/08/13 Javascript
Element-ui之ElScrollBar组件滚动条的使用方法
2018/09/14 Javascript
利用python在大量数据文件下删除某一行的例子
2019/08/21 Python
wxPython实现绘图小例子
2019/11/19 Python
Python使用shutil模块实现文件拷贝
2020/07/31 Python
整理HTML5中表单的常用属性及新属性
2016/02/19 HTML / CSS
阿拉伯世界最大的电子卖场:Souq埃及
2016/08/01 全球购物
阿根廷票务网站:StubHub阿根廷
2018/04/13 全球购物
墨尔本最受欢迎的复古风格品牌:Princess Highway
2018/12/21 全球购物
Quiksilver美国官网:始于1969年的优质冲浪服和滑雪板外套
2020/04/20 全球购物
J2EE面试题集锦(附答案)
2013/08/16 面试题
中文系学生自荐信范文
2013/11/13 职场文书
电脑教师的自我评价
2013/12/18 职场文书
万年牢教学反思
2014/02/15 职场文书
陈欧广告词
2014/03/14 职场文书
经济贸易系毕业生求职信
2014/05/31 职场文书
2015年学校少先队工作总结
2015/07/20 职场文书
SQL SERVER实现连接与合并查询
2022/02/24 SQL Server