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中Function类型详解
Apr 28 Javascript
JS实现黑色风格的网页TAB选项卡效果代码
Oct 09 Javascript
表单input项使用label同时引用Bootstrap库导致input点击效果区增大问题
Oct 11 Javascript
JS实现微信弹出搜索框 多条件查询功能
Dec 13 Javascript
javascript监听页面刷新和页面关闭事件方法详解
Jan 09 Javascript
JavaScript编写一个贪吃蛇游戏
Mar 09 Javascript
深究AngularJS中$sce的使用
Jun 12 Javascript
vue2过滤器模糊查询方法
Sep 16 Javascript
支付宝小程序自定义弹窗dialog插件的实现代码
Nov 30 Javascript
JavaScript栈和队列相关操作与实现方法详解
Dec 07 Javascript
vue实现移动端轻量日期组件不依赖第三方库的方法
Apr 28 Javascript
Vue el-autocomplete远程搜索下拉框并实现自动填充功能(推荐)
Oct 25 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 水平的题目
2007/05/30 PHP
利用php+mysql来做一个功能强大的在线计算器
2010/10/12 PHP
PHP __autoload函数(自动载入类文件)的使用方法
2012/02/04 PHP
php源码分析之DZX1.5字符串截断函数cutstr用法
2015/06/17 PHP
详解PHP中的状态模式编程
2015/08/11 PHP
PHP的swoole扩展安装方法详细教程
2016/05/18 PHP
Zend Framework上传文件重命名的实现方法
2016/11/25 PHP
爱恋千雪-US-AscII加密解密工具(网页加密)下载
2007/06/06 Javascript
火狐浏览器(firefox)下获得Event对象以及keyCode
2008/11/13 Javascript
JQuery从头学起第二讲
2010/07/04 Javascript
由Javascript实现的页面日历
2011/11/04 Javascript
利用js的Node遍历找到repeater的一个字段实例介绍
2013/04/25 Javascript
Javascript的表单验证长度
2016/03/16 Javascript
Node.js环境下JavaScript实现单链表与双链表结构
2016/06/12 Javascript
原生js仿jquery实现对Ajax的封装
2016/10/04 Javascript
vue+iview+less 实现换肤功能
2018/08/17 Javascript
详解elementui之el-image-viewer(图片查看器)
2019/08/30 Javascript
如何在微信小程序中存setStorage
2019/12/13 Javascript
Python中还原JavaScript的escape函数编码后字符串的方法
2014/08/22 Python
Python中__call__用法实例
2014/08/29 Python
python实现合并两个数组的方法
2015/05/16 Python
详解Django中Request对象的相关用法
2015/07/17 Python
Python numpy实现数组合并实例(vstack,hstack)
2018/01/09 Python
pycharm 主题theme设置调整仿sublime的方法
2018/05/23 Python
Windows下安装Scrapy
2018/10/17 Python
用Python实现将一张图片分成9宫格的示例
2019/07/05 Python
关于pytorch处理类别不平衡的问题
2019/12/31 Python
Python字符编码转码之GBK,UTF8互转
2020/02/09 Python
Python利用matplotlib绘制折线图的新手教程
2020/11/05 Python
python爬虫中url管理器去重操作实例
2020/11/30 Python
HTML5标签小集
2011/08/02 HTML / CSS
Zavvi美国:英国娱乐之家
2017/03/19 全球购物
广告学专业应届生求职信
2013/10/01 职场文书
《大海那边》教学反思
2014/04/09 职场文书
学校联谊协议书
2014/09/16 职场文书
python析构函数用法及注意事项
2021/06/22 Python